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

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