import { useCallback, useState } from "react"; import { type FileWithPath } from "react-dropzone"; import { Icon } from "@iconify/react"; import Dropzone from "../dropzone"; import Camera from "./camera"; import ImageEditorPortrait from "./image-editor"; interface Props { text: string; type?: "file" | "image"; forceCrop?: boolean; file?: string | File | undefined; setFile?: (value: File | undefined) => void; image?: string | undefined; setImage?: (value: string | undefined) => void; } export default function SwitchFileUpload({ text, type = "image", forceCrop, file, setFile, image, setImage }: Props) { const [isCameraOpen, setIsCameraOpen] = useState(false); const [isCropOpen, setIsCropOpen] = useState(false); const handleDrop = useCallback( (acceptedFiles: FileWithPath[]) => { const file = acceptedFiles[0]; if (type === "file") { setFile!(file); } else { const reader = new FileReader(); reader.onload = (event) => { setImage!(event.target!.result as string); if (forceCrop) setIsCropOpen(true); }; reader.readAsDataURL(file); } }, [setFile, setImage], ); return (
{!file && !image ? (
<>
Drag and drop {text}
or click to open
>
) : (
"Uploaded!"
)}