'use client' import type { ChangeEvent, FC } from 'react' import { createRef, useEffect, useState } from 'react' import type { Area } from 'react-easy-crop' import Cropper from 'react-easy-crop' import classNames from 'classnames' import { ImagePlus } from '../icons/src/vender/line/images' import { useDraggableUploader } from './hooks' import { checkIsAnimatedImage } from './utils' import { ALLOW_FILE_EXTENSIONS } from '@/types/app' type UploaderProps = { className?: string onImageCropped?: (tempUrl: string, croppedAreaPixels: Area, fileName: string) => void onUpload?: (file?: File) => void } const Uploader: FC = ({ className, onImageCropped, onUpload, }) => { const [inputImage, setInputImage] = useState<{ file: File; url: string }>() const [isAnimatedImage, setIsAnimatedImage] = useState(false) useEffect(() => { return () => { if (inputImage) URL.revokeObjectURL(inputImage.url) } }, [inputImage]) const [crop, setCrop] = useState({ x: 0, y: 0 }) const [zoom, setZoom] = useState(1) const onCropComplete = async (_: Area, croppedAreaPixels: Area) => { if (!inputImage) return onImageCropped?.(inputImage.url, croppedAreaPixels, inputImage.file.name) onUpload?.(undefined) } const handleLocalFileInput = (e: ChangeEvent) => { const file = e.target.files?.[0] if (file) { setInputImage({ file, url: URL.createObjectURL(file) }) checkIsAnimatedImage(file).then((isAnimatedImage) => { setIsAnimatedImage(!!isAnimatedImage) if (isAnimatedImage) onUpload?.(file) }) } } const { isDragActive, handleDragEnter, handleDragOver, handleDragLeave, handleDrop, } = useDraggableUploader((file: File) => setInputImage({ file, url: URL.createObjectURL(file) })) const inputRef = createRef() const handleShowImage = () => { if (isAnimatedImage) { return ( ) } return ( ) } return (
{ !inputImage ? <>
Drop your image here, or  ((e.target as HTMLInputElement).value = '')} accept={ALLOW_FILE_EXTENSIONS.map(ext => `.${ext}`).join(',')} onChange={handleLocalFileInput} />
Supports PNG, JPG, JPEG, WEBP and GIF
: handleShowImage() }
) } export default Uploader