AppCard.tsx 14 KB

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