app-info.tsx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import { useTranslation } from 'react-i18next'
  2. import { useRouter } from 'next/navigation'
  3. import { useContext, useContextSelector } from 'use-context-selector'
  4. import { RiArrowDownSLine } from '@remixicon/react'
  5. import React, { useCallback, useState } from 'react'
  6. import AppIcon from '../base/app-icon'
  7. import SwitchAppModal from '../app/switch-app-modal'
  8. import s from './style.module.css'
  9. import cn from '@/utils/classnames'
  10. import {
  11. PortalToFollowElem,
  12. PortalToFollowElemContent,
  13. PortalToFollowElemTrigger,
  14. } from '@/app/components/base/portal-to-follow-elem'
  15. import Divider from '@/app/components/base/divider'
  16. import Confirm from '@/app/components/base/confirm'
  17. import { useStore as useAppStore } from '@/app/components/app/store'
  18. import { ToastContext } from '@/app/components/base/toast'
  19. import AppsContext, { useAppContext } from '@/context/app-context'
  20. import { useProviderContext } from '@/context/provider-context'
  21. import { copyApp, deleteApp, exportAppConfig, updateAppInfo } from '@/service/apps'
  22. import DuplicateAppModal from '@/app/components/app/duplicate-modal'
  23. import type { DuplicateAppModalProps } from '@/app/components/app/duplicate-modal'
  24. import CreateAppModal from '@/app/components/explore/create-app-modal'
  25. import { AiText, ChatBot, CuteRobote } from '@/app/components/base/icons/src/vender/solid/communication'
  26. import { Route } from '@/app/components/base/icons/src/vender/solid/mapsAndTravel'
  27. import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal'
  28. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  29. import { getRedirection } from '@/utils/app-redirection'
  30. import UpdateDSLModal from '@/app/components/workflow/update-dsl-modal'
  31. import type { EnvironmentVariable } from '@/app/components/workflow/types'
  32. import DSLExportConfirmModal from '@/app/components/workflow/dsl-export-confirm-modal'
  33. import { fetchWorkflowDraft } from '@/service/workflow'
  34. export type IAppInfoProps = {
  35. expand: boolean
  36. }
  37. const AppInfo = ({ expand }: IAppInfoProps) => {
  38. const { t } = useTranslation()
  39. const { notify } = useContext(ToastContext)
  40. const { replace } = useRouter()
  41. const { onPlanInfoChanged } = useProviderContext()
  42. const appDetail = useAppStore(state => state.appDetail)
  43. const setAppDetail = useAppStore(state => state.setAppDetail)
  44. const [open, setOpen] = useState(false)
  45. const [showEditModal, setShowEditModal] = useState(false)
  46. const [showDuplicateModal, setShowDuplicateModal] = useState(false)
  47. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  48. const [showSwitchTip, setShowSwitchTip] = useState<string>('')
  49. const [showSwitchModal, setShowSwitchModal] = useState<boolean>(false)
  50. const [showImportDSLModal, setShowImportDSLModal] = useState<boolean>(false)
  51. const [secretEnvList, setSecretEnvList] = useState<EnvironmentVariable[]>([])
  52. const mutateApps = useContextSelector(
  53. AppsContext,
  54. state => state.mutateApps,
  55. )
  56. const onEdit: CreateAppModalProps['onConfirm'] = useCallback(async ({
  57. name,
  58. icon,
  59. icon_background,
  60. description,
  61. }) => {
  62. if (!appDetail)
  63. return
  64. try {
  65. const app = await updateAppInfo({
  66. appID: appDetail.id,
  67. name,
  68. icon,
  69. icon_background,
  70. description,
  71. })
  72. setShowEditModal(false)
  73. notify({
  74. type: 'success',
  75. message: t('app.editDone'),
  76. })
  77. setAppDetail(app)
  78. mutateApps()
  79. }
  80. catch (e) {
  81. notify({ type: 'error', message: t('app.editFailed') })
  82. }
  83. }, [appDetail, mutateApps, notify, setAppDetail, t])
  84. const onCopy: DuplicateAppModalProps['onConfirm'] = async ({ name, icon, icon_background }) => {
  85. if (!appDetail)
  86. return
  87. try {
  88. const newApp = await copyApp({
  89. appID: appDetail.id,
  90. name,
  91. icon,
  92. icon_background,
  93. mode: appDetail.mode,
  94. })
  95. setShowDuplicateModal(false)
  96. notify({
  97. type: 'success',
  98. message: t('app.newApp.appCreated'),
  99. })
  100. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  101. mutateApps()
  102. onPlanInfoChanged()
  103. getRedirection(true, newApp, replace)
  104. }
  105. catch (e) {
  106. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  107. }
  108. }
  109. const onExport = async (include = false) => {
  110. if (!appDetail)
  111. return
  112. try {
  113. const { data } = await exportAppConfig({
  114. appID: appDetail.id,
  115. include,
  116. })
  117. const a = document.createElement('a')
  118. const file = new Blob([data], { type: 'application/yaml' })
  119. a.href = URL.createObjectURL(file)
  120. a.download = `${appDetail.name}.yml`
  121. a.click()
  122. }
  123. catch (e) {
  124. notify({ type: 'error', message: t('app.exportFailed') })
  125. }
  126. }
  127. const exportCheck = async () => {
  128. if (!appDetail)
  129. return
  130. if (appDetail.mode !== 'workflow' && appDetail.mode !== 'advanced-chat') {
  131. onExport()
  132. return
  133. }
  134. try {
  135. const workflowDraft = await fetchWorkflowDraft(`/apps/${appDetail.id}/workflows/draft`)
  136. const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret')
  137. if (list.length === 0) {
  138. onExport()
  139. return
  140. }
  141. setSecretEnvList(list)
  142. }
  143. catch (e) {
  144. notify({ type: 'error', message: t('app.exportFailed') })
  145. }
  146. }
  147. const onConfirmDelete = useCallback(async () => {
  148. if (!appDetail)
  149. return
  150. try {
  151. await deleteApp(appDetail.id)
  152. notify({ type: 'success', message: t('app.appDeleted') })
  153. mutateApps()
  154. onPlanInfoChanged()
  155. setAppDetail()
  156. replace('/apps')
  157. }
  158. catch (e: any) {
  159. notify({
  160. type: 'error',
  161. message: `${t('app.appDeleteFailed')}${'message' in e ? `: ${e.message}` : ''}`,
  162. })
  163. }
  164. setShowConfirmDelete(false)
  165. }, [appDetail, mutateApps, notify, onPlanInfoChanged, replace, t])
  166. const { isCurrentWorkspaceEditor } = useAppContext()
  167. if (!appDetail)
  168. return null
  169. return (
  170. <PortalToFollowElem
  171. open={open}
  172. onOpenChange={setOpen}
  173. placement='bottom-start'
  174. offset={4}
  175. >
  176. <div className='relative'>
  177. <PortalToFollowElemTrigger
  178. onClick={() => {
  179. if (isCurrentWorkspaceEditor)
  180. setOpen(v => !v)
  181. }}
  182. className='block'
  183. >
  184. <div className={cn('flex p-1 rounded-lg', open && 'bg-gray-100', isCurrentWorkspaceEditor && 'hover:bg-gray-100 cursor-pointer')}>
  185. <div className='relative shrink-0 mr-2'>
  186. <AppIcon size={expand ? 'large' : 'small'} icon={appDetail.icon} background={appDetail.icon_background} />
  187. <span className={cn(
  188. 'absolute bottom-[-3px] right-[-3px] w-4 h-4 p-0.5 bg-white rounded border-[0.5px] border-[rgba(0,0,0,0.02)] shadow-sm',
  189. !expand && '!w-3.5 !h-3.5 !bottom-[-2px] !right-[-2px]',
  190. )}>
  191. {appDetail.mode === 'advanced-chat' && (
  192. <ChatBot className={cn('w-3 h-3 text-[#1570EF]', !expand && '!w-2.5 !h-2.5')} />
  193. )}
  194. {appDetail.mode === 'agent-chat' && (
  195. <CuteRobote className={cn('w-3 h-3 text-indigo-600', !expand && '!w-2.5 !h-2.5')} />
  196. )}
  197. {appDetail.mode === 'chat' && (
  198. <ChatBot className={cn('w-3 h-3 text-[#1570EF]', !expand && '!w-2.5 !h-2.5')} />
  199. )}
  200. {appDetail.mode === 'completion' && (
  201. <AiText className={cn('w-3 h-3 text-[#0E9384]', !expand && '!w-2.5 !h-2.5')} />
  202. )}
  203. {appDetail.mode === 'workflow' && (
  204. <Route className={cn('w-3 h-3 text-[#f79009]', !expand && '!w-2.5 !h-2.5')} />
  205. )}
  206. </span>
  207. </div>
  208. {expand && (
  209. <div className="grow w-0">
  210. <div className='flex justify-between items-center text-sm leading-5 font-medium text-text-secondary'>
  211. <div className='truncate' title={appDetail.name}>{appDetail.name}</div>
  212. {isCurrentWorkspaceEditor && <RiArrowDownSLine className='shrink-0 ml-[2px] w-3 h-3 text-gray-500' />}
  213. </div>
  214. <div className='flex items-center text-[10px] leading-[18px] font-medium text-gray-500 gap-1'>
  215. {appDetail.mode === 'advanced-chat' && (
  216. <>
  217. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.chatbot').toUpperCase()}</div>
  218. <div title={t('app.newApp.advanced') || ''} className='px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.newApp.advanced').toUpperCase()}</div>
  219. </>
  220. )}
  221. {appDetail.mode === 'agent-chat' && (
  222. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.agent').toUpperCase()}</div>
  223. )}
  224. {appDetail.mode === 'chat' && (
  225. <>
  226. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.chatbot').toUpperCase()}</div>
  227. <div title={t('app.newApp.basic') || ''} className='px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{(t('app.newApp.basic').toUpperCase())}</div>
  228. </>
  229. )}
  230. {appDetail.mode === 'completion' && (
  231. <>
  232. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.completion').toUpperCase()}</div>
  233. <div title={t('app.newApp.basic') || ''} className='px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{(t('app.newApp.basic').toUpperCase())}</div>
  234. </>
  235. )}
  236. {appDetail.mode === 'workflow' && (
  237. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.workflow').toUpperCase()}</div>
  238. )}
  239. </div>
  240. </div>
  241. )}
  242. </div>
  243. </PortalToFollowElemTrigger>
  244. <PortalToFollowElemContent className='z-[1002]'>
  245. <div className='relative w-[320px] bg-white rounded-2xl shadow-xl'>
  246. {/* header */}
  247. <div className={cn('flex pl-4 pt-3 pr-3', !appDetail.description && 'pb-2')}>
  248. <div className='relative shrink-0 mr-2'>
  249. <AppIcon size="large" icon={appDetail.icon} background={appDetail.icon_background} />
  250. <span className='absolute bottom-[-3px] right-[-3px] w-4 h-4 p-0.5 bg-white rounded border-[0.5px] border-[rgba(0,0,0,0.02)] shadow-sm'>
  251. {appDetail.mode === 'advanced-chat' && (
  252. <ChatBot className='w-3 h-3 text-[#1570EF]' />
  253. )}
  254. {appDetail.mode === 'agent-chat' && (
  255. <CuteRobote className='w-3 h-3 text-indigo-600' />
  256. )}
  257. {appDetail.mode === 'chat' && (
  258. <ChatBot className='w-3 h-3 text-[#1570EF]' />
  259. )}
  260. {appDetail.mode === 'completion' && (
  261. <AiText className='w-3 h-3 text-[#0E9384]' />
  262. )}
  263. {appDetail.mode === 'workflow' && (
  264. <Route className='w-3 h-3 text-[#f79009]' />
  265. )}
  266. </span>
  267. </div>
  268. <div className='grow w-0'>
  269. <div title={appDetail.name} className='flex justify-between items-center text-sm leading-5 font-medium text-gray-900 truncate'>{appDetail.name}</div>
  270. <div className='flex items-center text-[10px] leading-[18px] font-medium text-gray-500 gap-1'>
  271. {appDetail.mode === 'advanced-chat' && (
  272. <>
  273. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.chatbot').toUpperCase()}</div>
  274. <div title={t('app.newApp.advanced') || ''} className='px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.newApp.advanced').toUpperCase()}</div>
  275. </>
  276. )}
  277. {appDetail.mode === 'agent-chat' && (
  278. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.agent').toUpperCase()}</div>
  279. )}
  280. {appDetail.mode === 'chat' && (
  281. <>
  282. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.chatbot').toUpperCase()}</div>
  283. <div title={t('app.newApp.basic') || ''} className='px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{(t('app.newApp.basic').toUpperCase())}</div>
  284. </>
  285. )}
  286. {appDetail.mode === 'completion' && (
  287. <>
  288. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.completion').toUpperCase()}</div>
  289. <div title={t('app.newApp.basic') || ''} className='px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{(t('app.newApp.basic').toUpperCase())}</div>
  290. </>
  291. )}
  292. {appDetail.mode === 'workflow' && (
  293. <div className='shrink-0 px-1 border bg-white border-[rgba(0,0,0,0.08)] rounded-[5px] truncate'>{t('app.types.workflow').toUpperCase()}</div>
  294. )}
  295. </div>
  296. </div>
  297. </div>
  298. {/* desscription */}
  299. {appDetail.description && (
  300. <div className='px-4 py-2 text-gray-500 text-xs leading-[18px]'>{appDetail.description}</div>
  301. )}
  302. {/* operations */}
  303. <Divider className="!my-1" />
  304. <div className="w-full py-1">
  305. <div className='h-9 py-2 px-3 mx-1 flex items-center hover:bg-gray-50 rounded-lg cursor-pointer' onClick={() => {
  306. setOpen(false)
  307. setShowEditModal(true)
  308. }}>
  309. <span className='text-gray-700 text-sm leading-5'>{t('app.editApp')}</span>
  310. </div>
  311. <div className='h-9 py-2 px-3 mx-1 flex items-center hover:bg-gray-50 rounded-lg cursor-pointer' onClick={() => {
  312. setOpen(false)
  313. setShowDuplicateModal(true)
  314. }}>
  315. <span className='text-gray-700 text-sm leading-5'>{t('app.duplicate')}</span>
  316. </div>
  317. {(appDetail.mode === 'completion' || appDetail.mode === 'chat') && (
  318. <>
  319. <Divider className="!my-1" />
  320. <div
  321. className='h-9 py-2 px-3 mx-1 flex items-center hover:bg-gray-50 rounded-lg cursor-pointer'
  322. onMouseEnter={() => setShowSwitchTip(appDetail.mode)}
  323. onMouseLeave={() => setShowSwitchTip('')}
  324. onClick={() => {
  325. setOpen(false)
  326. setShowSwitchModal(true)
  327. }}
  328. >
  329. <span className='text-gray-700 text-sm leading-5'>{t('app.switch')}</span>
  330. </div>
  331. </>
  332. )}
  333. <Divider className="!my-1" />
  334. <div className='h-9 py-2 px-3 mx-1 flex items-center hover:bg-gray-50 rounded-lg cursor-pointer' onClick={exportCheck}>
  335. <span className='text-gray-700 text-sm leading-5'>{t('app.export')}</span>
  336. </div>
  337. {
  338. (appDetail.mode === 'advanced-chat' || appDetail.mode === 'workflow') && (
  339. <div
  340. className='h-9 py-2 px-3 mx-1 flex items-center hover:bg-gray-50 rounded-lg cursor-pointer'
  341. onClick={() => {
  342. setOpen(false)
  343. setShowImportDSLModal(true)
  344. }}>
  345. <span className='text-gray-700 text-sm leading-5'>{t('workflow.common.importDSL')}</span>
  346. </div>
  347. )
  348. }
  349. <Divider className="!my-1" />
  350. <div className='group h-9 py-2 px-3 mx-1 flex items-center hover:bg-red-50 rounded-lg cursor-pointer' onClick={() => {
  351. setOpen(false)
  352. setShowConfirmDelete(true)
  353. }}>
  354. <span className='text-gray-700 text-sm leading-5 group-hover:text-red-500'>
  355. {t('common.operation.delete')}
  356. </span>
  357. </div>
  358. </div>
  359. {/* switch tip */}
  360. <div
  361. className={cn(
  362. 'hidden absolute left-[324px] top-0 w-[376px] rounded-xl bg-white border-[0.5px] border-[rgba(0,0,0,0.05)] shadow-lg',
  363. showSwitchTip && '!block',
  364. )}
  365. >
  366. <div className={cn(
  367. 'w-full h-[256px] bg-center bg-no-repeat bg-contain rounded-xl',
  368. showSwitchTip === 'chat' && s.expertPic,
  369. showSwitchTip === 'completion' && s.completionPic,
  370. )} />
  371. <div className='px-4 pb-2'>
  372. <div className='flex items-center gap-1 text-gray-700 text-md leading-6 font-semibold'>
  373. {showSwitchTip === 'chat' ? t('app.newApp.advanced') : t('app.types.workflow')}
  374. <span className='px-1 rounded-[5px] bg-white border border-black/8 text-gray-500 text-[10px] leading-[18px] font-medium'>BETA</span>
  375. </div>
  376. <div className='text-orange-500 text-xs leading-[18px] font-medium'>{t('app.newApp.advancedFor').toLocaleUpperCase()}</div>
  377. <div className='mt-1 text-gray-500 text-sm leading-5'>{t('app.newApp.advancedDescription')}</div>
  378. </div>
  379. </div>
  380. </div>
  381. </PortalToFollowElemContent>
  382. {showSwitchModal && (
  383. <SwitchAppModal
  384. inAppDetail
  385. show={showSwitchModal}
  386. appDetail={appDetail}
  387. onClose={() => setShowSwitchModal(false)}
  388. onSuccess={() => setShowSwitchModal(false)}
  389. />
  390. )}
  391. {showEditModal && (
  392. <CreateAppModal
  393. isEditModal
  394. appIcon={appDetail.icon}
  395. appIconBackground={appDetail.icon_background}
  396. appName={appDetail.name}
  397. appDescription={appDetail.description}
  398. show={showEditModal}
  399. onConfirm={onEdit}
  400. onHide={() => setShowEditModal(false)}
  401. />
  402. )}
  403. {showDuplicateModal && (
  404. <DuplicateAppModal
  405. appName={appDetail.name}
  406. icon={appDetail.icon}
  407. icon_background={appDetail.icon_background}
  408. show={showDuplicateModal}
  409. onConfirm={onCopy}
  410. onHide={() => setShowDuplicateModal(false)}
  411. />
  412. )}
  413. {showConfirmDelete && (
  414. <Confirm
  415. title={t('app.deleteAppConfirmTitle')}
  416. content={t('app.deleteAppConfirmContent')}
  417. isShow={showConfirmDelete}
  418. onConfirm={onConfirmDelete}
  419. onCancel={() => setShowConfirmDelete(false)}
  420. />
  421. )}
  422. {showImportDSLModal && (
  423. <UpdateDSLModal
  424. onCancel={() => setShowImportDSLModal(false)}
  425. onBackup={exportCheck}
  426. />
  427. )}
  428. {secretEnvList.length > 0 && (
  429. <DSLExportConfirmModal
  430. envList={secretEnvList}
  431. onConfirm={onExport}
  432. onClose={() => setSecretEnvList([])}
  433. />
  434. )}
  435. </div>
  436. </PortalToFollowElem>
  437. )
  438. }
  439. export default React.memo(AppInfo)