選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

app-info.tsx 16KB

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