index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 !h-8 !px-3 bg-white !text-[13px]
  147. ${uploadDisabled ? 'opacity-40' : ''}
  148. `}
  149. disabled={uploadDisabled}
  150. >
  151. <ImagePlus className='mr-2 w-4 h-4' />
  152. {
  153. (webappLogo || fileId)
  154. ? t('custom.change')
  155. : t('custom.upload')
  156. }
  157. <input
  158. className={`
  159. absolute block inset-0 opacity-0 text-[0] w-full
  160. ${uploadDisabled ? 'cursor-not-allowed' : 'cursor-pointer'}
  161. `}
  162. onClick={e => (e.target as HTMLInputElement).value = ''}
  163. type='file'
  164. accept={ALLOW_FILE_EXTENSIONS.map(ext => `.${ext}`).join(',')}
  165. onChange={handleChange}
  166. disabled={uploadDisabled}
  167. />
  168. </Button>
  169. )
  170. }
  171. {
  172. uploading && (
  173. <Button
  174. className='relative mr-2 !h-8 !px-3 bg-white !text-[13px] opacity-40'
  175. disabled={true}
  176. >
  177. <RiLoader2Line className='animate-spin mr-2 w-4 h-4' />
  178. {t('custom.uploading')}
  179. </Button>
  180. )
  181. }
  182. {
  183. fileId && (
  184. <>
  185. <Button
  186. variant='primary'
  187. className='mr-2 !h-8 !px-3 !py-0 !text-[13px]'
  188. onClick={handleApply}
  189. disabled={webappBrandRemoved || !isCurrentWorkspaceManager}
  190. >
  191. {t('custom.apply')}
  192. </Button>
  193. <Button
  194. className='mr-2 !h-8 !px-3 !text-[13px] bg-white'
  195. onClick={handleCancel}
  196. disabled={webappBrandRemoved || !isCurrentWorkspaceManager}
  197. >
  198. {t('common.operation.cancel')}
  199. </Button>
  200. </>
  201. )
  202. }
  203. <div className='mr-2 h-5 w-[1px] bg-black/5'></div>
  204. <Button
  205. className={`
  206. !h-8 !px-3 bg-white !text-[13px]
  207. ${(uploadDisabled || (!webappLogo && !webappBrandRemoved)) ? 'opacity-40' : ''}
  208. `}
  209. disabled={uploadDisabled || (!webappLogo && !webappBrandRemoved)}
  210. onClick={handleRestore}
  211. >
  212. {t('custom.restore')}
  213. </Button>
  214. </div>
  215. </div>
  216. {
  217. uploadProgress === -1 && (
  218. <div className='mt-2 text-xs text-[#D92D20]'>{t('custom.uploadedFail')}</div>
  219. )
  220. }
  221. </div>
  222. )
  223. }
  224. export default CustomWebAppBrand