appCard.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 'use client'
  2. import type { HTMLProps } from 'react'
  3. import React, { useMemo, useState } from 'react'
  4. import {
  5. Cog8ToothIcon,
  6. DocumentTextIcon,
  7. PaintBrushIcon,
  8. RocketLaunchIcon,
  9. } from '@heroicons/react/24/outline'
  10. import { usePathname, useRouter } from 'next/navigation'
  11. import { useTranslation } from 'react-i18next'
  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 Tag from '@/app/components/base/tag'
  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. export type IAppCardProps = {
  32. className?: string
  33. appInfo: AppDetailResponse & Partial<AppSSO>
  34. cardType?: 'api' | 'webapp'
  35. customBgColor?: string
  36. onChangeStatus: (val: boolean) => Promise<void>
  37. onSaveSiteConfig?: (params: ConfigParams) => Promise<void>
  38. onGenerateCode?: () => Promise<void>
  39. }
  40. const EmbedIcon = ({ className = '' }: HTMLProps<HTMLDivElement>) => {
  41. return <div className={`${style.codeBrowserIcon} ${className}`}></div>
  42. }
  43. function AppCard({
  44. appInfo,
  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.preview'), opIcon: RocketLaunchIcon },
  65. { opName: t('appOverview.overview.appInfo.customize.entry'), opIcon: PaintBrushIcon },
  66. ] as { opName: string; opIcon: any }[],
  67. api: [{ opName: t('appOverview.overview.apiInfo.doc'), opIcon: DocumentTextIcon }],
  68. app: [],
  69. }
  70. if (appInfo.mode !== 'completion' && appInfo.mode !== 'workflow')
  71. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.embedded.entry'), opIcon: EmbedIcon })
  72. if (isCurrentWorkspaceEditor)
  73. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.settings.entry'), opIcon: Cog8ToothIcon })
  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}/${appMode}/${access_token}`
  85. const apiUrl = appInfo?.api_base_url
  86. let bgColor = 'bg-primary-50 bg-opacity-40'
  87. if (cardType === 'api')
  88. bgColor = 'bg-purple-50'
  89. const genClickFuncByName = (opName: string) => {
  90. switch (opName) {
  91. case t('appOverview.overview.appInfo.preview'):
  92. return () => {
  93. window.open(appUrl, '_blank')
  94. }
  95. case t('appOverview.overview.appInfo.customize.entry'):
  96. return () => {
  97. setShowCustomizeModal(true)
  98. }
  99. case t('appOverview.overview.appInfo.settings.entry'):
  100. return () => {
  101. setShowSettingsModal(true)
  102. }
  103. case t('appOverview.overview.appInfo.embedded.entry'):
  104. return () => {
  105. setShowEmbedded(true)
  106. }
  107. default:
  108. // jump to page develop
  109. return () => {
  110. const pathSegments = pathname.split('/')
  111. pathSegments.pop()
  112. router.push(`${pathSegments.join('/')}/develop`)
  113. }
  114. }
  115. }
  116. const onGenCode = async () => {
  117. if (onGenerateCode) {
  118. setGenLoading(true)
  119. await asyncRunSafe(onGenerateCode())
  120. setGenLoading(false)
  121. }
  122. }
  123. return (
  124. <div
  125. className={
  126. `shadow-xs border-[0.5px] rounded-lg border-gray-200 ${className ?? ''}`}
  127. >
  128. <div className={`px-6 py-5 ${customBgColor ?? bgColor} rounded-lg`}>
  129. <div className="mb-2.5 flex flex-row items-start justify-between">
  130. <AppBasic
  131. iconType={cardType}
  132. icon={appInfo.icon}
  133. icon_background={appInfo.icon_background}
  134. name={basicName}
  135. type={
  136. isApp
  137. ? t('appOverview.overview.appInfo.explanation')
  138. : t('appOverview.overview.apiInfo.explanation')
  139. }
  140. />
  141. <div className="flex flex-row items-center h-9">
  142. <Tag className="mr-2" color={runningStatus ? 'green' : 'yellow'}>
  143. {runningStatus
  144. ? t('appOverview.overview.status.running')
  145. : t('appOverview.overview.status.disable')}
  146. </Tag>
  147. <Switch defaultValue={runningStatus} onChange={onChangeStatus} disabled={toggleDisabled} />
  148. </div>
  149. </div>
  150. <div className="flex flex-col justify-center py-2">
  151. <div className="py-1">
  152. <div className="pb-1 text-xs text-gray-500">
  153. {isApp
  154. ? t('appOverview.overview.appInfo.accessibleAddress')
  155. : t('appOverview.overview.apiInfo.accessibleAddress')}
  156. </div>
  157. <div className="w-full h-9 pl-2 pr-0.5 py-0.5 bg-black bg-opacity-2 rounded-lg border border-black border-opacity-5 justify-start items-center inline-flex">
  158. <div className="h-4 px-2 justify-start items-start gap-2 flex flex-1 min-w-0">
  159. <div className="text-gray-700 text-xs font-medium text-ellipsis overflow-hidden whitespace-nowrap">
  160. {isApp ? appUrl : apiUrl}
  161. </div>
  162. </div>
  163. <Divider type="vertical" className="!h-3.5 shrink-0 !mx-0.5" />
  164. {isApp && <ShareQRCode content={isApp ? appUrl : apiUrl} selectorId={randomString(8)} className={'hover:bg-gray-200'} />}
  165. <CopyFeedback
  166. content={isApp ? appUrl : apiUrl}
  167. className={'hover:bg-gray-200'}
  168. />
  169. {/* button copy link/ button regenerate */}
  170. {showConfirmDelete && (
  171. <Confirm
  172. type='warning'
  173. title={t('appOverview.overview.appInfo.regenerate')}
  174. content={t('appOverview.overview.appInfo.regenerateNotice')}
  175. isShow={showConfirmDelete}
  176. onConfirm={() => {
  177. onGenCode()
  178. setShowConfirmDelete(false)
  179. }}
  180. onCancel={() => setShowConfirmDelete(false)}
  181. />
  182. )}
  183. {isApp && isCurrentWorkspaceManager && (
  184. <Tooltip
  185. popupContent={t('appOverview.overview.appInfo.regenerate') || ''}
  186. >
  187. <div
  188. className="w-8 h-8 ml-0.5 cursor-pointer hover:bg-gray-200 rounded-lg"
  189. onClick={() => setShowConfirmDelete(true)}
  190. >
  191. <div
  192. className={
  193. `w-full h-full ${style.refreshIcon} ${genLoading ? style.generateLogo : ''}`}
  194. ></div>
  195. </div>
  196. </Tooltip>
  197. )}
  198. </div>
  199. </div>
  200. </div>
  201. <div className={'pt-2 flex flex-row items-center flex-wrap gap-y-2'}>
  202. {!isApp && <SecretKeyButton className='flex-shrink-0 !h-8 bg-white mr-2' textCls='!text-gray-700 font-medium' iconCls='stroke-[1.2px]' appId={appInfo.id} />}
  203. {OPERATIONS_MAP[cardType].map((op) => {
  204. const disabled
  205. = op.opName === t('appOverview.overview.appInfo.settings.entry')
  206. ? false
  207. : !runningStatus
  208. return (
  209. <Button
  210. className="mr-2"
  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 flex-row items-center">
  222. <op.opIcon className="h-4 w-4 mr-1.5 stroke-[1.8px]" />
  223. <span className="text-[13px]">{op.opName}</span>
  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