app-info.tsx 19 KB

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