AppCard.tsx 6.2 KB

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