Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. import { useTranslation } from 'react-i18next'
  2. import { useRouter } from 'next/navigation'
  3. import { useContext } from 'use-context-selector'
  4. import React, { useCallback, useState } from 'react'
  5. import {
  6. RiDeleteBinLine,
  7. RiEditLine,
  8. RiEqualizer2Line,
  9. RiExchange2Line,
  10. RiFileCopy2Line,
  11. RiFileDownloadLine,
  12. RiFileUploadLine,
  13. } from '@remixicon/react'
  14. import AppIcon from '../base/app-icon'
  15. import { useStore as useAppStore } from '@/app/components/app/store'
  16. import { ToastContext } from '@/app/components/base/toast'
  17. import { useAppContext } from '@/context/app-context'
  18. import { useProviderContext } from '@/context/provider-context'
  19. import { copyApp, deleteApp, exportAppConfig, updateAppInfo } from '@/service/apps'
  20. import type { DuplicateAppModalProps } from '@/app/components/app/duplicate-modal'
  21. import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal'
  22. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  23. import { getRedirection } from '@/utils/app-redirection'
  24. import type { EnvironmentVariable } from '@/app/components/workflow/types'
  25. import { fetchWorkflowDraft } from '@/service/workflow'
  26. import ContentDialog from '@/app/components/base/content-dialog'
  27. import Button from '@/app/components/base/button'
  28. import CardView from '@/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/card-view'
  29. import Divider from '../base/divider'
  30. import type { Operation } from './app-operations'
  31. import AppOperations from './app-operations'
  32. import dynamic from 'next/dynamic'
  33. import cn from '@/utils/classnames'
  34. const SwitchAppModal = dynamic(() => import('@/app/components/app/switch-app-modal'), {
  35. ssr: false,
  36. })
  37. const CreateAppModal = dynamic(() => import('@/app/components/explore/create-app-modal'), {
  38. ssr: false,
  39. })
  40. const DuplicateAppModal = dynamic(() => import('@/app/components/app/duplicate-modal'), {
  41. ssr: false,
  42. })
  43. const Confirm = dynamic(() => import('@/app/components/base/confirm'), {
  44. ssr: false,
  45. })
  46. const UpdateDSLModal = dynamic(() => import('@/app/components/workflow/update-dsl-modal'), {
  47. ssr: false,
  48. })
  49. const DSLExportConfirmModal = dynamic(() => import('@/app/components/workflow/dsl-export-confirm-modal'), {
  50. ssr: false,
  51. })
  52. export type IAppInfoProps = {
  53. expand: boolean
  54. onlyShowDetail?: boolean
  55. openState?: boolean
  56. onDetailExpand?: (expand: boolean) => void
  57. }
  58. const AppInfo = ({ expand, onlyShowDetail = false, openState = false, onDetailExpand }: IAppInfoProps) => {
  59. const { t } = useTranslation()
  60. const { notify } = useContext(ToastContext)
  61. const { replace } = useRouter()
  62. const { onPlanInfoChanged } = useProviderContext()
  63. const appDetail = useAppStore(state => state.appDetail)
  64. const setAppDetail = useAppStore(state => state.setAppDetail)
  65. const [open, setOpen] = useState(openState)
  66. const [showEditModal, setShowEditModal] = useState(false)
  67. const [showDuplicateModal, setShowDuplicateModal] = useState(false)
  68. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  69. const [showSwitchModal, setShowSwitchModal] = useState<boolean>(false)
  70. const [showImportDSLModal, setShowImportDSLModal] = useState<boolean>(false)
  71. const [secretEnvList, setSecretEnvList] = useState<EnvironmentVariable[]>([])
  72. const [showExportWarning, setShowExportWarning] = useState(false)
  73. const onEdit: CreateAppModalProps['onConfirm'] = useCallback(async ({
  74. name,
  75. icon_type,
  76. icon,
  77. icon_background,
  78. description,
  79. use_icon_as_answer_icon,
  80. max_active_requests,
  81. }) => {
  82. if (!appDetail)
  83. return
  84. try {
  85. const app = await updateAppInfo({
  86. appID: appDetail.id,
  87. name,
  88. icon_type,
  89. icon,
  90. icon_background,
  91. description,
  92. use_icon_as_answer_icon,
  93. max_active_requests,
  94. })
  95. setShowEditModal(false)
  96. notify({
  97. type: 'success',
  98. message: t('app.editDone'),
  99. })
  100. setAppDetail(app)
  101. }
  102. catch {
  103. notify({ type: 'error', message: t('app.editFailed') })
  104. }
  105. }, [appDetail, notify, setAppDetail, t])
  106. const onCopy: DuplicateAppModalProps['onConfirm'] = async ({ name, icon_type, icon, icon_background }) => {
  107. if (!appDetail)
  108. return
  109. try {
  110. const newApp = await copyApp({
  111. appID: appDetail.id,
  112. name,
  113. icon_type,
  114. icon,
  115. icon_background,
  116. mode: appDetail.mode,
  117. })
  118. setShowDuplicateModal(false)
  119. notify({
  120. type: 'success',
  121. message: t('app.newApp.appCreated'),
  122. })
  123. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  124. onPlanInfoChanged()
  125. getRedirection(true, newApp, replace)
  126. }
  127. catch {
  128. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  129. }
  130. }
  131. const onExport = async (include = false) => {
  132. if (!appDetail)
  133. return
  134. try {
  135. const { data } = await exportAppConfig({
  136. appID: appDetail.id,
  137. include,
  138. })
  139. const a = document.createElement('a')
  140. const file = new Blob([data], { type: 'application/yaml' })
  141. a.href = URL.createObjectURL(file)
  142. a.download = `${appDetail.name}.yml`
  143. a.click()
  144. }
  145. catch {
  146. notify({ type: 'error', message: t('app.exportFailed') })
  147. }
  148. }
  149. const exportCheck = async () => {
  150. if (!appDetail)
  151. return
  152. if (appDetail.mode !== 'workflow' && appDetail.mode !== 'advanced-chat') {
  153. onExport()
  154. return
  155. }
  156. setShowExportWarning(true)
  157. }
  158. const handleConfirmExport = async () => {
  159. if (!appDetail)
  160. return
  161. setShowExportWarning(false)
  162. try {
  163. const workflowDraft = await fetchWorkflowDraft(`/apps/${appDetail.id}/workflows/draft`)
  164. const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret')
  165. if (list.length === 0) {
  166. onExport()
  167. return
  168. }
  169. setSecretEnvList(list)
  170. }
  171. catch {
  172. notify({ type: 'error', message: t('app.exportFailed') })
  173. }
  174. }
  175. const onConfirmDelete = useCallback(async () => {
  176. if (!appDetail)
  177. return
  178. try {
  179. await deleteApp(appDetail.id)
  180. notify({ type: 'success', message: t('app.appDeleted') })
  181. onPlanInfoChanged()
  182. setAppDetail()
  183. replace('/apps')
  184. }
  185. catch (e: any) {
  186. notify({
  187. type: 'error',
  188. message: `${t('app.appDeleteFailed')}${'message' in e ? `: ${e.message}` : ''}`,
  189. })
  190. }
  191. setShowConfirmDelete(false)
  192. }, [appDetail, notify, onPlanInfoChanged, replace, setAppDetail, t])
  193. const { isCurrentWorkspaceEditor } = useAppContext()
  194. if (!appDetail)
  195. return null
  196. const operations = [
  197. {
  198. id: 'edit',
  199. title: t('app.editApp'),
  200. icon: <RiEditLine />,
  201. onClick: () => {
  202. setOpen(false)
  203. onDetailExpand?.(false)
  204. setShowEditModal(true)
  205. },
  206. },
  207. {
  208. id: 'duplicate',
  209. title: t('app.duplicate'),
  210. icon: <RiFileCopy2Line />,
  211. onClick: () => {
  212. setOpen(false)
  213. onDetailExpand?.(false)
  214. setShowDuplicateModal(true)
  215. },
  216. },
  217. {
  218. id: 'export',
  219. title: t('app.export'),
  220. icon: <RiFileDownloadLine />,
  221. onClick: exportCheck,
  222. },
  223. (appDetail.mode !== 'agent-chat' && (appDetail.mode === 'advanced-chat' || appDetail.mode === 'workflow')) ? {
  224. id: 'import',
  225. title: t('workflow.common.importDSL'),
  226. icon: <RiFileUploadLine />,
  227. onClick: () => {
  228. setOpen(false)
  229. onDetailExpand?.(false)
  230. setShowImportDSLModal(true)
  231. },
  232. } : undefined,
  233. (appDetail.mode !== 'agent-chat' && (appDetail.mode === 'completion' || appDetail.mode === 'chat')) ? {
  234. id: 'switch',
  235. title: t('app.switch'),
  236. icon: <RiExchange2Line />,
  237. onClick: () => {
  238. setOpen(false)
  239. onDetailExpand?.(false)
  240. setShowSwitchModal(true)
  241. },
  242. } : undefined,
  243. ].filter((op): op is Operation => Boolean(op))
  244. return (
  245. <div>
  246. {!onlyShowDetail && (
  247. <button
  248. onClick={() => {
  249. if (isCurrentWorkspaceEditor)
  250. setOpen(v => !v)
  251. }}
  252. className='block w-full'
  253. >
  254. <div className='flex flex-col gap-2 rounded-lg p-1 hover:bg-state-base-hover'>
  255. <div className='flex items-center gap-1'>
  256. <div className={cn(!expand && 'ml-1')}>
  257. <AppIcon
  258. size={expand ? 'large' : 'small'}
  259. iconType={appDetail.icon_type}
  260. icon={appDetail.icon}
  261. background={appDetail.icon_background}
  262. imageUrl={appDetail.icon_url}
  263. />
  264. </div>
  265. {expand && (
  266. <div className='ml-auto flex items-center justify-center rounded-md p-0.5'>
  267. <div className='flex h-5 w-5 items-center justify-center'>
  268. <RiEqualizer2Line className='h-4 w-4 text-text-tertiary' />
  269. </div>
  270. </div>
  271. )}
  272. </div>
  273. {!expand && (
  274. <div className='flex items-center justify-center'>
  275. <div className='flex h-5 w-5 items-center justify-center rounded-md p-0.5'>
  276. <RiEqualizer2Line className='h-4 w-4 text-text-tertiary' />
  277. </div>
  278. </div>
  279. )}
  280. {expand && (
  281. <div className='flex flex-col items-start gap-1'>
  282. <div className='flex w-full'>
  283. <div className='system-md-semibold truncate whitespace-nowrap text-text-secondary'>{appDetail.name}</div>
  284. </div>
  285. <div className='system-2xs-medium-uppercase whitespace-nowrap text-text-tertiary'>{appDetail.mode === 'advanced-chat' ? t('app.types.advanced') : appDetail.mode === 'agent-chat' ? t('app.types.agent') : appDetail.mode === 'chat' ? t('app.types.chatbot') : appDetail.mode === 'completion' ? t('app.types.completion') : t('app.types.workflow')}</div>
  286. </div>
  287. )}
  288. </div>
  289. </button>
  290. )}
  291. <ContentDialog
  292. show={onlyShowDetail ? openState : open}
  293. onClose={() => {
  294. setOpen(false)
  295. onDetailExpand?.(false)
  296. }}
  297. className='absolute bottom-2 left-2 top-2 flex w-[420px] flex-col rounded-2xl !p-0'
  298. >
  299. <div className='flex shrink-0 flex-col items-start justify-center gap-3 self-stretch p-4'>
  300. <div className='flex items-center gap-3 self-stretch'>
  301. <AppIcon
  302. size="large"
  303. iconType={appDetail.icon_type}
  304. icon={appDetail.icon}
  305. background={appDetail.icon_background}
  306. imageUrl={appDetail.icon_url}
  307. />
  308. <div className='flex w-full grow flex-col items-start justify-center'>
  309. <div className='system-md-semibold w-full truncate text-text-secondary'>{appDetail.name}</div>
  310. <div className='system-2xs-medium-uppercase text-text-tertiary'>{appDetail.mode === 'advanced-chat' ? t('app.types.advanced') : appDetail.mode === 'agent-chat' ? t('app.types.agent') : appDetail.mode === 'chat' ? t('app.types.chatbot') : appDetail.mode === 'completion' ? t('app.types.completion') : t('app.types.workflow')}</div>
  311. </div>
  312. </div>
  313. {/* description */}
  314. {appDetail.description && (
  315. <div className='system-xs-regular overflow-wrap-anywhere max-h-[105px] w-full max-w-full overflow-y-auto whitespace-normal break-words text-text-tertiary'>{appDetail.description}</div>
  316. )}
  317. {/* operations */}
  318. <AppOperations
  319. gap={4}
  320. operations={operations}
  321. />
  322. </div>
  323. <CardView
  324. appId={appDetail.id}
  325. isInPanel={true}
  326. className='flex flex-1 flex-col gap-2 overflow-auto px-2 py-1'
  327. />
  328. <Divider />
  329. <div className='flex min-h-fit shrink-0 flex-col items-start justify-center gap-3 self-stretch pb-2'>
  330. <Button
  331. size={'medium'}
  332. variant={'ghost'}
  333. className='gap-0.5'
  334. onClick={() => {
  335. setOpen(false)
  336. onDetailExpand?.(false)
  337. setShowConfirmDelete(true)
  338. }}
  339. >
  340. <RiDeleteBinLine className='h-4 w-4 text-text-tertiary' />
  341. <span className='system-sm-medium text-text-tertiary'>{t('common.operation.deleteApp')}</span>
  342. </Button>
  343. </div>
  344. </ContentDialog>
  345. {showSwitchModal && (
  346. <SwitchAppModal
  347. inAppDetail
  348. show={showSwitchModal}
  349. appDetail={appDetail}
  350. onClose={() => setShowSwitchModal(false)}
  351. onSuccess={() => setShowSwitchModal(false)}
  352. />
  353. )}
  354. {showEditModal && (
  355. <CreateAppModal
  356. isEditModal
  357. appName={appDetail.name}
  358. appIconType={appDetail.icon_type}
  359. appIcon={appDetail.icon}
  360. appIconBackground={appDetail.icon_background}
  361. appIconUrl={appDetail.icon_url}
  362. appDescription={appDetail.description}
  363. appMode={appDetail.mode}
  364. appUseIconAsAnswerIcon={appDetail.use_icon_as_answer_icon}
  365. max_active_requests={appDetail.max_active_requests ?? null}
  366. show={showEditModal}
  367. onConfirm={onEdit}
  368. onHide={() => setShowEditModal(false)}
  369. />
  370. )}
  371. {showDuplicateModal && (
  372. <DuplicateAppModal
  373. appName={appDetail.name}
  374. icon_type={appDetail.icon_type}
  375. icon={appDetail.icon}
  376. icon_background={appDetail.icon_background}
  377. icon_url={appDetail.icon_url}
  378. show={showDuplicateModal}
  379. onConfirm={onCopy}
  380. onHide={() => setShowDuplicateModal(false)}
  381. />
  382. )}
  383. {showConfirmDelete && (
  384. <Confirm
  385. title={t('app.deleteAppConfirmTitle')}
  386. content={t('app.deleteAppConfirmContent')}
  387. isShow={showConfirmDelete}
  388. onConfirm={onConfirmDelete}
  389. onCancel={() => setShowConfirmDelete(false)}
  390. />
  391. )}
  392. {showImportDSLModal && (
  393. <UpdateDSLModal
  394. onCancel={() => setShowImportDSLModal(false)}
  395. onBackup={exportCheck}
  396. />
  397. )}
  398. {secretEnvList.length > 0 && (
  399. <DSLExportConfirmModal
  400. envList={secretEnvList}
  401. onConfirm={onExport}
  402. onClose={() => setSecretEnvList([])}
  403. />
  404. )}
  405. {showExportWarning && (
  406. <Confirm
  407. type="info"
  408. isShow={showExportWarning}
  409. title={t('workflow.sidebar.exportWarning')}
  410. content={t('workflow.sidebar.exportWarningDesc')}
  411. onConfirm={handleConfirmExport}
  412. onCancel={() => setShowExportWarning(false)}
  413. />
  414. )}
  415. </div>
  416. )
  417. }
  418. export default React.memo(AppInfo)