dify/web/app/(commonLayout)/apps/AppCard.tsx

193 lines
5.7 KiB
TypeScript
Raw Normal View History

2023-05-15 08:51:32 +08:00
'use client'
import { useContext, useContextSelector } from 'use-context-selector'
import Link from 'next/link'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import cn from 'classnames'
2023-05-15 08:51:32 +08:00
import style from '../list.module.css'
import AppModeLabel from './AppModeLabel'
import s from './style.module.css'
import SettingsModal from '@/app/components/app/overview/settings'
2023-08-28 19:48:53 +08:00
import type { ConfigParams } from '@/app/components/app/overview/settings'
2023-05-15 08:51:32 +08:00
import type { App } from '@/types/app'
import Confirm from '@/app/components/base/confirm'
import { ToastContext } from '@/app/components/base/toast'
import { deleteApp, fetchAppDetail, updateAppSiteConfig } from '@/service/apps'
2023-05-15 08:51:32 +08:00
import AppIcon from '@/app/components/base/app-icon'
2023-08-15 13:35:47 +08:00
import AppsContext, { useAppContext } from '@/context/app-context'
import CustomPopover from '@/app/components/base/popover'
import Divider from '@/app/components/base/divider'
import { asyncRunSafe } from '@/utils'
2023-05-15 08:51:32 +08:00
export type AppCardProps = {
app: App
onRefresh?: () => void
2023-05-15 08:51:32 +08:00
}
const AppCard = ({ app, onRefresh }: AppCardProps) => {
2023-05-15 08:51:32 +08:00
const { t } = useTranslation()
const { notify } = useContext(ToastContext)
2023-08-15 13:35:47 +08:00
const { isCurrentWorkspaceManager } = useAppContext()
2023-05-15 08:51:32 +08:00
const mutateApps = useContextSelector(
AppsContext,
state => state.mutateApps,
)
2023-05-15 08:51:32 +08:00
const [showConfirmDelete, setShowConfirmDelete] = useState(false)
const [showSettingsModal, setShowSettingsModal] = useState(false)
const [detailState, setDetailState] = useState<{
loading: boolean
detail?: App
}>({ loading: false })
2023-05-15 08:51:32 +08:00
const onConfirmDelete = useCallback(async () => {
try {
await deleteApp(app.id)
notify({ type: 'success', message: t('app.appDeleted') })
if (onRefresh)
onRefresh()
2023-05-15 08:51:32 +08:00
mutateApps()
}
catch (e: any) {
notify({
type: 'error',
message: `${t('app.appDeleteFailed')}${
'message' in e ? `: ${e.message}` : ''
}`,
})
2023-05-15 08:51:32 +08:00
}
setShowConfirmDelete(false)
}, [app.id])
const getAppDetail = async () => {
setDetailState({ loading: true })
const [err, res] = await asyncRunSafe<App>(
fetchAppDetail({ url: '/apps', id: app.id }) as Promise<App>,
)
if (!err) {
setDetailState({ loading: false, detail: res })
setShowSettingsModal(true)
}
else { setDetailState({ loading: false }) }
}
const onSaveSiteConfig = useCallback(
2023-08-28 19:48:53 +08:00
async (params: ConfigParams) => {
const [err] = await asyncRunSafe<App>(
updateAppSiteConfig({
url: `/apps/${app.id}/site`,
body: params,
}) as Promise<App>,
)
if (!err) {
notify({
type: 'success',
message: t('common.actionMsg.modifiedSuccessfully'),
})
if (onRefresh)
onRefresh()
mutateApps()
}
else {
notify({
type: 'error',
message: t('common.actionMsg.modifiedUnsuccessfully'),
})
}
},
[app.id],
)
const Operations = (props: any) => {
2023-08-28 19:48:53 +08:00
const onClickSettings = async (e: React.MouseEvent<HTMLButtonElement>) => {
props?.onClose()
e.preventDefault()
await getAppDetail()
}
2023-08-28 19:48:53 +08:00
const onClickDelete = async (e: React.MouseEvent<HTMLDivElement>) => {
props?.onClose()
e.preventDefault()
setShowConfirmDelete(true)
}
return (
<div className="w-full py-1">
<button className={s.actionItem} onClick={onClickSettings} disabled={detailState.loading}>
<span className={s.actionName}>{t('common.operation.settings')}</span>
</button>
<Divider className="!my-1" />
<div
className={cn(s.actionItem, s.deleteActionItem, 'group')}
onClick={onClickDelete}
>
<span className={cn(s.actionName, 'group-hover:text-red-500')}>
{t('common.operation.delete')}
</span>
</div>
</div>
)
}
2023-05-15 08:51:32 +08:00
return (
<>
<Link
href={`/app/${app.id}/overview`}
className={style.listItem}
>
2023-05-15 08:51:32 +08:00
<div className={style.listItemTitle}>
<AppIcon
size="small"
icon={app.icon}
background={app.icon_background}
/>
2023-05-15 08:51:32 +08:00
<div className={style.listItemHeading}>
<div className={style.listItemHeadingContent}>{app.name}</div>
</div>
{isCurrentWorkspaceManager && <CustomPopover
htmlContent={<Operations />}
position="br"
trigger="click"
btnElement={<div className={cn(s.actionIcon, s.commonIcon)} />}
btnClassName={open =>
cn(
open ? '!bg-gray-100 !shadow-none' : '!bg-transparent',
style.actionIconWrapper,
)
}
className={'!w-[128px] h-fit !z-20'}
/>}
</div>
<div className={style.listItemDescription}>
{app.model_config?.pre_prompt}
2023-05-15 08:51:32 +08:00
</div>
<div className={style.listItemFooter}>
<AppModeLabel mode={app.mode} />
</div>
{showConfirmDelete && (
<Confirm
title={t('app.deleteAppConfirmTitle')}
content={t('app.deleteAppConfirmContent')}
isShow={showConfirmDelete}
onClose={() => setShowConfirmDelete(false)}
onConfirm={onConfirmDelete}
onCancel={() => setShowConfirmDelete(false)}
/>
)}
{showSettingsModal && detailState.detail && (
<SettingsModal
appInfo={detailState.detail}
isShow={showSettingsModal}
onClose={() => setShowSettingsModal(false)}
onSave={onSaveSiteConfig}
/>
)}
2023-05-15 08:51:32 +08:00
</Link>
</>
)
}
export default AppCard