app-info.tsx 20 KB

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