index.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 type {} from '@/app/components/base/image-uploader/utils'
  15. import { useToastContext } from '@/app/components/base/toast'
  16. import {
  17. updateCurrentWorkspace,
  18. } from '@/service/common'
  19. import { useAppContext } from '@/context/app-context'
  20. import { API_PREFIX } from '@/config'
  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 [uploadProgress, setUploadProgress] = useState(0)
  33. const isSandbox = enableBilling && plan.type === Plan.sandbox
  34. const uploading = uploadProgress > 0 && uploadProgress < 100
  35. const webappLogo = currentWorkspace.custom_config?.replace_webapp_logo || ''
  36. const webappBrandRemoved = currentWorkspace.custom_config?.remove_webapp_brand
  37. const uploadDisabled = isSandbox || webappBrandRemoved || !isCurrentWorkspaceManager
  38. const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
  39. const file = e.target.files?.[0]
  40. if (!file)
  41. return
  42. if (file.size > 5 * 1024 * 1024) {
  43. notify({ type: 'error', message: t('common.imageUploader.uploadFromComputerLimit', { size: 5 }) })
  44. return
  45. }
  46. imageUpload({
  47. file,
  48. onProgressCallback: (progress) => {
  49. setUploadProgress(progress)
  50. },
  51. onSuccessCallback: (res) => {
  52. setUploadProgress(100)
  53. setFileId(res.id)
  54. },
  55. onErrorCallback: () => {
  56. notify({ type: 'error', message: t('common.imageUploader.uploadFromComputerUploadError') })
  57. setUploadProgress(-1)
  58. },
  59. }, false, '/workspaces/custom-config/webapp-logo/upload')
  60. }
  61. const handleApply = async () => {
  62. await updateCurrentWorkspace({
  63. url: '/workspaces/custom-config',
  64. body: {
  65. remove_webapp_brand: webappBrandRemoved,
  66. replace_webapp_logo: fileId,
  67. },
  68. })
  69. mutateCurrentWorkspace()
  70. setFileId('')
  71. }
  72. const handleRestore = async () => {
  73. await updateCurrentWorkspace({
  74. url: '/workspaces/custom-config',
  75. body: {
  76. remove_webapp_brand: false,
  77. replace_webapp_logo: null,
  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. replace_webapp_logo: webappLogo,
  88. },
  89. })
  90. mutateCurrentWorkspace()
  91. }
  92. const handleCancel = () => {
  93. setFileId('')
  94. setUploadProgress(0)
  95. }
  96. return (
  97. <div className='py-4'>
  98. <div className='mb-2 text-sm font-medium text-gray-900'>{t('custom.webapp.title')}</div>
  99. <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'>
  100. <div className={`${s.mask} absolute top-0 left-0 w-full -bottom-2 z-10`}></div>
  101. <div className='flex items-center -mt-2 mb-4 p-6 bg-white rounded-xl'>
  102. <div className='flex items-center px-4 w-[125px] h-9 rounded-lg bg-primary-600 border-[0.5px] border-primary-700 shadow-xs'>
  103. <MessageDotsCircle className='shrink-0 mr-2 w-4 h-4 text-white' />
  104. <div className='grow h-2 rounded-sm bg-white opacity-50' />
  105. </div>
  106. </div>
  107. <div className='flex items-center h-5 justify-between'>
  108. <div className='w-[369px] h-1.5 rounded-sm bg-gray-200 opacity-80' />
  109. {
  110. !webappBrandRemoved && (
  111. <div className='flex items-center text-[10px] font-medium text-gray-400'>
  112. POWERED BY
  113. {
  114. webappLogo
  115. ? <img key={webappLogo} src={`${API_PREFIX.slice(0, -12)}/files/workspaces/${currentWorkspace.id}/webapp-logo`} alt='logo' className='ml-2 block w-auto h-5' />
  116. : <LogoSite className='ml-2 !h-5' />
  117. }
  118. </div>
  119. )
  120. }
  121. </div>
  122. </div>
  123. <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'>
  124. {t('custom.webapp.removeBrand')}
  125. <Switch
  126. size='l'
  127. defaultValue={webappBrandRemoved}
  128. disabled={isSandbox || !isCurrentWorkspaceManager}
  129. onChange={handleSwitch}
  130. />
  131. </div>
  132. <div className={`
  133. flex items-center justify-between px-4 py-3 rounded-xl border-[0.5px] border-gray-200 bg-gray-50
  134. ${webappBrandRemoved && 'opacity-30'}
  135. `}>
  136. <div>
  137. <div className='leading-5 text-sm font-medium text-gray-900'>{t('custom.webapp.changeLogo')}</div>
  138. <div className='leading-[18px] text-xs text-gray-500'>{t('custom.webapp.changeLogoTip')}</div>
  139. </div>
  140. <div className='flex items-center'>
  141. {
  142. !uploading && (
  143. <Button
  144. className={`
  145. relative mr-2 !h-8 !px-3 bg-white !text-[13px]
  146. ${uploadDisabled ? 'opacity-40' : ''}
  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 !h-8 !px-3 bg-white !text-[13px] opacity-40'
  174. disabled={true}
  175. >
  176. <Loading02 className='animate-spin mr-2 w-4 h-4' />
  177. {t('custom.uploading')}
  178. </Button>
  179. )
  180. }
  181. {
  182. fileId && (
  183. <>
  184. <Button
  185. type='primary'
  186. className='mr-2 !h-8 !px-3 !py-0 !text-[13px]'
  187. onClick={handleApply}
  188. disabled={webappBrandRemoved || !isCurrentWorkspaceManager}
  189. >
  190. {t('custom.apply')}
  191. </Button>
  192. <Button
  193. className='mr-2 !h-8 !px-3 !text-[13px] bg-white'
  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. className={`
  205. !h-8 !px-3 bg-white !text-[13px]
  206. ${(uploadDisabled || (!webappLogo && !webappBrandRemoved)) ? 'opacity-40' : ''}
  207. `}
  208. disabled={uploadDisabled || (!webappLogo && !webappBrandRemoved)}
  209. onClick={handleRestore}
  210. >
  211. {t('custom.restore')}
  212. </Button>
  213. </div>
  214. </div>
  215. {
  216. uploadProgress === -1 && (
  217. <div className='mt-2 text-xs text-[#D92D20]'>{t('custom.uploadedFail')}</div>
  218. )
  219. }
  220. </div>
  221. )
  222. }
  223. export default CustomWebAppBrand