import { Box, styled, TextField, useTheme } from '@mui/material'; import Typography from '@mui/material/Typography'; import React, { useContext, useEffect, useRef, useState } from 'react'; import InputHeader from '../InputHeader'; import InputFooter from './InputFooter'; import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext'; import greyPattern from '@assets/grey-pattern.png'; import { globalInputHeight } from '../../config/uiConfig'; interface ToolFileInputProps { value: File | null; onChange: (file: File) => void; accept: string[]; title?: string; } export default function ToolFileInput({ value, onChange, accept, title = 'File' }: ToolFileInputProps) { const [preview, setPreview] = useState(null); const theme = useTheme(); const { showSnackBar } = useContext(CustomSnackBarContext); const fileInputRef = useRef(null); const handleCopy = () => { navigator.clipboard .writeText(value?.name ?? '') .then(() => showSnackBar('Text copied', 'success')) .catch((err) => { showSnackBar('Failed to copy: ' + err, 'error'); }); }; useEffect(() => { if (value) { const objectUrl = URL.createObjectURL(value); setPreview(objectUrl); // Clean up memory when the component is unmounted or the file changes return () => URL.revokeObjectURL(objectUrl); } else { setPreview(null); } }, [value]); const handleFileChange = (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (file) onChange(file); }; const handleImportClick = () => { fileInputRef.current?.click(); }; return ( {preview ? ( Preview ) : ( Click here to select an image from your device, press Ctrl+V to use an image from your clipboard, drag and drop a file from desktop )} ); }