appCard.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. 'use client'
  2. import React, { useMemo, useState } from 'react'
  3. import { usePathname, useRouter } from 'next/navigation'
  4. import { useTranslation } from 'react-i18next'
  5. import {
  6. RiBookOpenLine,
  7. RiEqualizer2Line,
  8. RiExternalLinkLine,
  9. RiPaintBrushLine,
  10. RiWindowLine,
  11. } from '@remixicon/react'
  12. import SettingsModal from './settings'
  13. import EmbeddedModal from './embedded'
  14. import CustomizeModal from './customize'
  15. import style from './style.module.css'
  16. import type { ConfigParams } from './settings'
  17. import Tooltip from '@/app/components/base/tooltip'
  18. import AppBasic from '@/app/components/app-sidebar/basic'
  19. import { asyncRunSafe, randomString } from '@/utils'
  20. import Button from '@/app/components/base/button'
  21. import Switch from '@/app/components/base/switch'
  22. import Divider from '@/app/components/base/divider'
  23. import CopyFeedback from '@/app/components/base/copy-feedback'
  24. import Confirm from '@/app/components/base/confirm'
  25. import ShareQRCode from '@/app/components/base/qrcode'
  26. import SecretKeyButton from '@/app/components/develop/secret-key/secret-key-button'
  27. import type { AppDetailResponse } from '@/models/app'
  28. import { useAppContext } from '@/context/app-context'
  29. import type { AppSSO } from '@/types/app'
  30. import Indicator from '@/app/components/header/indicator'
  31. export type IAppCardProps = {
  32. className?: string
  33. appInfo: AppDetailResponse & Partial<AppSSO>
  34. isInPanel?: boolean
  35. cardType?: 'api' | 'webapp'
  36. customBgColor?: string
  37. onChangeStatus: (val: boolean) => Promise<void>
  38. onSaveSiteConfig?: (params: ConfigParams) => Promise<void>
  39. onGenerateCode?: () => Promise<void>
  40. }
  41. function AppCard({
  42. appInfo,
  43. isInPanel,
  44. cardType = 'webapp',
  45. customBgColor,
  46. onChangeStatus,
  47. onSaveSiteConfig,
  48. onGenerateCode,
  49. className,
  50. }: IAppCardProps) {
  51. const router = useRouter()
  52. const pathname = usePathname()
  53. const { isCurrentWorkspaceManager, isCurrentWorkspaceEditor } = useAppContext()
  54. const [showSettingsModal, setShowSettingsModal] = useState(false)
  55. const [showEmbedded, setShowEmbedded] = useState(false)
  56. const [showCustomizeModal, setShowCustomizeModal] = useState(false)
  57. const [genLoading, setGenLoading] = useState(false)
  58. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  59. const { t } = useTranslation()
  60. const OPERATIONS_MAP = useMemo(() => {
  61. const operationsMap = {
  62. webapp: [
  63. { opName: t('appOverview.overview.appInfo.launch'), opIcon: RiExternalLinkLine },
  64. ] as { opName: string; opIcon: any }[],
  65. api: [{ opName: t('appOverview.overview.apiInfo.doc'), opIcon: RiBookOpenLine }],
  66. app: [],
  67. }
  68. if (appInfo.mode !== 'completion' && appInfo.mode !== 'workflow')
  69. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.embedded.entry'), opIcon: RiWindowLine })
  70. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.customize.entry'), opIcon: RiPaintBrushLine })
  71. if (isCurrentWorkspaceEditor)
  72. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.settings.entry'), opIcon: RiEqualizer2Line })
  73. return operationsMap
  74. }, [isCurrentWorkspaceEditor, appInfo, t])
  75. const isApp = cardType === 'webapp'
  76. const basicName = isApp
  77. ? appInfo?.site?.title
  78. : t('appOverview.overview.apiInfo.title')
  79. const toggleDisabled = isApp ? !isCurrentWorkspaceEditor : !isCurrentWorkspaceManager
  80. const runningStatus = isApp ? appInfo.enable_site : appInfo.enable_api
  81. const { app_base_url, access_token } = appInfo.site ?? {}
  82. const appMode = (appInfo.mode !== 'completion' && appInfo.mode !== 'workflow') ? 'chat' : appInfo.mode
  83. const appUrl = `${app_base_url}/${appMode}/${access_token}`
  84. const apiUrl = appInfo?.api_base_url
  85. const genClickFuncByName = (opName: string) => {
  86. switch (opName) {
  87. case t('appOverview.overview.appInfo.launch'):
  88. return () => {
  89. window.open(appUrl, '_blank')
  90. }
  91. case t('appOverview.overview.appInfo.customize.entry'):
  92. return () => {
  93. setShowCustomizeModal(true)
  94. }
  95. case t('appOverview.overview.appInfo.settings.entry'):
  96. return () => {
  97. setShowSettingsModal(true)
  98. }
  99. case t('appOverview.overview.appInfo.embedded.entry'):
  100. return () => {
  101. setShowEmbedded(true)
  102. }
  103. default:
  104. // jump to page develop
  105. return () => {
  106. const pathSegments = pathname.split('/')
  107. pathSegments.pop()
  108. router.push(`${pathSegments.join('/')}/develop`)
  109. }
  110. }
  111. }
  112. const onGenCode = async () => {
  113. if (onGenerateCode) {
  114. setGenLoading(true)
  115. await asyncRunSafe(onGenerateCode())
  116. setGenLoading(false)
  117. }
  118. }
  119. return (
  120. <div
  121. className={
  122. `${isInPanel ? 'border-l-[0.5px] border-t' : 'shadow-xs border-[0.5px]'} rounded-xl border-effects-highlight w-full max-w-full ${className ?? ''}`}
  123. >
  124. <div className={`${customBgColor ?? 'bg-background-default'} rounded-xl`}>
  125. <div className='flex flex-col p-3 justify-center items-start gap-3 self-stretch border-b-[0.5px] border-divider-subtle w-full'>
  126. <div className='flex items-center gap-3 self-stretch w-full'>
  127. <AppBasic
  128. iconType={cardType}
  129. icon={appInfo.icon}
  130. icon_background={appInfo.icon_background}
  131. name={basicName}
  132. type={
  133. isApp
  134. ? t('appOverview.overview.appInfo.explanation')
  135. : t('appOverview.overview.apiInfo.explanation')
  136. }
  137. />
  138. <div className='flex items-center gap-1'>
  139. <Indicator color={runningStatus ? 'green' : 'yellow'} />
  140. <div className={`${runningStatus ? 'text-text-success' : 'text-text-warning'} system-xs-semibold-uppercase`}>
  141. {runningStatus
  142. ? t('appOverview.overview.status.running')
  143. : t('appOverview.overview.status.disable')}
  144. </div>
  145. </div>
  146. <Switch defaultValue={runningStatus} onChange={onChangeStatus} disabled={toggleDisabled} />
  147. </div>
  148. <div className='flex flex-col justify-center items-start self-stretch'>
  149. <div className="pb-1 system-xs-medium text-text-tertiary">
  150. {isApp
  151. ? t('appOverview.overview.appInfo.accessibleAddress')
  152. : t('appOverview.overview.apiInfo.accessibleAddress')}
  153. </div>
  154. <div className="w-full h-9 pl-2 p-1 bg-components-input-bg-normal rounded-lg items-center inline-flex gap-0.5">
  155. <div className="h-4 px-1 justify-start items-start gap-2 flex flex-1 min-w-0">
  156. <div className="text-text-secondary text-xs font-medium text-ellipsis overflow-hidden whitespace-nowrap">
  157. {isApp ? appUrl : apiUrl}
  158. </div>
  159. </div>
  160. <CopyFeedback
  161. content={isApp ? appUrl : apiUrl}
  162. className={'!size-6'}
  163. />
  164. {isApp && <ShareQRCode content={isApp ? appUrl : apiUrl} className='z-50 !size-6 hover:bg-state-base-hover rounded-md' selectorId={randomString(8)} />}
  165. {isApp && <Divider type="vertical" className="!h-3.5 shrink-0 !mx-0.5" />}
  166. {/* button copy link/ button regenerate */}
  167. {showConfirmDelete && (
  168. <Confirm
  169. type='warning'
  170. title={t('appOverview.overview.appInfo.regenerate')}
  171. content={t('appOverview.overview.appInfo.regenerateNotice')}
  172. isShow={showConfirmDelete}
  173. onConfirm={() => {
  174. onGenCode()
  175. setShowConfirmDelete(false)
  176. }}
  177. onCancel={() => setShowConfirmDelete(false)}
  178. />
  179. )}
  180. {isApp && isCurrentWorkspaceManager && (
  181. <Tooltip
  182. popupContent={t('appOverview.overview.appInfo.regenerate') || ''}
  183. >
  184. <div
  185. className="w-6 h-6 cursor-pointer hover:bg-state-base-hover rounded-md"
  186. onClick={() => setShowConfirmDelete(true)}
  187. >
  188. <div
  189. className={
  190. `w-full h-full ${style.refreshIcon} ${genLoading ? style.generateLogo : ''}`}
  191. ></div>
  192. </div>
  193. </Tooltip>
  194. )}
  195. </div>
  196. </div>
  197. </div>
  198. <div className={'flex p-3 items-center gap-1 self-stretch'}>
  199. {!isApp && <SecretKeyButton appId={appInfo.id} />}
  200. {OPERATIONS_MAP[cardType].map((op) => {
  201. const disabled
  202. = op.opName === t('appOverview.overview.appInfo.settings.entry')
  203. ? false
  204. : !runningStatus
  205. return (
  206. <Button
  207. className="mr-1 min-w-[88px]"
  208. size="small"
  209. variant={'ghost'}
  210. key={op.opName}
  211. onClick={genClickFuncByName(op.opName)}
  212. disabled={disabled}
  213. >
  214. <Tooltip
  215. popupContent={
  216. t('appOverview.overview.appInfo.preUseReminder') ?? ''
  217. }
  218. popupClassName={disabled ? 'mt-[-8px]' : '!hidden'}
  219. >
  220. <div className="flex items-center justify-center gap-[1px]">
  221. <op.opIcon className="h-3.5 w-3.5" />
  222. <div className={`${runningStatus ? 'text-text-tertiary' : 'text-components-button-ghost-text-disabled'} system-xs-medium px-[3px]`}>{op.opName}</div>
  223. </div>
  224. </Tooltip>
  225. </Button>
  226. )
  227. })}
  228. </div>
  229. </div>
  230. {isApp
  231. ? (
  232. <>
  233. <SettingsModal
  234. isChat={appMode === 'chat'}
  235. appInfo={appInfo}
  236. isShow={showSettingsModal}
  237. onClose={() => setShowSettingsModal(false)}
  238. onSave={onSaveSiteConfig}
  239. />
  240. <EmbeddedModal
  241. siteInfo={appInfo.site}
  242. isShow={showEmbedded}
  243. onClose={() => setShowEmbedded(false)}
  244. appBaseUrl={app_base_url}
  245. accessToken={access_token}
  246. />
  247. <CustomizeModal
  248. isShow={showCustomizeModal}
  249. linkUrl=""
  250. onClose={() => setShowCustomizeModal(false)}
  251. appId={appInfo.id}
  252. api_base_url={appInfo.api_base_url}
  253. mode={appInfo.mode}
  254. />
  255. </>
  256. )
  257. : null}
  258. </div>
  259. )
  260. }
  261. export default AppCard