appCard.tsx 7.1 KB

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