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