import { memo, useState, } from 'react' import { useTranslation } from 'react-i18next' import { RiUploadCloud2Line } from '@remixicon/react' import FileInput from '../file-input' import { useFile } from '../hooks' import { useStore } from '../store' import { FILE_URL_REGEX } from '../constants' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger, } from '@/app/components/base/portal-to-follow-elem' import Button from '@/app/components/base/button' import type { FileUpload } from '@/app/components/base/features/types' import cn from '@/utils/classnames' type FileFromLinkOrLocalProps = { showFromLink?: boolean showFromLocal?: boolean trigger: (open: boolean) => React.ReactNode fileConfig: FileUpload } const FileFromLinkOrLocal = ({ showFromLink = true, showFromLocal = true, trigger, fileConfig, }: FileFromLinkOrLocalProps) => { const { t } = useTranslation() const files = useStore(s => s.files) const [open, setOpen] = useState(false) const [url, setUrl] = useState('') const [showError, setShowError] = useState(false) const { handleLoadFileFromLink } = useFile(fileConfig) const disabled = !!fileConfig.number_limits && files.length >= fileConfig.number_limits const handleSaveUrl = () => { if (!url) return if (!FILE_URL_REGEX.test(url)) { setShowError(true) return } handleLoadFileFromLink(url) setUrl('') } return ( setOpen(v => !v)} asChild> {trigger(open)}
{ showFromLink && ( <>
{ setShowError(false) setUrl(e.target.value) }} disabled={disabled} />
{ showError && (
{t('common.fileUploader.pasteFileLinkInvalid')}
) } ) } { showFromLink && showFromLocal && (
OR
) } { showFromLocal && ( ) }
) } export default memo(FileFromLinkOrLocal)