import React, { useRef } from "react"; import { Label } from "theme-ui"; import { acceptedImagesTypes } from "@/legacy/lib/consts"; type HandleFileProp = { inputFile: React.RefObject; children?: React.ReactNode; handleFile: (file: File | undefined, isDragActive: boolean) => Promise; isDragActive: boolean; }; type CustomScreenshotProps = { onHandleFile: (file: File, isDragActive: boolean) => Promise; }; const FileInputRenderer: React.FC = ({ inputFile, handleFile, children, isDragActive, }) => ( <> `image/${type}`).join(",")} onChange={(e: React.ChangeEvent) => { void handleFile(e.target.files?.[0], isDragActive); }} /> ); type CustomScreenshotPayload = { handleFile: HandleFileProp["handleFile"]; inputFile: HandleFileProp["inputFile"]; FileInputRenderer: React.FC; fileInputProps: { inputFile: HandleFileProp["inputFile"]; handleFile: HandleFileProp["handleFile"]; }; }; export default function useCustomScreenshot({ onHandleFile, }: CustomScreenshotProps): CustomScreenshotPayload { const inputFile = useRef(null); const handleFile = async (file: File | undefined, isDragActive: boolean) => { if (file) { await onHandleFile(file, isDragActive); if (inputFile?.current) { inputFile.current.value = ""; } } }; return { handleFile, inputFile, FileInputRenderer, fileInputProps: { inputFile, handleFile, }, }; }