index.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import type { ChangeEvent } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import s from './style.module.css'
  5. import LogoSite from '@/app/components/base/logo/logo-site'
  6. import Switch from '@/app/components/base/switch'
  7. import Button from '@/app/components/base/button'
  8. import { Loading02 } from '@/app/components/base/icons/src/vender/line/general'
  9. import { MessageDotsCircle } from '@/app/components/base/icons/src/vender/solid/communication'
  10. import { ImagePlus } from '@/app/components/base/icons/src/vender/line/images'
  11. import { useProviderContext } from '@/context/provider-context'
  12. import { Plan } from '@/app/components/billing/type'
  13. import { imageUpload } from '@/app/components/base/image-uploader/utils'
  14. import { useToastContext } from '@/app/components/base/toast'
  15. import {
  16. updateCurrentWorkspace,
  17. } from '@/service/common'
  18. import { useAppContext } from '@/context/app-context'
  19. const ALLOW_FILE_EXTENSIONS = ['svg', 'png']
  20. const CustomWebAppBrand = () => {
  21. const { t } = useTranslation()
  22. const { notify } = useToastContext()
  23. const { plan, enableBilling } = useProviderContext()
  24. const {
  25. currentWorkspace,
  26. mutateCurrentWorkspace,
  27. isCurrentWorkspaceManager,
  28. } = useAppContext()
  29. const [fileId, setFileId] = useState('')
  30. const [imgKey, setImgKey] = useState(Date.now())
  31. const [uploadProgress, setUploadProgress] = useState(0)
  32. const isSandbox = enableBilling && plan.type === Plan.sandbox
  33. const uploading = uploadProgress > 0 && uploadProgress < 100
  34. const webappLogo = currentWorkspace.custom_config?.replace_webapp_logo || ''
  35. const webappBrandRemoved = currentWorkspace.custom_config?.remove_webapp_brand
  36. const uploadDisabled = isSandbox || webappBrandRemoved || !isCurrentWorkspaceManager
  37. const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
  38. const file = e.target.files?.[0]
  39. if (!file)
  40. return
  41. if (file.size > 5 * 1024 * 1024) {
  42. notify({ type: 'error', message: t('common.imageUploader.uploadFromComputerLimit', { size: 5 }) })
  43. return
  44. }
  45. imageUpload({
  46. file,
  47. onProgressCallback: (progress) => {
  48. setUploadProgress(progress)
  49. },
  50. onSuccessCallback: (res) => {
  51. setUploadProgress(100)
  52. setFileId(res.id)
  53. },
  54. onErrorCallback: () => {
  55. notify({ type: 'error', message: t('common.imageUploader.uploadFromComputerUploadError') })
  56. setUploadProgress(-1)
  57. },
  58. }, false, '/workspaces/custom-config/webapp-logo/upload')
  59. }
  60. const handleApply = async () => {
  61. await updateCurrentWorkspace({
  62. url: '/workspaces/custom-config',
  63. body: {
  64. remove_webapp_brand: webappBrandRemoved,
  65. replace_webapp_logo: fileId,
  66. },
  67. })
  68. mutateCurrentWorkspace()
  69. setFileId('')
  70. setImgKey(Date.now())
  71. }
  72. const handleRestore = async () => {
  73. await updateCurrentWorkspace({
  74. url: '/workspaces/custom-config',
  75. body: {
  76. remove_webapp_brand: false,
  77. replace_webapp_logo: '',
  78. },
  79. })
  80. mutateCurrentWorkspace()
  81. }
  82. const handleSwitch = async (checked: boolean) => {
  83. await updateCurrentWorkspace({
  84. url: '/workspaces/custom-config',
  85. body: {
  86. remove_webapp_brand: checked,
  87. },
  88. })
  89. mutateCurrentWorkspace()
  90. }
  91. const handleCancel = () => {
  92. setFileId('')
  93. setUploadProgress(0)
  94. }
  95. return (
  96. <div className='py-4'>
  97. <div className='mb-2 text-sm font-medium text-gray-900'>{t('custom.webapp.title')}</div>
  98. <div className='relative mb-4 pl-4 pb-6 pr-[119px] rounded-xl border-[0.5px] border-black/[0.08] shadow-xs bg-gray-50 overflow-hidden'>
  99. <div className={`${s.mask} absolute top-0 left-0 w-full -bottom-2 z-10`}></div>
  100. <div className='flex items-center -mt-2 mb-4 p-6 bg-white rounded-xl'>
  101. <div className='flex items-center px-4 w-[125px] h-9 rounded-lg bg-primary-600 border-[0.5px] border-primary-700 shadow-xs'>
  102. <MessageDotsCircle className='shrink-0 mr-2 w-4 h-4 text-white' />
  103. <div className='grow h-2 rounded-sm bg-white opacity-50' />
  104. </div>
  105. </div>
  106. <div className='flex items-center h-5 justify-between'>
  107. <div className='w-[369px] h-1.5 rounded-sm bg-gray-200 opacity-80' />
  108. {
  109. !webappBrandRemoved && (
  110. <div className='flex items-center text-[10px] font-medium text-gray-400'>
  111. POWERED BY
  112. {
  113. webappLogo
  114. ? <img src={`${webappLogo}?hash=${imgKey}`} alt='logo' className='ml-2 block w-auto h-5' />
  115. : <LogoSite className='ml-2 !h-5' />
  116. }
  117. </div>
  118. )
  119. }
  120. </div>
  121. </div>
  122. <div className='flex items-center justify-between mb-2 px-4 h-14 rounded-xl border-[0.5px] border-gray-200 bg-gray-50 text-sm font-medium text-gray-900'>
  123. {t('custom.webapp.removeBrand')}
  124. <Switch
  125. size='l'
  126. defaultValue={webappBrandRemoved}
  127. disabled={isSandbox || !isCurrentWorkspaceManager}
  128. onChange={handleSwitch}
  129. />
  130. </div>
  131. <div className={`
  132. flex items-center justify-between px-4 py-3 rounded-xl border-[0.5px] border-gray-200 bg-gray-50
  133. ${webappBrandRemoved && 'opacity-30'}
  134. `}>
  135. <div>
  136. <div className='leading-5 text-sm font-medium text-gray-900'>{t('custom.webapp.changeLogo')}</div>
  137. <div className='leading-[18px] text-xs text-gray-500'>{t('custom.webapp.changeLogoTip')}</div>
  138. </div>
  139. <div className='flex items-center'>
  140. {
  141. !uploading && (
  142. <Button
  143. className={`
  144. relative mr-2 !h-8 !px-3 bg-white !text-[13px]
  145. ${uploadDisabled ? 'opacity-40' : ''}
  146. `}
  147. disabled={uploadDisabled}
  148. >
  149. <ImagePlus className='mr-2 w-4 h-4' />
  150. {
  151. (webappLogo || fileId)
  152. ? t('custom.change')
  153. : t('custom.upload')
  154. }
  155. <input
  156. className={`
  157. absolute block inset-0 opacity-0 text-[0] w-full
  158. ${uploadDisabled ? 'cursor-not-allowed' : 'cursor-pointer'}
  159. `}
  160. onClick={e => (e.target as HTMLInputElement).value = ''}
  161. type='file'
  162. accept={ALLOW_FILE_EXTENSIONS.map(ext => `.${ext}`).join(',')}
  163. onChange={handleChange}
  164. disabled={uploadDisabled}
  165. />
  166. </Button>
  167. )
  168. }
  169. {
  170. uploading && (
  171. <Button
  172. className='relative mr-2 !h-8 !px-3 bg-white !text-[13px] opacity-40'
  173. disabled={true}
  174. >
  175. <Loading02 className='animate-spin mr-2 w-4 h-4' />
  176. {t('custom.uploading')}
  177. </Button>
  178. )
  179. }
  180. {
  181. fileId && (
  182. <>
  183. <Button
  184. type='primary'
  185. className='mr-2 !h-8 !px-3 !py-0 !text-[13px]'
  186. onClick={handleApply}
  187. disabled={webappBrandRemoved || !isCurrentWorkspaceManager}
  188. >
  189. {t('custom.apply')}
  190. </Button>
  191. <Button
  192. className='mr-2 !h-8 !px-3 !text-[13px] bg-white'
  193. onClick={handleCancel}
  194. disabled={webappBrandRemoved || !isCurrentWorkspaceManager}
  195. >
  196. {t('common.operation.cancel')}
  197. </Button>
  198. </>
  199. )
  200. }
  201. <div className='mr-2 h-5 w-[1px] bg-black/5'></div>
  202. <Button
  203. className={`
  204. !h-8 !px-3 bg-white !text-[13px]
  205. ${(uploadDisabled || (!webappLogo && !webappBrandRemoved)) ? 'opacity-40' : ''}
  206. `}
  207. disabled={uploadDisabled || (!webappLogo && !webappBrandRemoved)}
  208. onClick={handleRestore}
  209. >
  210. {t('custom.restore')}
  211. </Button>
  212. </div>
  213. </div>
  214. {
  215. uploadProgress === -1 && (
  216. <div className='mt-2 text-xs text-[#D92D20]'>{t('custom.uploadedFail')}</div>
  217. )
  218. }
  219. </div>
  220. )
  221. }
  222. export default CustomWebAppBrand