appCard.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if (onGenerateCode) {
  113. setGenLoading(true)
  114. await asyncRunSafe(onGenerateCode())
  115. setGenLoading(false)
  116. }
  117. }
  118. return (
  119. <div
  120. className={`min-w-max shadow-xs border-[0.5px] rounded-lg border-gray-200 ${
  121. className ?? ''
  122. }`}
  123. >
  124. <div className={`px-6 py-5 ${customBgColor ?? bgColor} rounded-lg`}>
  125. <div className="mb-2.5 flex flex-row items-start justify-between">
  126. <AppBasic
  127. iconType={cardType}
  128. icon={appInfo.icon}
  129. icon_background={appInfo.icon_background}
  130. name={basicName}
  131. type={
  132. isApp
  133. ? t('appOverview.overview.appInfo.explanation')
  134. : t('appOverview.overview.apiInfo.explanation')
  135. }
  136. />
  137. <div className="flex flex-row items-center h-9">
  138. <Tag className="mr-2" color={runningStatus ? 'green' : 'yellow'}>
  139. {runningStatus
  140. ? t('appOverview.overview.status.running')
  141. : t('appOverview.overview.status.disable')}
  142. </Tag>
  143. <Switch defaultValue={runningStatus} onChange={onChangeStatus} disabled={currentWorkspace?.role === 'normal'} />
  144. </div>
  145. </div>
  146. <div className="flex flex-col justify-center py-2">
  147. <div className="py-1">
  148. <div className="pb-1 text-xs text-gray-500">
  149. {isApp
  150. ? t('appOverview.overview.appInfo.accessibleAddress')
  151. : t('appOverview.overview.apiInfo.accessibleAddress')}
  152. </div>
  153. <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">
  154. <div className="h-4 px-2 justify-start items-start gap-2 flex flex-1">
  155. <div className="text-gray-700 text-xs font-medium">
  156. {isApp ? appUrl : apiUrl}
  157. </div>
  158. </div>
  159. <Divider type="vertical" className="!h-3.5 shrink-0 !mx-0.5" />
  160. <CopyFeedback
  161. content={isApp ? appUrl : apiUrl}
  162. selectorId={randomString(8)}
  163. className={'hover:bg-gray-200'}
  164. />
  165. {/* button copy link/ button regenerate */}
  166. {isApp && isCurrentWorkspaceManager && (
  167. <Tooltip
  168. content={t('appOverview.overview.appInfo.regenerate') || ''}
  169. selector={`code-generate-${randomString(8)}`}
  170. >
  171. <div
  172. className="w-8 h-8 ml-0.5 cursor-pointer hover:bg-gray-200 rounded-lg"
  173. onClick={onGenCode}
  174. >
  175. <div
  176. className={`w-full h-full ${style.refreshIcon} ${
  177. genLoading ? style.generateLogo : ''
  178. }`}
  179. ></div>
  180. </div>
  181. </Tooltip>
  182. )}
  183. </div>
  184. </div>
  185. </div>
  186. <div className={'pt-2 flex flex-row items-center'}>
  187. {!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} />}
  188. {OPERATIONS_MAP[cardType].map((op) => {
  189. const disabled
  190. = op.opName === t('appOverview.overview.appInfo.settings.entry')
  191. ? false
  192. : !runningStatus
  193. return (
  194. <Button
  195. 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)]"
  196. key={op.opName}
  197. onClick={genClickFuncByName(op.opName)}
  198. disabled={disabled}
  199. >
  200. <Tooltip
  201. content={
  202. t('appOverview.overview.appInfo.preUseReminder') ?? ''
  203. }
  204. selector={`op-btn-${randomString(16)}`}
  205. className={disabled ? 'mt-[-8px]' : '!hidden'}
  206. >
  207. <div className="flex flex-row items-center">
  208. <op.opIcon className="h-4 w-4 mr-1.5 stroke-[1.8px]" />
  209. <span className="text-[13px]">{op.opName}</span>
  210. </div>
  211. </Tooltip>
  212. </Button>
  213. )
  214. })}
  215. </div>
  216. </div>
  217. {isApp
  218. ? (
  219. <>
  220. <SettingsModal
  221. appInfo={appInfo}
  222. isShow={showSettingsModal}
  223. onClose={() => setShowSettingsModal(false)}
  224. onSave={onSaveSiteConfig}
  225. />
  226. <EmbeddedModal
  227. isShow={showEmbedded}
  228. onClose={() => setShowEmbedded(false)}
  229. appBaseUrl={app_base_url}
  230. accessToken={access_token}
  231. />
  232. <CustomizeModal
  233. isShow={showCustomizeModal}
  234. linkUrl=""
  235. onClose={() => setShowCustomizeModal(false)}
  236. appId={appInfo.id}
  237. mode={appInfo.mode}
  238. />
  239. </>
  240. )
  241. : null}
  242. </div>
  243. )
  244. }
  245. export default AppCard