AppCard.tsx 6.0 KB

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