AppCard.tsx 5.8 KB

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