import React, { useCallback, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import produce from 'immer' import { RiEqualizer2Line, RiImage2Fill } from '@remixicon/react' import FeatureCard from '@/app/components/base/features/new-feature-panel/feature-card' import SettingModal from '@/app/components/base/features/new-feature-panel/file-upload/setting-modal' import Badge from '@/app/components/base/badge' import Button from '@/app/components/base/button' import { useFeatures, useFeaturesStore } from '@/app/components/base/features/hooks' import type { OnFeaturesChange } from '@/app/components/base/features/types' import { FeatureEnum } from '@/app/components/base/features/types' type Props = { disabled: boolean onChange?: OnFeaturesChange } const FileUpload = ({ disabled, onChange, }: Props) => { const { t } = useTranslation() const file = useFeatures(s => s.features.file) const featuresStore = useFeaturesStore() const [modalOpen, setModalOpen] = useState(false) const [isHovering, setIsHovering] = useState(false) const supportedTypes = useMemo(() => { return file?.allowed_file_types?.join(',') || '-' }, [file?.allowed_file_types]) const handleChange = useCallback((type: FeatureEnum, enabled: boolean) => { const { features, setFeatures, } = featuresStore!.getState() const newFeatures = produce(features, (draft) => { draft[type] = { ...draft[type], enabled, image: { enabled }, } }) setFeatures(newFeatures) if (onChange) onChange() }, [featuresStore, onChange]) return ( } title={
{t('appDebug.feature.imageUpload.title')}
} value={file?.enabled} onChange={state => handleChange(FeatureEnum.file, state)} onMouseEnter={() => setIsHovering(true)} onMouseLeave={() => setIsHovering(false)} disabled={disabled} > <> {!file?.enabled && (
{t('appDebug.feature.imageUpload.description')}
)} {file?.enabled && ( <> {!isHovering && !modalOpen && (
{t('appDebug.feature.imageUpload.supportedTypes')}
{supportedTypes}
{t('appDebug.feature.imageUpload.numberLimit')}
{file?.number_limits}
)} {(isHovering || modalOpen) && ( { setModalOpen(v) setIsHovering(v) }} onChange={onChange} > )} )}
) } export default FileUpload