You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app-info.tsx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import { useTranslation } from 'react-i18next'
  2. import { useRouter } from 'next/navigation'
  3. import { useContext, useContextSelector } from 'use-context-selector'
  4. import React, { useCallback, useState } from 'react'
  5. import {
  6. RiDeleteBinLine,
  7. RiEditLine,
  8. RiEqualizer2Line,
  9. RiExchange2Line,
  10. RiFileCopy2Line,
  11. RiFileDownloadLine,
  12. RiFileUploadLine,
  13. } from '@remixicon/react'
  14. import AppIcon from '../base/app-icon'
  15. import SwitchAppModal from '../app/switch-app-modal'
  16. import cn from '@/utils/classnames'
  17. import Confirm from '@/app/components/base/confirm'
  18. import { useStore as useAppStore } from '@/app/components/app/store'
  19. import { ToastContext } from '@/app/components/base/toast'
  20. import AppsContext, { useAppContext } from '@/context/app-context'
  21. import { useProviderContext } from '@/context/provider-context'
  22. import { copyApp, deleteApp, exportAppConfig, updateAppInfo } from '@/service/apps'
  23. import DuplicateAppModal from '@/app/components/app/duplicate-modal'
  24. import type { DuplicateAppModalProps } from '@/app/components/app/duplicate-modal'
  25. import CreateAppModal from '@/app/components/explore/create-app-modal'
  26. import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal'
  27. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  28. import { getRedirection } from '@/utils/app-redirection'
  29. import UpdateDSLModal from '@/app/components/workflow/update-dsl-modal'
  30. import type { EnvironmentVariable } from '@/app/components/workflow/types'
  31. import DSLExportConfirmModal from '@/app/components/workflow/dsl-export-confirm-modal'
  32. import { fetchWorkflowDraft } from '@/service/workflow'
  33. import ContentDialog from '@/app/components/base/content-dialog'
  34. import Button from '@/app/components/base/button'
  35. import CardView from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/cardView'
  36. import Divider from '../base/divider'
  37. import type { Operation } from './app-operations'
  38. import AppOperations from './app-operations'
  39. export type IAppInfoProps = {
  40. expand: boolean
  41. onlyShowDetail?: boolean
  42. openState?: boolean
  43. onDetailExpand?: (expand: boolean) => void
  44. }
  45. const AppInfo = ({ expand, onlyShowDetail = false, openState = false, onDetailExpand }: IAppInfoProps) => {
  46. const { t } = useTranslation()
  47. const { notify } = useContext(ToastContext)
  48. const { replace } = useRouter()
  49. const { onPlanInfoChanged } = useProviderContext()
  50. const appDetail = useAppStore(state => state.appDetail)
  51. const setAppDetail = useAppStore(state => state.setAppDetail)
  52. const [open, setOpen] = useState(openState)
  53. const [showEditModal, setShowEditModal] = useState(false)
  54. const [showDuplicateModal, setShowDuplicateModal] = useState(false)
  55. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  56. const [showSwitchModal, setShowSwitchModal] = useState<boolean>(false)
  57. const [showImportDSLModal, setShowImportDSLModal] = useState<boolean>(false)
  58. const [secretEnvList, setSecretEnvList] = useState<EnvironmentVariable[]>([])
  59. const mutateApps = useContextSelector(
  60. AppsContext,
  61. state => state.mutateApps,
  62. )
  63. const onEdit: CreateAppModalProps['onConfirm'] = useCallback(async ({
  64. name,
  65. icon_type,
  66. icon,
  67. icon_background,
  68. description,
  69. use_icon_as_answer_icon,
  70. }) => {
  71. if (!appDetail)
  72. return
  73. try {
  74. const app = await updateAppInfo({
  75. appID: appDetail.id,
  76. name,
  77. icon_type,
  78. icon,
  79. icon_background,
  80. description,
  81. use_icon_as_answer_icon,
  82. })
  83. setShowEditModal(false)
  84. notify({
  85. type: 'success',
  86. message: t('app.editDone'),
  87. })
  88. setAppDetail(app)
  89. mutateApps()
  90. }
  91. catch {
  92. notify({ type: 'error', message: t('app.editFailed') })
  93. }
  94. }, [appDetail, mutateApps, notify, setAppDetail, t])
  95. const onCopy: DuplicateAppModalProps['onConfirm'] = async ({ name, icon_type, icon, icon_background }) => {
  96. if (!appDetail)
  97. return
  98. try {
  99. const newApp = await copyApp({
  100. appID: appDetail.id,
  101. name,
  102. icon_type,
  103. icon,
  104. icon_background,
  105. mode: appDetail.mode,
  106. })
  107. setShowDuplicateModal(false)
  108. notify({
  109. type: 'success',
  110. message: t('app.newApp.appCreated'),
  111. })
  112. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  113. mutateApps()
  114. onPlanInfoChanged()
  115. getRedirection(true, newApp, replace)
  116. }
  117. catch {
  118. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  119. }
  120. }
  121. const onExport = async (include = false) => {
  122. if (!appDetail)
  123. return
  124. try {
  125. const { data } = await exportAppConfig({
  126. appID: appDetail.id,
  127. include,
  128. })
  129. const a = document.createElement('a')
  130. const file = new Blob([data], { type: 'application/yaml' })
  131. a.href = URL.createObjectURL(file)
  132. a.download = `${appDetail.name}.yml`
  133. a.click()
  134. }
  135. catch {
  136. notify({ type: 'error', message: t('app.exportFailed') })
  137. }
  138. }
  139. const exportCheck = async () => {
  140. if (!appDetail)
  141. return
  142. if (appDetail.mode !== 'workflow' && appDetail.mode !== 'advanced-chat') {
  143. onExport()
  144. return
  145. }
  146. try {
  147. const workflowDraft = await fetchWorkflowDraft(`/apps/${appDetail.id}/workflows/draft`)
  148. const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret')
  149. if (list.length === 0) {
  150. onExport()
  151. return
  152. }
  153. setSecretEnvList(list)
  154. }
  155. catch {
  156. notify({ type: 'error', message: t('app.exportFailed') })
  157. }
  158. }
  159. const onConfirmDelete = useCallback(async () => {
  160. if (!appDetail)
  161. return
  162. try {
  163. await deleteApp(appDetail.id)
  164. notify({ type: 'success', message: t('app.appDeleted') })
  165. mutateApps()
  166. onPlanInfoChanged()
  167. setAppDetail()
  168. replace('/apps')
  169. }
  170. catch (e: any) {
  171. notify({
  172. type: 'error',
  173. message: `${t('app.appDeleteFailed')}${'message' in e ? `: ${e.message}` : ''}`,
  174. })
  175. }
  176. setShowConfirmDelete(false)
  177. }, [appDetail, mutateApps, notify, onPlanInfoChanged, replace, setAppDetail, t])
  178. const { isCurrentWorkspaceEditor } = useAppContext()
  179. if (!appDetail)
  180. return null
  181. const operations = [
  182. {
  183. id: 'edit',
  184. title: t('app.editApp'),
  185. icon: <RiEditLine />,
  186. onClick: () => {
  187. setOpen(false)
  188. onDetailExpand?.(false)
  189. setShowEditModal(true)
  190. },
  191. },
  192. {
  193. id: 'duplicate',
  194. title: t('app.duplicate'),
  195. icon: <RiFileCopy2Line />,
  196. onClick: () => {
  197. setOpen(false)
  198. onDetailExpand?.(false)
  199. setShowDuplicateModal(true)
  200. },
  201. },
  202. {
  203. id: 'export',
  204. title: t('app.export'),
  205. icon: <RiFileDownloadLine />,
  206. onClick: exportCheck,
  207. },
  208. (appDetail.mode !== 'agent-chat' && (appDetail.mode === 'advanced-chat' || appDetail.mode === 'workflow')) ? {
  209. id: 'import',
  210. title: t('workflow.common.importDSL'),
  211. icon: <RiFileUploadLine />,
  212. onClick: () => {
  213. setOpen(false)
  214. onDetailExpand?.(false)
  215. setShowImportDSLModal(true)
  216. },
  217. } : undefined,
  218. (appDetail.mode !== 'agent-chat' && (appDetail.mode === 'completion' || appDetail.mode === 'chat')) ? {
  219. id: 'switch',
  220. title: t('app.switch'),
  221. icon: <RiExchange2Line />,
  222. onClick: () => {
  223. setOpen(false)
  224. onDetailExpand?.(false)
  225. setShowSwitchModal(true)
  226. },
  227. } : undefined,
  228. ].filter((op): op is Operation => Boolean(op))
  229. return (
  230. <div>
  231. {!onlyShowDetail && (
  232. <button
  233. onClick={() => {
  234. if (isCurrentWorkspaceEditor)
  235. setOpen(v => !v)
  236. }}
  237. className='block w-full'
  238. >
  239. <div className={cn('flex rounded-lg', expand ? 'flex-col gap-2 p-2 pb-2.5' : 'items-start justify-center gap-1 p-1', open && 'bg-state-base-hover', isCurrentWorkspaceEditor && 'cursor-pointer hover:bg-state-base-hover')}>
  240. <div className={`flex items-center self-stretch ${expand ? 'justify-between' : 'flex-col gap-1'}`}>
  241. <AppIcon
  242. size={expand ? 'large' : 'small'}
  243. iconType={appDetail.icon_type}
  244. icon={appDetail.icon}
  245. background={appDetail.icon_background}
  246. imageUrl={appDetail.icon_url}
  247. />
  248. <div className='flex items-center justify-center rounded-md p-0.5'>
  249. <div className='flex h-5 w-5 items-center justify-center'>
  250. <RiEqualizer2Line className='h-4 w-4 text-text-tertiary' />
  251. </div>
  252. </div>
  253. </div>
  254. {
  255. expand && (
  256. <div className='flex flex-col items-start gap-1'>
  257. <div className='flex w-full'>
  258. <div className='system-md-semibold truncate text-text-secondary'>{appDetail.name}</div>
  259. </div>
  260. <div className='system-2xs-medium-uppercase text-text-tertiary'>{appDetail.mode === 'advanced-chat' ? t('app.types.advanced') : appDetail.mode === 'agent-chat' ? t('app.types.agent') : appDetail.mode === 'chat' ? t('app.types.chatbot') : appDetail.mode === 'completion' ? t('app.types.completion') : t('app.types.workflow')}</div>
  261. </div>
  262. )
  263. }
  264. </div>
  265. </button>
  266. )}
  267. <ContentDialog
  268. show={onlyShowDetail ? openState : open}
  269. onClose={() => {
  270. setOpen(false)
  271. onDetailExpand?.(false)
  272. }}
  273. className='absolute bottom-2 left-2 top-2 flex w-[420px] flex-col rounded-2xl !p-0'
  274. >
  275. <div className='flex shrink-0 flex-col items-start justify-center gap-3 self-stretch p-4'>
  276. <div className='flex items-center gap-3 self-stretch'>
  277. <AppIcon
  278. size='large'
  279. iconType={appDetail.icon_type}
  280. icon={appDetail.icon}
  281. background={appDetail.icon_background}
  282. imageUrl={appDetail.icon_url}
  283. />
  284. <div className='flex w-full grow flex-col items-start justify-center'>
  285. <div className='system-md-semibold w-full truncate text-text-secondary'>{appDetail.name}</div>
  286. <div className='system-2xs-medium-uppercase text-text-tertiary'>{appDetail.mode === 'advanced-chat' ? t('app.types.advanced') : appDetail.mode === 'agent-chat' ? t('app.types.agent') : appDetail.mode === 'chat' ? t('app.types.chatbot') : appDetail.mode === 'completion' ? t('app.types.completion') : t('app.types.workflow')}</div>
  287. </div>
  288. </div>
  289. {/* description */}
  290. {appDetail.description && (
  291. <div className='system-xs-regular overflow-wrap-anywhere max-h-[105px] w-full max-w-full overflow-y-auto whitespace-normal break-words text-text-tertiary'>{appDetail.description}</div>
  292. )}
  293. {/* operations */}
  294. <AppOperations
  295. gap={4}
  296. operations={operations}
  297. />
  298. </div>
  299. <CardView
  300. appId={appDetail.id}
  301. isInPanel={true}
  302. className='flex flex-1 flex-col gap-2 overflow-auto px-2 py-1'
  303. />
  304. <Divider />
  305. <div className='flex min-h-fit shrink-0 flex-col items-start justify-center gap-3 self-stretch border-t-[0.5px] border-divider-subtle p-2'>
  306. <Button
  307. size={'medium'}
  308. variant={'ghost'}
  309. className='gap-0.5'
  310. onClick={() => {
  311. setOpen(false)
  312. onDetailExpand?.(false)
  313. setShowConfirmDelete(true)
  314. }}
  315. >
  316. <RiDeleteBinLine className='h-4 w-4 text-text-tertiary' />
  317. <span className='system-sm-medium text-text-tertiary'>{t('common.operation.deleteApp')}</span>
  318. </Button>
  319. </div>
  320. </ContentDialog>
  321. {showSwitchModal && (
  322. <SwitchAppModal
  323. inAppDetail
  324. show={showSwitchModal}
  325. appDetail={appDetail}
  326. onClose={() => setShowSwitchModal(false)}
  327. onSuccess={() => setShowSwitchModal(false)}
  328. />
  329. )}
  330. {showEditModal && (
  331. <CreateAppModal
  332. isEditModal
  333. appName={appDetail.name}
  334. appIconType={appDetail.icon_type}
  335. appIcon={appDetail.icon}
  336. appIconBackground={appDetail.icon_background}
  337. appIconUrl={appDetail.icon_url}
  338. appDescription={appDetail.description}
  339. appMode={appDetail.mode}
  340. appUseIconAsAnswerIcon={appDetail.use_icon_as_answer_icon}
  341. show={showEditModal}
  342. onConfirm={onEdit}
  343. onHide={() => setShowEditModal(false)}
  344. />
  345. )}
  346. {showDuplicateModal && (
  347. <DuplicateAppModal
  348. appName={appDetail.name}
  349. icon_type={appDetail.icon_type}
  350. icon={appDetail.icon}
  351. icon_background={appDetail.icon_background}
  352. icon_url={appDetail.icon_url}
  353. show={showDuplicateModal}
  354. onConfirm={onCopy}
  355. onHide={() => setShowDuplicateModal(false)}
  356. />
  357. )}
  358. {showConfirmDelete && (
  359. <Confirm
  360. title={t('app.deleteAppConfirmTitle')}
  361. content={t('app.deleteAppConfirmContent')}
  362. isShow={showConfirmDelete}
  363. onConfirm={onConfirmDelete}
  364. onCancel={() => setShowConfirmDelete(false)}
  365. />
  366. )}
  367. {showImportDSLModal && (
  368. <UpdateDSLModal
  369. onCancel={() => setShowImportDSLModal(false)}
  370. onBackup={exportCheck}
  371. />
  372. )}
  373. {secretEnvList.length > 0 && (
  374. <DSLExportConfirmModal
  375. envList={secretEnvList}
  376. onConfirm={onExport}
  377. onClose={() => setSecretEnvList([])}
  378. />
  379. )}
  380. </div>
  381. )
  382. }
  383. export default React.memo(AppInfo)