AppCard.tsx 5.5 KB

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