app-info.tsx 20 KB

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