import React, { useRef } from 'react'; import { Box } from '@mui/material'; import Typography from '@mui/material/Typography'; import InputHeader from '../InputHeader'; import InputFooter from './InputFooter'; import ImageIcon from '@mui/icons-material/Image'; import { useTranslation } from 'react-i18next'; import { heicTo, isHeic } from 'heic-to'; interface MultiImageInputComponentProps { accept: string[]; title?: string; type: 'image'; value: MultiImageInput[]; onChange: (files: MultiImageInput[]) => void; } export interface MultiImageInput { file: File; order: number; preview?: string; } export default function ToolMultiImageInput({ value, onChange, accept, title, type }: MultiImageInputComponentProps) { const { t } = useTranslation(); const fileInputRef = useRef(null); const handleFileChange = async ( event: React.ChangeEvent ) => { const files = event.target.files; if (!files) return; const newFiles: MultiImageInput[] = await Promise.all( Array.from(files).map(async (file, index) => { let processedFile = file; try { if (await isHeic(file)) { const convertedBlob = await heicTo({ blob: file, type: 'image/jpeg' }); processedFile = new File( [convertedBlob], file.name.replace(/\.[^/.]+$/, '') + '.jpg', { type: 'image/jpeg' } ); } } catch (err) { console.warn('HEIC conversion failed:', file.name, err); } return { file: processedFile, order: value.length + index, preview: URL.createObjectURL(processedFile) }; }) ); onChange([...value, ...newFiles]); event.target.value = ''; }; const handleImportClick = () => { fileInputRef.current?.click(); }; function handleClear() { value.forEach((f) => { if (f.preview) URL.revokeObjectURL(f.preview); }); onChange([]); } function handleRemove(index: number) { const fileToRemove = value[index]; if (fileToRemove?.preview) { URL.revokeObjectURL(fileToRemove.preview); } const updatedFiles = value.filter((_, i) => i !== index); onChange(updatedFiles); } function fileNameTruncate(fileName: string) { const maxLength = 10; return fileName.length > maxLength ? fileName.slice(0, maxLength) + '...' : fileName; } return ( {value?.length ? ( value.map((file, index) => ( {file.preview ? ( ) : ( )} {fileNameTruncate(file.file.name)} handleRemove(index)} > ✖ )) ) : ( {t('toolMultipleImageInput.noFilesSelected')} )} ); }