Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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