Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.5 KB
Raw Blame
import type { WorkspaceCustomConfigPayload } from '@dify/contracts/api/console/workspaces/types.gen'
import type { ChangeEvent } from 'react'
import { useMutation, useQuery, useQueryClient, useSuspenseQuery } from '@tanstack/react-query'
import { useAtomValue } from 'jotai'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { getImageUploadErrorMessage, imageUpload } from '@/app/components/base/image-uploader/utils'
import { toast } from '@/app/notifications'
import { workspacePermissionKeysAtom } from '@/context/permission-state'
import { systemFeaturesQueryOptions } from '@/features/system-features/client'
import { consoleQuery } from '@/service/console'
import { hasPermission } from '@/utils/permission'

const MAX_LOGO_FILE_SIZE = 5 * 1024 * 1024
const WEB_APP_LOGO_UPLOAD_URL = '/workspaces/custom-config/webapp-logo/upload'
const useWebAppBrand = () => {
  const { t } = useTranslation(['common'])
  const { data: canReplaceLogo } = useQuery(
    consoleQuery.features.get.queryOptions({
      select: (data) => data.can_replace_logo,
    }),
  )
  const queryClient = useQueryClient()
  const workspacePermissionKeys = useAtomValue(workspacePermissionKeysAtom)
  const [fileId, setFileId] = useState('')
  const [imgKey, setImgKey] = useState(() => Date.now())
  const [uploadProgress, setUploadProgress] = useState(0)
  const [uploading, setUploading] = useState(false)
  const { data: systemFeatures } = useSuspenseQuery(systemFeaturesQueryOptions())
  const customConfigQuery = useQuery(consoleQuery.workspaces.customConfig.get.queryOptions())
  const { data: customConfig } = customConfigQuery
  const updateCustomConfigMutation = useMutation(
    consoleQuery.workspaces.customConfig.post.mutationOptions(),
  )
  const webappLogo = customConfig?.replace_webapp_logo || ''
  const webappBrandRemoved = customConfig?.remove_webapp_brand ?? undefined
  const canManageCustomBrand = hasPermission(workspacePermissionKeys, 'customization.manage')
  const isCustomConfigUnavailable = customConfig === undefined || canReplaceLogo === undefined
  const uploadDisabled =
    isCustomConfigUnavailable || !canReplaceLogo || webappBrandRemoved || !canManageCustomBrand
  const workspaceLogo = systemFeatures.branding.enabled
    ? systemFeatures.branding.workspace_logo
    : ''
  const persistWorkspaceBrand = async (body: WorkspaceCustomConfigPayload) => {
    if (isCustomConfigUnavailable || !canReplaceLogo || !canManageCustomBrand) return
    await updateCustomConfigMutation.mutateAsync({ body })
    await queryClient.invalidateQueries({
      queryKey: consoleQuery.workspaces.customConfig.get.key(),
    })
  }
  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    if (uploadDisabled || uploading) return
    const file = e.target.files?.[0]
    if (!file) return
    if (file.size > MAX_LOGO_FILE_SIZE) {
      toast.error(t(($) => $['imageUploader.uploadFromComputerLimit'], { ns: 'common', size: 5 }))
      return
    }
    setUploadProgress(0)
    setUploading(true)
    imageUpload(
      {
        file,
        onProgressCallback: setUploadProgress,
        onSuccessCallback: (res) => {
          setUploadProgress(100)
          setFileId(res.id)
          setUploading(false)
        },
        onErrorCallback: (error) => {
          const errorMessage = getImageUploadErrorMessage(
            error,
            t(($) => $['imageUploader.uploadFromComputerUploadError'], { ns: 'common' }),
            t,
          )
          toast.error(errorMessage)
          setUploadProgress(-1)
          setUploading(false)
        },
      },
      false,
      WEB_APP_LOGO_UPLOAD_URL,
    )
  }
  const handleApply = async () => {
    await persistWorkspaceBrand({
      remove_webapp_brand: webappBrandRemoved,
      replace_webapp_logo: fileId,
    })
    setFileId('')
    setImgKey(Date.now())
  }
  const handleRestore = async () => {
    await persistWorkspaceBrand({
      remove_webapp_brand: false,
      replace_webapp_logo: '',
    })
  }
  const handleSwitch = async (checked: boolean) => {
    await persistWorkspaceBrand({
      remove_webapp_brand: checked,
    })
  }
  const handleCancel = () => {
    setFileId('')
    setUploadProgress(0)
  }
  return {
    fileId,
    imgKey,
    uploadProgress,
    uploading,
    webappLogo,
    webappBrandRemoved,
    isCustomConfigUnavailable,
    uploadDisabled,
    workspaceLogo,
    canReplaceLogo,
    canManageCustomBrand,
    handleApply,
    handleCancel,
    handleChange,
    handleRestore,
    handleSwitch,
  }
}
export default useWebAppBrand