app-info.tsx 18 KB

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