AppCard.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. 'use client'
  2. import { useContext, useContextSelector } from 'use-context-selector'
  3. import { useRouter } from 'next/navigation'
  4. import { useCallback, useEffect, useState } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import { RiMoreFill } from '@remixicon/react'
  7. import s from './style.module.css'
  8. import cn from '@/utils/classnames'
  9. import type { App } from '@/types/app'
  10. import Confirm from '@/app/components/base/confirm'
  11. import { ToastContext } from '@/app/components/base/toast'
  12. import { copyApp, deleteApp, exportAppConfig, updateAppInfo } from '@/service/apps'
  13. import DuplicateAppModal from '@/app/components/app/duplicate-modal'
  14. import type { DuplicateAppModalProps } from '@/app/components/app/duplicate-modal'
  15. import AppIcon from '@/app/components/base/app-icon'
  16. import AppsContext, { useAppContext } from '@/context/app-context'
  17. import type { HtmlContentProps } from '@/app/components/base/popover'
  18. import CustomPopover from '@/app/components/base/popover'
  19. import Divider from '@/app/components/base/divider'
  20. import { getRedirection } from '@/utils/app-redirection'
  21. import { useProviderContext } from '@/context/provider-context'
  22. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  23. import { AiText, ChatBot, CuteRobote } from '@/app/components/base/icons/src/vender/solid/communication'
  24. import { Route } from '@/app/components/base/icons/src/vender/solid/mapsAndTravel'
  25. import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal'
  26. import EditAppModal from '@/app/components/explore/create-app-modal'
  27. import SwitchAppModal from '@/app/components/app/switch-app-modal'
  28. import type { Tag } from '@/app/components/base/tag-management/constant'
  29. import TagSelector from '@/app/components/base/tag-management/selector'
  30. import type { EnvironmentVariable } from '@/app/components/workflow/types'
  31. import DSLExportConfirmModal from '@/app/components/workflow/dsl-export-confirm-modal'
  32. import { fetchWorkflowDraft } from '@/service/workflow'
  33. export type AppCardProps = {
  34. app: App
  35. onRefresh?: () => void
  36. }
  37. const AppCard = ({ app, onRefresh }: AppCardProps) => {
  38. const { t } = useTranslation()
  39. const { notify } = useContext(ToastContext)
  40. const { isCurrentWorkspaceEditor } = useAppContext()
  41. const { onPlanInfoChanged } = useProviderContext()
  42. const { push } = useRouter()
  43. const mutateApps = useContextSelector(
  44. AppsContext,
  45. state => state.mutateApps,
  46. )
  47. const [showEditModal, setShowEditModal] = useState(false)
  48. const [showDuplicateModal, setShowDuplicateModal] = useState(false)
  49. const [showSwitchModal, setShowSwitchModal] = useState<boolean>(false)
  50. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  51. const [secretEnvList, setSecretEnvList] = useState<EnvironmentVariable[]>([])
  52. const onConfirmDelete = useCallback(async () => {
  53. try {
  54. await deleteApp(app.id)
  55. notify({ type: 'success', message: t('app.appDeleted') })
  56. if (onRefresh)
  57. onRefresh()
  58. mutateApps()
  59. onPlanInfoChanged()
  60. }
  61. catch (e: any) {
  62. notify({
  63. type: 'error',
  64. message: `${t('app.appDeleteFailed')}${'message' in e ? `: ${e.message}` : ''}`,
  65. })
  66. }
  67. setShowConfirmDelete(false)
  68. }, [app.id])
  69. const onEdit: CreateAppModalProps['onConfirm'] = useCallback(async ({
  70. name,
  71. icon,
  72. icon_background,
  73. description,
  74. }) => {
  75. try {
  76. await updateAppInfo({
  77. appID: app.id,
  78. name,
  79. icon,
  80. icon_background,
  81. description,
  82. })
  83. setShowEditModal(false)
  84. notify({
  85. type: 'success',
  86. message: t('app.editDone'),
  87. })
  88. if (onRefresh)
  89. onRefresh()
  90. mutateApps()
  91. }
  92. catch (e) {
  93. notify({ type: 'error', message: t('app.editFailed') })
  94. }
  95. }, [app.id, mutateApps, notify, onRefresh, t])
  96. const onCopy: DuplicateAppModalProps['onConfirm'] = async ({ name, icon, icon_background }) => {
  97. try {
  98. const newApp = await copyApp({
  99. appID: app.id,
  100. name,
  101. icon,
  102. icon_background,
  103. mode: app.mode,
  104. })
  105. setShowDuplicateModal(false)
  106. notify({
  107. type: 'success',
  108. message: t('app.newApp.appCreated'),
  109. })
  110. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  111. if (onRefresh)
  112. onRefresh()
  113. mutateApps()
  114. onPlanInfoChanged()
  115. getRedirection(isCurrentWorkspaceEditor, newApp, push)
  116. }
  117. catch (e) {
  118. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  119. }
  120. }
  121. const onExport = async (include = false) => {
  122. try {
  123. const { data } = await exportAppConfig({
  124. appID: app.id,
  125. include,
  126. })
  127. const a = document.createElement('a')
  128. const file = new Blob([data], { type: 'application/yaml' })
  129. a.href = URL.createObjectURL(file)
  130. a.download = `${app.name}.yml`
  131. a.click()
  132. }
  133. catch (e) {
  134. notify({ type: 'error', message: t('app.exportFailed') })
  135. }
  136. }
  137. const exportCheck = async () => {
  138. if (app.mode !== 'workflow' && app.mode !== 'advanced-chat') {
  139. onExport()
  140. return
  141. }
  142. try {
  143. const workflowDraft = await fetchWorkflowDraft(`/apps/${app.id}/workflows/draft`)
  144. const list = (workflowDraft.environment_variables || []).filter(env => env.value_type === 'secret')
  145. if (list.length === 0) {
  146. onExport()
  147. return
  148. }
  149. setSecretEnvList(list)
  150. }
  151. catch (e) {
  152. notify({ type: 'error', message: t('app.exportFailed') })
  153. }
  154. }
  155. const onSwitch = () => {
  156. if (onRefresh)
  157. onRefresh()
  158. mutateApps()
  159. setShowSwitchModal(false)
  160. }
  161. const Operations = (props: HtmlContentProps) => {
  162. const onMouseLeave = async () => {
  163. props.onClose?.()
  164. }
  165. const onClickSettings = async (e: React.MouseEvent<HTMLButtonElement>) => {
  166. e.stopPropagation()
  167. props.onClick?.()
  168. e.preventDefault()
  169. setShowEditModal(true)
  170. }
  171. const onClickDuplicate = async (e: React.MouseEvent<HTMLButtonElement>) => {
  172. e.stopPropagation()
  173. props.onClick?.()
  174. e.preventDefault()
  175. setShowDuplicateModal(true)
  176. }
  177. const onClickExport = async (e: React.MouseEvent<HTMLButtonElement>) => {
  178. e.stopPropagation()
  179. props.onClick?.()
  180. e.preventDefault()
  181. exportCheck()
  182. }
  183. const onClickSwitch = async (e: React.MouseEvent<HTMLDivElement>) => {
  184. e.stopPropagation()
  185. props.onClick?.()
  186. e.preventDefault()
  187. setShowSwitchModal(true)
  188. }
  189. const onClickDelete = async (e: React.MouseEvent<HTMLDivElement>) => {
  190. e.stopPropagation()
  191. props.onClick?.()
  192. e.preventDefault()
  193. setShowConfirmDelete(true)
  194. }
  195. return (
  196. <div className="relative w-full py-1" onMouseLeave={onMouseLeave}>
  197. <button className={s.actionItem} onClick={onClickSettings}>
  198. <span className={s.actionName}>{t('app.editApp')}</span>
  199. </button>
  200. <Divider className="!my-1" />
  201. <button className={s.actionItem} onClick={onClickDuplicate}>
  202. <span className={s.actionName}>{t('app.duplicate')}</span>
  203. </button>
  204. <button className={s.actionItem} onClick={onClickExport}>
  205. <span className={s.actionName}>{t('app.export')}</span>
  206. </button>
  207. {(app.mode === 'completion' || app.mode === 'chat') && (
  208. <>
  209. <Divider className="!my-1" />
  210. <div
  211. className='h-9 py-2 px-3 mx-1 flex items-center hover:bg-gray-50 rounded-lg cursor-pointer'
  212. onClick={onClickSwitch}
  213. >
  214. <span className='text-gray-700 text-sm leading-5'>{t('app.switch')}</span>
  215. </div>
  216. </>
  217. )}
  218. <Divider className="!my-1" />
  219. <div
  220. className={cn(s.actionItem, s.deleteActionItem, 'group')}
  221. onClick={onClickDelete}
  222. >
  223. <span className={cn(s.actionName, 'group-hover:text-red-500')}>
  224. {t('common.operation.delete')}
  225. </span>
  226. </div>
  227. </div>
  228. )
  229. }
  230. const [tags, setTags] = useState<Tag[]>(app.tags)
  231. useEffect(() => {
  232. setTags(app.tags)
  233. }, [app.tags])
  234. return (
  235. <>
  236. <div
  237. onClick={(e) => {
  238. e.preventDefault()
  239. getRedirection(isCurrentWorkspaceEditor, app, push)
  240. }}
  241. className='group flex col-span-1 bg-white border-2 border-solid border-transparent rounded-xl shadow-sm min-h-[160px] flex flex-col transition-all duration-200 ease-in-out cursor-pointer hover:shadow-lg'
  242. >
  243. <div className='flex pt-[14px] px-[14px] pb-3 h-[66px] items-center gap-3 grow-0 shrink-0'>
  244. <div className='relative shrink-0'>
  245. <AppIcon
  246. size="large"
  247. icon={app.icon}
  248. background={app.icon_background}
  249. />
  250. <span className='absolute bottom-[-3px] right-[-3px] w-4 h-4 p-0.5 bg-white rounded border-[0.5px] border-[rgba(0,0,0,0.02)] shadow-sm'>
  251. {app.mode === 'advanced-chat' && (
  252. <ChatBot className='w-3 h-3 text-[#1570EF]' />
  253. )}
  254. {app.mode === 'agent-chat' && (
  255. <CuteRobote className='w-3 h-3 text-indigo-600' />
  256. )}
  257. {app.mode === 'chat' && (
  258. <ChatBot className='w-3 h-3 text-[#1570EF]' />
  259. )}
  260. {app.mode === 'completion' && (
  261. <AiText className='w-3 h-3 text-[#0E9384]' />
  262. )}
  263. {app.mode === 'workflow' && (
  264. <Route className='w-3 h-3 text-[#f79009]' />
  265. )}
  266. </span>
  267. </div>
  268. <div className='grow w-0 py-[1px]'>
  269. <div className='flex items-center text-sm leading-5 font-semibold text-gray-800'>
  270. <div className='truncate' title={app.name}>{app.name}</div>
  271. </div>
  272. <div className='flex items-center text-[10px] leading-[18px] text-gray-500 font-medium'>
  273. {app.mode === 'advanced-chat' && <div className='truncate'>{t('app.types.chatbot').toUpperCase()}</div>}
  274. {app.mode === 'chat' && <div className='truncate'>{t('app.types.chatbot').toUpperCase()}</div>}
  275. {app.mode === 'agent-chat' && <div className='truncate'>{t('app.types.agent').toUpperCase()}</div>}
  276. {app.mode === 'workflow' && <div className='truncate'>{t('app.types.workflow').toUpperCase()}</div>}
  277. {app.mode === 'completion' && <div className='truncate'>{t('app.types.completion').toUpperCase()}</div>}
  278. </div>
  279. </div>
  280. </div>
  281. <div
  282. className={cn(
  283. 'grow mb-2 px-[14px] max-h-[72px] text-xs leading-normal text-gray-500 group-hover:line-clamp-2 group-hover:max-h-[36px]',
  284. tags.length ? 'line-clamp-2' : 'line-clamp-4',
  285. )}
  286. title={app.description}
  287. >
  288. {app.description}
  289. </div>
  290. <div className={cn(
  291. 'items-center shrink-0 mt-1 pt-1 pl-[14px] pr-[6px] pb-[6px] h-[42px]',
  292. tags.length ? 'flex' : '!hidden group-hover:!flex',
  293. )}>
  294. {isCurrentWorkspaceEditor && (
  295. <>
  296. <div className={cn('grow flex items-center gap-1 w-0')} onClick={(e) => {
  297. e.stopPropagation()
  298. e.preventDefault()
  299. }}>
  300. <div className={cn(
  301. 'group-hover:!block group-hover:!mr-0 mr-[41px] grow w-full',
  302. tags.length ? '!block' : '!hidden',
  303. )}>
  304. <TagSelector
  305. position='bl'
  306. type='app'
  307. targetID={app.id}
  308. value={tags.map(tag => tag.id)}
  309. selectedTags={tags}
  310. onCacheUpdate={setTags}
  311. onChange={onRefresh}
  312. />
  313. </div>
  314. </div>
  315. <div className='!hidden group-hover:!flex shrink-0 mx-1 w-[1px] h-[14px] bg-gray-200' />
  316. <div className='!hidden group-hover:!flex shrink-0'>
  317. <CustomPopover
  318. htmlContent={<Operations />}
  319. position="br"
  320. trigger="click"
  321. btnElement={
  322. <div
  323. className='flex items-center justify-center w-8 h-8 cursor-pointer rounded-md'
  324. >
  325. <RiMoreFill className='w-4 h-4 text-gray-700' />
  326. </div>
  327. }
  328. btnClassName={open =>
  329. cn(
  330. open ? '!bg-black/5 !shadow-none' : '!bg-transparent',
  331. 'h-8 w-8 !p-2 rounded-md border-none hover:!bg-black/5',
  332. )
  333. }
  334. popupClassName={
  335. (app.mode === 'completion' || app.mode === 'chat')
  336. ? '!w-[238px] translate-x-[-110px]'
  337. : ''
  338. }
  339. className={'!w-[128px] h-fit !z-20'}
  340. />
  341. </div>
  342. </>
  343. )}
  344. </div>
  345. </div>
  346. {showEditModal && (
  347. <EditAppModal
  348. isEditModal
  349. appIcon={app.icon}
  350. appIconBackground={app.icon_background}
  351. appName={app.name}
  352. appDescription={app.description}
  353. show={showEditModal}
  354. onConfirm={onEdit}
  355. onHide={() => setShowEditModal(false)}
  356. />
  357. )}
  358. {showDuplicateModal && (
  359. <DuplicateAppModal
  360. appName={app.name}
  361. icon={app.icon}
  362. icon_background={app.icon_background}
  363. show={showDuplicateModal}
  364. onConfirm={onCopy}
  365. onHide={() => setShowDuplicateModal(false)}
  366. />
  367. )}
  368. {showSwitchModal && (
  369. <SwitchAppModal
  370. show={showSwitchModal}
  371. appDetail={app}
  372. onClose={() => setShowSwitchModal(false)}
  373. onSuccess={onSwitch}
  374. />
  375. )}
  376. {showConfirmDelete && (
  377. <Confirm
  378. title={t('app.deleteAppConfirmTitle')}
  379. content={t('app.deleteAppConfirmContent')}
  380. isShow={showConfirmDelete}
  381. onConfirm={onConfirmDelete}
  382. onCancel={() => setShowConfirmDelete(false)}
  383. />
  384. )}
  385. {secretEnvList.length > 0 && (
  386. <DSLExportConfirmModal
  387. envList={secretEnvList}
  388. onConfirm={onExport}
  389. onClose={() => setSecretEnvList([])}
  390. />
  391. )}
  392. </>
  393. )
  394. }
  395. export default AppCard