appCard.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 SecretKeyButton from '@/app/components/develop/secret-key/secret-key-button'
  26. import type { AppDetailResponse } from '@/models/app'
  27. import { AppType } from '@/types/app'
  28. import { useAppContext } from '@/context/app-context'
  29. export type IAppCardProps = {
  30. className?: string
  31. appInfo: AppDetailResponse
  32. cardType?: 'api' | 'webapp'
  33. customBgColor?: string
  34. onChangeStatus: (val: boolean) => Promise<void>
  35. onSaveSiteConfig?: (params: ConfigParams) => Promise<void>
  36. onGenerateCode?: () => Promise<void>
  37. }
  38. const EmbedIcon = ({ className = '' }: HTMLProps<HTMLDivElement>) => {
  39. return <div className={`${style.codeBrowserIcon} ${className}`}></div>
  40. }
  41. function AppCard({
  42. appInfo,
  43. cardType = 'webapp',
  44. customBgColor,
  45. onChangeStatus,
  46. onSaveSiteConfig,
  47. onGenerateCode,
  48. className,
  49. }: IAppCardProps) {
  50. const router = useRouter()
  51. const pathname = usePathname()
  52. const { currentWorkspace, isCurrentWorkspaceManager } = useAppContext()
  53. const [showSettingsModal, setShowSettingsModal] = useState(false)
  54. const [showEmbedded, setShowEmbedded] = useState(false)
  55. const [showCustomizeModal, setShowCustomizeModal] = useState(false)
  56. const [genLoading, setGenLoading] = useState(false)
  57. const { t } = useTranslation()
  58. const OPERATIONS_MAP = useMemo(() => {
  59. const operationsMap = {
  60. webapp: [
  61. { opName: t('appOverview.overview.appInfo.preview'), opIcon: RocketLaunchIcon },
  62. { opName: t('appOverview.overview.appInfo.customize.entry'), opIcon: PaintBrushIcon },
  63. ] as { opName: string; opIcon: any }[],
  64. api: [{ opName: t('appOverview.overview.apiInfo.doc'), opIcon: DocumentTextIcon }],
  65. app: [],
  66. }
  67. if (appInfo.mode === AppType.chat)
  68. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.embedded.entry'), opIcon: EmbedIcon })
  69. if (isCurrentWorkspaceManager)
  70. operationsMap.webapp.push({ opName: t('appOverview.overview.appInfo.settings.entry'), opIcon: Cog8ToothIcon })
  71. return operationsMap
  72. }, [isCurrentWorkspaceManager, appInfo, t])
  73. const isApp = cardType === 'webapp'
  74. const basicName = isApp
  75. ? appInfo?.site?.title
  76. : t('appOverview.overview.apiInfo.title')
  77. const runningStatus = isApp ? appInfo.enable_site : appInfo.enable_api
  78. const { app_base_url, access_token } = appInfo.site ?? {}
  79. const appUrl = `${app_base_url}/${appInfo.mode}/${access_token}`
  80. const apiUrl = appInfo?.api_base_url
  81. let bgColor = 'bg-primary-50 bg-opacity-40'
  82. if (cardType === 'api')
  83. bgColor = 'bg-purple-50'
  84. const genClickFuncByName = (opName: string) => {
  85. switch (opName) {
  86. case t('appOverview.overview.appInfo.preview'):
  87. return () => {
  88. window.open(appUrl, '_blank')
  89. }
  90. case t('appOverview.overview.appInfo.customize.entry'):
  91. return () => {
  92. setShowCustomizeModal(true)
  93. }
  94. case t('appOverview.overview.appInfo.settings.entry'):
  95. return () => {
  96. setShowSettingsModal(true)
  97. }
  98. case t('appOverview.overview.appInfo.embedded.entry'):
  99. return () => {
  100. setShowEmbedded(true)
  101. }
  102. default:
  103. // jump to page develop
  104. return () => {
  105. const pathSegments = pathname.split('/')
  106. pathSegments.pop()
  107. router.push(`${pathSegments.join('/')}/develop`)
  108. }
  109. }
  110. }
  111. const onGenCode = async () => {
  112. setGenLoading(true)
  113. await asyncRunSafe(onGenerateCode?.() as any)
  114. setGenLoading(false)
  115. }
  116. return (
  117. <div
  118. className={`min-w-max shadow-xs border-[0.5px] rounded-lg border-gray-200 ${
  119. className ?? ''
  120. }`}
  121. >
  122. <div className={`px-6 py-5 ${customBgColor ?? bgColor} rounded-lg`}>
  123. <div className="mb-2.5 flex flex-row items-start justify-between">
  124. <AppBasic
  125. iconType={cardType}
  126. icon={appInfo.icon}
  127. icon_background={appInfo.icon_background}
  128. name={basicName}
  129. type={
  130. isApp
  131. ? t('appOverview.overview.appInfo.explanation')
  132. : t('appOverview.overview.apiInfo.explanation')
  133. }
  134. />
  135. <div className="flex flex-row items-center h-9">
  136. <Tag className="mr-2" color={runningStatus ? 'green' : 'yellow'}>
  137. {runningStatus
  138. ? t('appOverview.overview.status.running')
  139. : t('appOverview.overview.status.disable')}
  140. </Tag>
  141. <Switch defaultValue={runningStatus} onChange={onChangeStatus} disabled={currentWorkspace?.role === 'normal'} />
  142. </div>
  143. </div>
  144. <div className="flex flex-col justify-center py-2">
  145. <div className="py-1">
  146. <div className="pb-1 text-xs text-gray-500">
  147. {isApp
  148. ? t('appOverview.overview.appInfo.accessibleAddress')
  149. : t('appOverview.overview.apiInfo.accessibleAddress')}
  150. </div>
  151. <div className="w-full h-9 pl-2 pr-0.5 py-0.5 bg-black bg-opacity-[0.02] rounded-lg border border-black border-opacity-5 justify-start items-center inline-flex">
  152. <div className="h-4 px-2 justify-start items-start gap-2 flex flex-1">
  153. <div className="text-gray-700 text-xs font-medium">
  154. {isApp ? appUrl : apiUrl}
  155. </div>
  156. </div>
  157. <Divider type="vertical" className="!h-3.5 shrink-0 !mx-0.5" />
  158. <CopyFeedback
  159. content={isApp ? appUrl : apiUrl}
  160. selectorId={randomString(8)}
  161. className={'hover:bg-gray-200'}
  162. />
  163. {/* button copy link/ button regenerate */}
  164. {isApp && isCurrentWorkspaceManager && (
  165. <Tooltip
  166. content={t('appOverview.overview.appInfo.regenerate') || ''}
  167. selector={`code-generate-${randomString(8)}`}
  168. >
  169. <div
  170. className="w-8 h-8 ml-0.5 cursor-pointer hover:bg-gray-200 rounded-lg"
  171. onClick={onGenCode}
  172. >
  173. <div
  174. className={`w-full h-full ${style.refreshIcon} ${
  175. genLoading ? style.generateLogo : ''
  176. }`}
  177. ></div>
  178. </div>
  179. </Tooltip>
  180. )}
  181. </div>
  182. </div>
  183. </div>
  184. <div className={'pt-2 flex flex-row items-center'}>
  185. {!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} />}
  186. {OPERATIONS_MAP[cardType].map((op) => {
  187. const disabled
  188. = op.opName === t('appOverview.overview.appInfo.settings.entry')
  189. ? false
  190. : !runningStatus
  191. return (
  192. <Button
  193. className="mr-2 border-[0.5px] !h-8 hover:outline hover:outline-[0.5px] hover:outline-gray-300 text-gray-700 font-medium bg-white shadow-[0px_1px_2px_0px_rgba(16,24,40,0.05)]"
  194. key={op.opName}
  195. onClick={genClickFuncByName(op.opName)}
  196. disabled={disabled}
  197. >
  198. <Tooltip
  199. content={
  200. t('appOverview.overview.appInfo.preUseReminder') ?? ''
  201. }
  202. selector={`op-btn-${randomString(16)}`}
  203. className={disabled ? 'mt-[-8px]' : '!hidden'}
  204. >
  205. <div className="flex flex-row items-center">
  206. <op.opIcon className="h-4 w-4 mr-1.5 stroke-[1.8px]" />
  207. <span className="text-[13px]">{op.opName}</span>
  208. </div>
  209. </Tooltip>
  210. </Button>
  211. )
  212. })}
  213. </div>
  214. </div>
  215. {isApp
  216. ? (
  217. <>
  218. <SettingsModal
  219. appInfo={appInfo}
  220. isShow={showSettingsModal}
  221. onClose={() => setShowSettingsModal(false)}
  222. onSave={onSaveSiteConfig}
  223. />
  224. <EmbeddedModal
  225. isShow={showEmbedded}
  226. onClose={() => setShowEmbedded(false)}
  227. appBaseUrl={app_base_url}
  228. accessToken={access_token}
  229. />
  230. <CustomizeModal
  231. isShow={showCustomizeModal}
  232. linkUrl=""
  233. onClose={() => setShowCustomizeModal(false)}
  234. appId={appInfo.id}
  235. mode={appInfo.mode}
  236. />
  237. </>
  238. )
  239. : null}
  240. </div>
  241. )
  242. }
  243. export default AppCard