Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import {
  5. Cog8ToothIcon,
  6. DocumentTextIcon,
  7. RocketLaunchIcon,
  8. ShareIcon,
  9. } from '@heroicons/react/24/outline'
  10. import { SparklesIcon } from '@heroicons/react/24/solid'
  11. import { usePathname, useRouter } from 'next/navigation'
  12. import { useTranslation } from 'react-i18next'
  13. import SettingsModal from './settings'
  14. import ShareLink from './share-link'
  15. import EmbeddedModal from './embedded'
  16. import CustomizeModal from './customize'
  17. import Tooltip from '@/app/components/base/tooltip'
  18. import AppBasic, { randomString } from '@/app/components/app-sidebar/basic'
  19. import Button from '@/app/components/base/button'
  20. import Tag from '@/app/components/base/tag'
  21. import Switch from '@/app/components/base/switch'
  22. import type { AppDetailResponse } from '@/models/app'
  23. import './style.css'
  24. import { AppType } from '@/types/app'
  25. export type IAppCardProps = {
  26. className?: string
  27. appInfo: AppDetailResponse
  28. cardType?: 'app' | 'api' | 'webapp'
  29. customBgColor?: string
  30. onChangeStatus: (val: boolean) => Promise<any>
  31. onSaveSiteConfig?: (params: any) => Promise<any>
  32. onGenerateCode?: () => Promise<any>
  33. }
  34. const EmbedIcon: FC<{ className?: string }> = ({ className = '' }) => {
  35. return <div className={`codeBrowserIcon ${className}`}></div>
  36. }
  37. function AppCard({
  38. appInfo,
  39. cardType = 'app',
  40. customBgColor,
  41. onChangeStatus,
  42. onSaveSiteConfig,
  43. onGenerateCode,
  44. className,
  45. }: IAppCardProps) {
  46. const router = useRouter()
  47. const pathname = usePathname()
  48. const [showSettingsModal, setShowSettingsModal] = useState(false)
  49. const [showShareModal, setShowShareModal] = useState(false)
  50. const [showEmbedded, setShowEmbedded] = useState(false)
  51. const [showCustomizeModal, setShowCustomizeModal] = useState(false)
  52. const { t } = useTranslation()
  53. const OPERATIONS_MAP = {
  54. webapp: [
  55. { opName: t('appOverview.overview.appInfo.preview'), opIcon: RocketLaunchIcon },
  56. { opName: t('appOverview.overview.appInfo.share.entry'), opIcon: ShareIcon },
  57. appInfo.mode === AppType.chat ? { opName: t('appOverview.overview.appInfo.embedded.entry'), opIcon: EmbedIcon } : false,
  58. { opName: t('appOverview.overview.appInfo.settings.entry'), opIcon: Cog8ToothIcon },
  59. ].filter(item => !!item),
  60. api: [{ opName: t('appOverview.overview.apiInfo.doc'), opIcon: DocumentTextIcon }],
  61. app: [],
  62. }
  63. const isApp = cardType === 'app' || cardType === 'webapp'
  64. const basicName = isApp ? appInfo?.site?.title : t('appOverview.overview.apiInfo.title')
  65. const runningStatus = isApp ? appInfo.enable_site : appInfo.enable_api
  66. const { app_base_url, access_token } = appInfo.site ?? {}
  67. const appUrl = `${app_base_url}/${appInfo.mode}/${access_token}`
  68. const apiUrl = appInfo?.api_base_url
  69. let bgColor = 'bg-primary-50 bg-opacity-40'
  70. if (cardType === 'api')
  71. bgColor = 'bg-purple-50'
  72. const genClickFuncByName = (opName: string) => {
  73. switch (opName) {
  74. case t('appOverview.overview.appInfo.preview'):
  75. return () => {
  76. window.open(appUrl, '_blank')
  77. }
  78. case t('appOverview.overview.appInfo.share.entry'):
  79. return () => {
  80. setShowShareModal(true)
  81. }
  82. case t('appOverview.overview.appInfo.settings.entry'):
  83. return () => {
  84. setShowSettingsModal(true)
  85. }
  86. case t('appOverview.overview.appInfo.embedded.entry'):
  87. return () => {
  88. setShowEmbedded(true)
  89. }
  90. default:
  91. // jump to page develop
  92. return () => {
  93. const pathSegments = pathname.split('/')
  94. pathSegments.pop()
  95. router.push(`${pathSegments.join('/')}/develop`)
  96. }
  97. }
  98. }
  99. const onClickCustomize = () => {
  100. setShowCustomizeModal(true)
  101. }
  102. return (
  103. <div
  104. className={`flex flex-col w-full shadow-sm border-[0.5px] rounded-lg border-gray-200 ${className ?? ''}`}
  105. >
  106. <div className={`px-6 py-4 ${customBgColor ?? bgColor} rounded-lg`}>
  107. <div className="mb-2.5 flex flex-row items-start justify-between">
  108. <AppBasic
  109. iconType={cardType}
  110. icon={appInfo.icon}
  111. icon_background={appInfo.icon_background}
  112. name={basicName}
  113. type={
  114. isApp
  115. ? t('appOverview.overview.appInfo.explanation')
  116. : t('appOverview.overview.apiInfo.explanation')
  117. }
  118. />
  119. <div className="flex flex-row items-center h-9">
  120. <Tag className="mr-2" color={runningStatus ? 'green' : 'yellow'}>
  121. {runningStatus ? t('appOverview.overview.status.running') : t('appOverview.overview.status.disable')}
  122. </Tag>
  123. <Switch defaultValue={runningStatus} onChange={onChangeStatus} />
  124. </div>
  125. </div>
  126. <div className="flex flex-col justify-center py-2">
  127. <div className="py-1">
  128. <div className="pb-1 text-xs text-gray-500">
  129. {isApp ? t('appOverview.overview.appInfo.accessibleAddress') : t('appOverview.overview.apiInfo.accessibleAddress')}
  130. </div>
  131. <div className="text-sm text-gray-800">
  132. {isApp ? appUrl : apiUrl}
  133. </div>
  134. </div>
  135. </div>
  136. <div
  137. className={`pt-2 flex flex-row items-center ${!isApp ? 'mb-[calc(2rem_+_1px)]' : ''
  138. }`}
  139. >
  140. {OPERATIONS_MAP[cardType].map((op) => {
  141. return (
  142. <Button
  143. className="mr-2 text-gray-800"
  144. key={op.opName}
  145. onClick={genClickFuncByName(op.opName)}
  146. disabled={
  147. [t('appOverview.overview.appInfo.preview'), t('appOverview.overview.appInfo.share.entry'), t('appOverview.overview.appInfo.embedded.entry')].includes(op.opName) && !runningStatus
  148. }
  149. >
  150. <Tooltip
  151. content={t('appOverview.overview.appInfo.preUseReminder') ?? ''}
  152. selector={`op-btn-${randomString(16)}`}
  153. className={
  154. ([t('appOverview.overview.appInfo.preview'), t('appOverview.overview.appInfo.share.entry'), t('appOverview.overview.appInfo.embedded.entry')].includes(op.opName) && !runningStatus)
  155. ? 'mt-[-8px]'
  156. : '!hidden'
  157. }
  158. >
  159. <div className="flex flex-row items-center">
  160. <op.opIcon className="h-4 w-4 mr-1.5 stroke-[1.8px]" />
  161. <span className="text-xs">{op.opName}</span>
  162. </div>
  163. </Tooltip>
  164. </Button>
  165. )
  166. })}
  167. </div>
  168. </div>
  169. {isApp
  170. ? (
  171. <div
  172. className={
  173. 'flex items-center px-6 py-2 box-border text-xs text-gray-500 bg-opacity-50 bg-white border-t-[0.5px] border-primary-50'
  174. }
  175. >
  176. <div
  177. className="flex items-center hover:text-primary-600 hover:cursor-pointer"
  178. onClick={onClickCustomize}
  179. >
  180. <SparklesIcon className="w-4 h-4 mr-1" />
  181. {t('appOverview.overview.appInfo.customize.entry')}
  182. </div>
  183. </div>
  184. )
  185. : null}
  186. {isApp
  187. ? (
  188. <div>
  189. <ShareLink
  190. isShow={showShareModal}
  191. onClose={() => setShowShareModal(false)}
  192. linkUrl={appUrl}
  193. onGenerateCode={onGenerateCode}
  194. />
  195. <SettingsModal
  196. appInfo={appInfo}
  197. isShow={showSettingsModal}
  198. onClose={() => setShowSettingsModal(false)}
  199. onSave={onSaveSiteConfig}
  200. />
  201. <EmbeddedModal
  202. isShow={showEmbedded}
  203. onClose={() => setShowEmbedded(false)}
  204. appBaseUrl={app_base_url}
  205. accessToken={access_token}
  206. />
  207. <CustomizeModal
  208. isShow={showCustomizeModal}
  209. linkUrl=""
  210. onClose={() => setShowCustomizeModal(false)}
  211. appId={appInfo.id}
  212. mode={appInfo.mode}
  213. />
  214. </div>
  215. )
  216. : null}
  217. </div>
  218. )
  219. }
  220. export default AppCard