import type { ChangeEvent } from 'react' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { RiLoader2Line, } from '@remixicon/react' import s from './style.module.css' import LogoSite from '@/app/components/base/logo/logo-site' import Switch from '@/app/components/base/switch' import Button from '@/app/components/base/button' import { MessageDotsCircle } from '@/app/components/base/icons/src/vender/solid/communication' import { ImagePlus } from '@/app/components/base/icons/src/vender/line/images' import { useProviderContext } from '@/context/provider-context' import { Plan } from '@/app/components/billing/type' import { imageUpload } from '@/app/components/base/image-uploader/utils' import { useToastContext } from '@/app/components/base/toast' import { updateCurrentWorkspace, } from '@/service/common' import { useAppContext } from '@/context/app-context' const ALLOW_FILE_EXTENSIONS = ['svg', 'png'] const CustomWebAppBrand = () => { const { t } = useTranslation() const { notify } = useToastContext() const { plan, enableBilling } = useProviderContext() const { currentWorkspace, mutateCurrentWorkspace, isCurrentWorkspaceManager, } = useAppContext() const [fileId, setFileId] = useState('') const [imgKey, setImgKey] = useState(Date.now()) const [uploadProgress, setUploadProgress] = useState(0) const isSandbox = enableBilling && plan.type === Plan.sandbox const uploading = uploadProgress > 0 && uploadProgress < 100 const webappLogo = currentWorkspace.custom_config?.replace_webapp_logo || '' const webappBrandRemoved = currentWorkspace.custom_config?.remove_webapp_brand const uploadDisabled = isSandbox || webappBrandRemoved || !isCurrentWorkspaceManager const handleChange = (e: ChangeEvent) => { const file = e.target.files?.[0] if (!file) return if (file.size > 5 * 1024 * 1024) { notify({ type: 'error', message: t('common.imageUploader.uploadFromComputerLimit', { size: 5 }) }) return } imageUpload({ file, onProgressCallback: (progress) => { setUploadProgress(progress) }, onSuccessCallback: (res) => { setUploadProgress(100) setFileId(res.id) }, onErrorCallback: () => { notify({ type: 'error', message: t('common.imageUploader.uploadFromComputerUploadError') }) setUploadProgress(-1) }, }, false, '/workspaces/custom-config/webapp-logo/upload') } const handleApply = async () => { await updateCurrentWorkspace({ url: '/workspaces/custom-config', body: { remove_webapp_brand: webappBrandRemoved, replace_webapp_logo: fileId, }, }) mutateCurrentWorkspace() setFileId('') setImgKey(Date.now()) } const handleRestore = async () => { await updateCurrentWorkspace({ url: '/workspaces/custom-config', body: { remove_webapp_brand: false, replace_webapp_logo: '', }, }) mutateCurrentWorkspace() } const handleSwitch = async (checked: boolean) => { await updateCurrentWorkspace({ url: '/workspaces/custom-config', body: { remove_webapp_brand: checked, }, }) mutateCurrentWorkspace() } const handleCancel = () => { setFileId('') setUploadProgress(0) } return (
{t('custom.webapp.title')}
{ !webappBrandRemoved && (
POWERED BY { webappLogo ? logo : }
) }
{t('custom.webapp.removeBrand')}
{t('custom.webapp.changeLogo')}
{t('custom.webapp.changeLogoTip')}
{ !uploading && ( ) } { uploading && ( ) } { fileId && ( <> ) }
{ uploadProgress === -1 && (
{t('custom.uploadedFail')}
) }
) } export default CustomWebAppBrand