index.tsx 7.9 KB

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