You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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