index.tsx 8.2 KB

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