appCard.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. export type IAppCardProps = {
  31. className?: string
  32. appInfo: AppDetailResponse
  33. cardType?: 'api' | 'webapp'
  34. customBgColor?: string
  35. onChangeStatus: (val: boolean) => Promise<void>
  36. onSaveSiteConfig?: (params: ConfigParams) => Promise<void>
  37. onGenerateCode?: () => Promise<void>
  38. }
  39. const EmbedIcon = ({ className = '' }: HTMLProps<HTMLDivElement>) => {
  40. return <div className={`${style.codeBrowserIcon} ${className}`}></div>
  41. }
  42. function AppCard({
  43. appInfo,
  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 { currentWorkspace, 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.preview'), opIcon: RocketLaunchIcon },
  64. { opName: t('appOverview.overview.appInfo.customize.entry'), opIcon: PaintBrushIcon },
  65. ] as { opName: string; opIcon: any }[],
  66. api: [{ opName: t('appOverview.overview.apiInfo.doc'), opIcon: DocumentTextIcon }],
  67. app: [],
  68. }
  69. if (appInfo.mode !== 'completion' && appInfo.mode !== 'workflow')
  70. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.embedded.entry'), opIcon: EmbedIcon })
  71. if (isCurrentWorkspaceEditor)
  72. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.settings.entry'), opIcon: Cog8ToothIcon })
  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. let bgColor = 'bg-primary-50 bg-opacity-40'
  86. if (cardType === 'api')
  87. bgColor = 'bg-purple-50'
  88. const genClickFuncByName = (opName: string) => {
  89. switch (opName) {
  90. case t('appOverview.overview.appInfo.preview'):
  91. return () => {
  92. window.open(appUrl, '_blank')
  93. }
  94. case t('appOverview.overview.appInfo.customize.entry'):
  95. return () => {
  96. setShowCustomizeModal(true)
  97. }
  98. case t('appOverview.overview.appInfo.settings.entry'):
  99. return () => {
  100. setShowSettingsModal(true)
  101. }
  102. case t('appOverview.overview.appInfo.embedded.entry'):
  103. return () => {
  104. setShowEmbedded(true)
  105. }
  106. default:
  107. // jump to page develop
  108. return () => {
  109. const pathSegments = pathname.split('/')
  110. pathSegments.pop()
  111. router.push(`${pathSegments.join('/')}/develop`)
  112. }
  113. }
  114. }
  115. const onGenCode = async () => {
  116. if (onGenerateCode) {
  117. setGenLoading(true)
  118. await asyncRunSafe(onGenerateCode())
  119. setGenLoading(false)
  120. }
  121. }
  122. return (
  123. <div
  124. className={`shadow-xs border-[0.5px] rounded-lg border-gray-200 ${className ?? ''
  125. }`}
  126. >
  127. <div className={`px-6 py-5 ${customBgColor ?? bgColor} rounded-lg`}>
  128. <div className="mb-2.5 flex flex-row items-start justify-between">
  129. <AppBasic
  130. iconType={cardType}
  131. icon={appInfo.icon}
  132. icon_background={appInfo.icon_background}
  133. name={basicName}
  134. type={
  135. isApp
  136. ? t('appOverview.overview.appInfo.explanation')
  137. : t('appOverview.overview.apiInfo.explanation')
  138. }
  139. />
  140. <div className="flex flex-row items-center h-9">
  141. <Tag className="mr-2" color={runningStatus ? 'green' : 'yellow'}>
  142. {runningStatus
  143. ? t('appOverview.overview.status.running')
  144. : t('appOverview.overview.status.disable')}
  145. </Tag>
  146. <Switch defaultValue={runningStatus} onChange={onChangeStatus} disabled={toggleDisabled} />
  147. </div>
  148. </div>
  149. <div className="flex flex-col justify-center py-2">
  150. <div className="py-1">
  151. <div className="pb-1 text-xs text-gray-500">
  152. {isApp
  153. ? t('appOverview.overview.appInfo.accessibleAddress')
  154. : t('appOverview.overview.apiInfo.accessibleAddress')}
  155. </div>
  156. <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">
  157. <div className="h-4 px-2 justify-start items-start gap-2 flex flex-1 min-w-0">
  158. <div className="text-gray-700 text-xs font-medium text-ellipsis overflow-hidden whitespace-nowrap">
  159. {isApp ? appUrl : apiUrl}
  160. </div>
  161. </div>
  162. <Divider type="vertical" className="!h-3.5 shrink-0 !mx-0.5" />
  163. {isApp && <ShareQRCode content={isApp ? appUrl : apiUrl} selectorId={randomString(8)} className={'hover:bg-gray-200'} />}
  164. <CopyFeedback
  165. content={isApp ? appUrl : apiUrl}
  166. selectorId={randomString(8)}
  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={''}
  175. isShow={showConfirmDelete}
  176. onClose={() => setShowConfirmDelete(false)}
  177. onConfirm={() => {
  178. onGenCode()
  179. setShowConfirmDelete(false)
  180. }}
  181. onCancel={() => setShowConfirmDelete(false)}
  182. />
  183. )}
  184. {isApp && isCurrentWorkspaceManager && (
  185. <Tooltip
  186. content={t('appOverview.overview.appInfo.regenerate') || ''}
  187. selector={`code-generate-${randomString(8)}`}
  188. >
  189. <div
  190. className="w-8 h-8 ml-0.5 cursor-pointer hover:bg-gray-200 rounded-lg"
  191. onClick={() => setShowConfirmDelete(true)}
  192. >
  193. <div
  194. className={`w-full h-full ${style.refreshIcon} ${genLoading ? style.generateLogo : ''
  195. }`}
  196. ></div>
  197. </div>
  198. </Tooltip>
  199. )}
  200. </div>
  201. </div>
  202. </div>
  203. <div className={'pt-2 flex flex-row items-center flex-wrap gap-y-2'}>
  204. {!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} />}
  205. {OPERATIONS_MAP[cardType].map((op) => {
  206. const disabled
  207. = op.opName === t('appOverview.overview.appInfo.settings.entry')
  208. ? false
  209. : !runningStatus
  210. return (
  211. <Button
  212. className="mr-2"
  213. key={op.opName}
  214. onClick={genClickFuncByName(op.opName)}
  215. disabled={disabled}
  216. >
  217. <Tooltip
  218. content={
  219. t('appOverview.overview.appInfo.preUseReminder') ?? ''
  220. }
  221. selector={`op-btn-${randomString(16)}`}
  222. className={disabled ? 'mt-[-8px]' : '!hidden'}
  223. >
  224. <div className="flex flex-row items-center">
  225. <op.opIcon className="h-4 w-4 mr-1.5 stroke-[1.8px]" />
  226. <span className="text-[13px]">{op.opName}</span>
  227. </div>
  228. </Tooltip>
  229. </Button>
  230. )
  231. })}
  232. </div>
  233. </div>
  234. {isApp
  235. ? (
  236. <>
  237. <SettingsModal
  238. appInfo={appInfo}
  239. isShow={showSettingsModal}
  240. onClose={() => setShowSettingsModal(false)}
  241. onSave={onSaveSiteConfig}
  242. />
  243. <EmbeddedModal
  244. isShow={showEmbedded}
  245. onClose={() => setShowEmbedded(false)}
  246. appBaseUrl={app_base_url}
  247. accessToken={access_token}
  248. />
  249. <CustomizeModal
  250. isShow={showCustomizeModal}
  251. linkUrl=""
  252. onClose={() => setShowCustomizeModal(false)}
  253. appId={appInfo.id}
  254. api_base_url={appInfo.api_base_url}
  255. mode={appInfo.mode}
  256. />
  257. </>
  258. )
  259. : null}
  260. </div>
  261. )
  262. }
  263. export default AppCard