app-info.tsx 14 KB

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