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 15KB

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