AppCard.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use client'
  2. import { useContext, useContextSelector } from 'use-context-selector'
  3. import Link from 'next/link'
  4. import { useCallback, useState } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import cn from 'classnames'
  7. import style from '../list.module.css'
  8. import AppModeLabel from './AppModeLabel'
  9. import s from './style.module.css'
  10. import SettingsModal from '@/app/components/app/overview/settings'
  11. import type { ConfigParams } from '@/app/components/app/overview/settings'
  12. import type { App } from '@/types/app'
  13. import Confirm from '@/app/components/base/confirm'
  14. import { ToastContext } from '@/app/components/base/toast'
  15. import { deleteApp, fetchAppDetail, updateAppSiteConfig } from '@/service/apps'
  16. import AppIcon from '@/app/components/base/app-icon'
  17. import AppsContext, { useAppContext } from '@/context/app-context'
  18. import CustomPopover from '@/app/components/base/popover'
  19. import Divider from '@/app/components/base/divider'
  20. import { asyncRunSafe } from '@/utils'
  21. export type AppCardProps = {
  22. app: App
  23. onRefresh?: () => void
  24. }
  25. const AppCard = ({ app, onRefresh }: AppCardProps) => {
  26. const { t } = useTranslation()
  27. const { notify } = useContext(ToastContext)
  28. const { isCurrentWorkspaceManager } = useAppContext()
  29. const mutateApps = useContextSelector(
  30. AppsContext,
  31. state => state.mutateApps,
  32. )
  33. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  34. const [showSettingsModal, setShowSettingsModal] = useState(false)
  35. const [detailState, setDetailState] = useState<{
  36. loading: boolean
  37. detail?: App
  38. }>({ loading: false })
  39. const onConfirmDelete = useCallback(async () => {
  40. try {
  41. await deleteApp(app.id)
  42. notify({ type: 'success', message: t('app.appDeleted') })
  43. if (onRefresh)
  44. onRefresh()
  45. mutateApps()
  46. }
  47. catch (e: any) {
  48. notify({
  49. type: 'error',
  50. message: `${t('app.appDeleteFailed')}${
  51. 'message' in e ? `: ${e.message}` : ''
  52. }`,
  53. })
  54. }
  55. setShowConfirmDelete(false)
  56. }, [app.id])
  57. const getAppDetail = async () => {
  58. setDetailState({ loading: true })
  59. const [err, res] = await asyncRunSafe<App>(
  60. fetchAppDetail({ url: '/apps', id: app.id }) as Promise<App>,
  61. )
  62. if (!err) {
  63. setDetailState({ loading: false, detail: res })
  64. setShowSettingsModal(true)
  65. }
  66. else { setDetailState({ loading: false }) }
  67. }
  68. const onSaveSiteConfig = useCallback(
  69. async (params: ConfigParams) => {
  70. const [err] = await asyncRunSafe<App>(
  71. updateAppSiteConfig({
  72. url: `/apps/${app.id}/site`,
  73. body: params,
  74. }) as Promise<App>,
  75. )
  76. if (!err) {
  77. notify({
  78. type: 'success',
  79. message: t('common.actionMsg.modifiedSuccessfully'),
  80. })
  81. if (onRefresh)
  82. onRefresh()
  83. mutateApps()
  84. }
  85. else {
  86. notify({
  87. type: 'error',
  88. message: t('common.actionMsg.modificationFailed'),
  89. })
  90. }
  91. },
  92. [app.id],
  93. )
  94. const Operations = (props: any) => {
  95. const onClickSettings = async (e: React.MouseEvent<HTMLButtonElement>) => {
  96. props?.onClose()
  97. e.preventDefault()
  98. await getAppDetail()
  99. }
  100. const onClickDelete = async (e: React.MouseEvent<HTMLDivElement>) => {
  101. props?.onClose()
  102. e.preventDefault()
  103. setShowConfirmDelete(true)
  104. }
  105. return (
  106. <div className="w-full py-1">
  107. <button className={s.actionItem} onClick={onClickSettings} disabled={detailState.loading}>
  108. <span className={s.actionName}>{t('common.operation.settings')}</span>
  109. </button>
  110. <Divider className="!my-1" />
  111. <div
  112. className={cn(s.actionItem, s.deleteActionItem, 'group')}
  113. onClick={onClickDelete}
  114. >
  115. <span className={cn(s.actionName, 'group-hover:text-red-500')}>
  116. {t('common.operation.delete')}
  117. </span>
  118. </div>
  119. </div>
  120. )
  121. }
  122. return (
  123. <>
  124. <Link
  125. href={`/app/${app.id}/overview`}
  126. className={style.listItem}
  127. >
  128. <div className={style.listItemTitle}>
  129. <AppIcon
  130. size="small"
  131. icon={app.icon}
  132. background={app.icon_background}
  133. />
  134. <div className={style.listItemHeading}>
  135. <div className={style.listItemHeadingContent}>{app.name}</div>
  136. </div>
  137. {isCurrentWorkspaceManager && <CustomPopover
  138. htmlContent={<Operations />}
  139. position="br"
  140. trigger="click"
  141. btnElement={<div className={cn(s.actionIcon, s.commonIcon)} />}
  142. btnClassName={open =>
  143. cn(
  144. open ? '!bg-gray-100 !shadow-none' : '!bg-transparent',
  145. style.actionIconWrapper,
  146. )
  147. }
  148. className={'!w-[128px] h-fit !z-20'}
  149. />}
  150. </div>
  151. <div className={style.listItemDescription}>
  152. {app.model_config?.pre_prompt}
  153. </div>
  154. <div className={style.listItemFooter}>
  155. <AppModeLabel mode={app.mode} />
  156. </div>
  157. {showConfirmDelete && (
  158. <Confirm
  159. title={t('app.deleteAppConfirmTitle')}
  160. content={t('app.deleteAppConfirmContent')}
  161. isShow={showConfirmDelete}
  162. onClose={() => setShowConfirmDelete(false)}
  163. onConfirm={onConfirmDelete}
  164. onCancel={() => setShowConfirmDelete(false)}
  165. />
  166. )}
  167. {showSettingsModal && detailState.detail && (
  168. <SettingsModal
  169. appInfo={detailState.detail}
  170. isShow={showSettingsModal}
  171. onClose={() => setShowSettingsModal(false)}
  172. onSave={onSaveSiteConfig}
  173. />
  174. )}
  175. </Link>
  176. </>
  177. )
  178. }
  179. export default AppCard