diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index cc0627ee..8998c196 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -242,8 +242,11 @@ }, "toolMultipleInput": { "deleteFile": "Delete file", + "dropFilesHere": "Drop your files here", "inputTitle": "Input {{type}}", - "noFilesSelected": "No files selected" + "invalidFileType": "Invalid file type. Please select files of a supported type.", + "noFilesSelected": "No files selected", + "selectFilesDescription": "Click here to select files from your device, or drag and drop files here" }, "toolOptions": { "title": "Tool options" diff --git a/src/components/input/ToolMultipleAudioInput.tsx b/src/components/input/ToolMultipleAudioInput.tsx index e1682e77..ee0f10d8 100644 --- a/src/components/input/ToolMultipleAudioInput.tsx +++ b/src/components/input/ToolMultipleAudioInput.tsx @@ -6,6 +6,7 @@ import InputFooter from './InputFooter'; import MusicNoteIcon from '@mui/icons-material/MusicNote'; import ClearIcon from '@mui/icons-material/Clear'; import { useTranslation } from 'react-i18next'; +import useFileDrop from '../../hooks/useFileDrop'; interface MultiAudioInputComponentProps { accept: string[]; @@ -31,13 +32,20 @@ export default function ToolMultipleAudioInput({ const theme = useTheme(); const fileInputRef = useRef(null); - const handleFileChange = (event: React.ChangeEvent) => { - const files = event.target.files; - if (files) + const { isDragging, dropHandlers, addFiles } = useFileDrop({ + accept, + onFiles: (files) => onChange([ ...value, - ...Array.from(files).map((file) => ({ file, order: value.length })) - ]); + ...files.map((file, index) => ({ file, order: value.length + index })) + ]) + }); + + const handleFileChange = async ( + event: React.ChangeEvent + ) => { + if (event.target.files) await addFiles(event.target.files); + event.target.value = ''; }; const handleImportClick = () => { @@ -110,19 +118,25 @@ export default function ToolMultipleAudioInput({ borderRadius: 2, boxShadow: '5', bgcolor: 'background.paper', - position: 'relative' + position: 'relative', + borderColor: isDragging ? 'primary.main' : undefined, + borderWidth: isDragging ? 2 : undefined, + borderStyle: isDragging ? 'dashed' : undefined }} + {...dropHandlers} > {value?.length ? ( @@ -175,9 +189,18 @@ export default function ToolMultipleAudioInput({ )) + ) : isDragging ? ( + + {t('toolMultipleInput.dropFilesHere')} + ) : ( - - {t('toolMultipleInput.noFilesSelected')} + + {t('toolMultipleInput.selectFilesDescription')} )} diff --git a/src/components/input/ToolMultipleImageInput.tsx b/src/components/input/ToolMultipleImageInput.tsx index 6804caad..cc54c9ef 100644 --- a/src/components/input/ToolMultipleImageInput.tsx +++ b/src/components/input/ToolMultipleImageInput.tsx @@ -1,11 +1,12 @@ import React, { useRef } from 'react'; -import { Box } from '@mui/material'; +import { Box, useTheme } 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'; +import useFileDrop from '../../hooks/useFileDrop'; interface MultiImageInputComponentProps { accept: string[]; @@ -30,6 +31,42 @@ export default function ToolMultiImageInput({ }: MultiImageInputComponentProps) { const { t } = useTranslation(); const fileInputRef = useRef(null); + const theme = useTheme(); + const { isDragging, dropHandlers, addFiles } = useFileDrop({ + accept, + onFiles: async (acceptedFiles) => { + const newFiles: MultiImageInput[] = await Promise.all( + acceptedFiles.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]); + } + }); const handleFileChange = async ( event: React.ChangeEvent @@ -37,36 +74,7 @@ export default function ToolMultiImageInput({ 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]); + await addFiles(files); event.target.value = ''; }; @@ -117,19 +125,25 @@ export default function ToolMultiImageInput({ borderRadius: 2, boxShadow: '5', bgcolor: 'background.paper', - position: 'relative' + position: 'relative', + borderColor: isDragging ? theme.palette.primary.main : undefined, + borderWidth: isDragging ? 2 : 1, + borderStyle: isDragging ? 'dashed' : 'solid' }} + {...dropHandlers} > {value?.length ? ( @@ -185,9 +199,18 @@ export default function ToolMultiImageInput({ )) + ) : isDragging ? ( + + {t('toolMultipleInput.dropFilesHere')} + ) : ( - - {t('toolMultipleInput.noFilesSelected')} + + {t('toolMultipleInput.selectFilesDescription')} )} diff --git a/src/components/input/ToolMultiplePdfInput.tsx b/src/components/input/ToolMultiplePdfInput.tsx index 304d28f6..d0227151 100644 --- a/src/components/input/ToolMultiplePdfInput.tsx +++ b/src/components/input/ToolMultiplePdfInput.tsx @@ -6,6 +6,7 @@ import InputFooter from './InputFooter'; import ClearIcon from '@mui/icons-material/Clear'; import PictureAsPdfIcon from '@mui/icons-material/PictureAsPdf'; import { useTranslation } from 'react-i18next'; +import useFileDrop from '../../hooks/useFileDrop'; interface MultiPdfInputComponentProps { accept: string[]; @@ -31,13 +32,20 @@ export default function ToolMultiFileInput({ const theme = useTheme(); const fileInputRef = useRef(null); - const handleFileChange = (event: React.ChangeEvent) => { - const files = event.target.files; - if (files) + const { isDragging, dropHandlers, addFiles } = useFileDrop({ + accept, + onFiles: (files) => onChange([ ...value, - ...Array.from(files).map((file) => ({ file, order: value.length })) - ]); + ...files.map((file, index) => ({ file, order: value.length + index })) + ]) + }); + + const handleFileChange = async ( + event: React.ChangeEvent + ) => { + if (event.target.files) await addFiles(event.target.files); + event.target.value = ''; }; const handleImportClick = () => { @@ -111,19 +119,25 @@ export default function ToolMultiFileInput({ borderRadius: 2, boxShadow: '5', bgcolor: 'background.paper', - position: 'relative' + position: 'relative', + borderColor: isDragging ? 'primary.main' : undefined, + borderWidth: isDragging ? 2 : undefined, + borderStyle: isDragging ? 'dashed' : undefined }} + {...dropHandlers} > {value?.length ? ( @@ -176,9 +190,18 @@ export default function ToolMultiFileInput({ )) + ) : isDragging ? ( + + {t('toolMultipleInput.dropFilesHere')} + ) : ( - - {t('toolMultipleInput.noFilesSelected')} + + {t('toolMultipleInput.selectFilesDescription')} )} diff --git a/src/components/input/ToolMultipleVideoInput.tsx b/src/components/input/ToolMultipleVideoInput.tsx index d861df18..997c2ac4 100644 --- a/src/components/input/ToolMultipleVideoInput.tsx +++ b/src/components/input/ToolMultipleVideoInput.tsx @@ -6,6 +6,7 @@ import InputFooter from './InputFooter'; import VideoFileIcon from '@mui/icons-material/VideoFile'; import ClearIcon from '@mui/icons-material/Clear'; import { useTranslation } from 'react-i18next'; +import useFileDrop from '../../hooks/useFileDrop'; interface MultiVideoInputComponentProps { accept: string[]; @@ -30,13 +31,20 @@ export default function ToolMultipleVideoInput({ const { t } = useTranslation(); const fileInputRef = useRef(null); - const handleFileChange = (event: React.ChangeEvent) => { - const files = event.target.files; - if (files) + const { isDragging, dropHandlers, addFiles } = useFileDrop({ + accept, + onFiles: (files) => onChange([ ...value, - ...Array.from(files).map((file) => ({ file, order: value.length })) - ]); + ...files.map((file, index) => ({ file, order: value.length + index })) + ]) + }); + + const handleFileChange = async ( + event: React.ChangeEvent + ) => { + if (event.target.files) await addFiles(event.target.files); + event.target.value = ''; }; const handleImportClick = () => { @@ -109,19 +117,25 @@ export default function ToolMultipleVideoInput({ borderRadius: 2, boxShadow: '5', bgcolor: 'background.paper', - position: 'relative' + position: 'relative', + borderColor: isDragging ? 'primary.main' : undefined, + borderWidth: isDragging ? 2 : undefined, + borderStyle: isDragging ? 'dashed' : undefined }} + {...dropHandlers} > {value?.length ? ( @@ -174,9 +188,18 @@ export default function ToolMultipleVideoInput({ )) + ) : isDragging ? ( + + {t('toolMultipleInput.dropFilesHere')} + ) : ( - - {t('toolMultipleInput.noFilesSelected')} + + {t('toolMultipleInput.selectFilesDescription')} )} diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 19299255..7013894a 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -2,3 +2,4 @@ export { default as useDebounce } from './useDebounce'; export { default as useTimeout } from './useTimeout'; export { default as usePrevious } from './usePrevious'; export { default as useUpdateEffect } from './useUpdateEffect'; +export { default as useFileDrop } from './useFileDrop'; diff --git a/src/hooks/useFileDrop.ts b/src/hooks/useFileDrop.ts new file mode 100644 index 00000000..6fccfe93 --- /dev/null +++ b/src/hooks/useFileDrop.ts @@ -0,0 +1,81 @@ +import { useContext, useRef, useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { CustomSnackBarContext } from '../contexts/CustomSnackBarContext'; + +const HEIC_EXTENSIONS = ['heic', 'heif']; + +const getExtension = (name: string) => + name.includes('.') ? name.split('.').pop()!.toLowerCase() : ''; + +export const isAcceptedFile = (file: File, accept: string[]): boolean => { + const extension = getExtension(file.name); + return accept.some((acceptType) => { + if (acceptType.startsWith('.')) { + return extension === acceptType.slice(1).toLowerCase(); + } + if (acceptType.endsWith('/*')) { + const category = acceptType.split('/')[0]; + if (file.type) return file.type.startsWith(`${category}/`); + // Some browsers report an empty type for HEIC/HEIF files + return category === 'image' && HEIC_EXTENSIONS.includes(extension); + } + if (acceptType === file.type) return true; + return ( + !file.type && + /^image\/hei[cf]$/.test(acceptType) && + HEIC_EXTENSIONS.includes(extension) + ); + }); +}; + +interface UseFileDropOptions { + accept: string[]; + onFiles: (files: File[]) => void | Promise; +} + +/** + * Shared drag-and-drop / file filtering logic for the multiple file inputs. + * Spread `dropHandlers` on the drop zone and pass picker files to `addFiles`. + */ +export default function useFileDrop({ accept, onFiles }: UseFileDropOptions) { + const { t } = useTranslation(); + const { showSnackBar } = useContext(CustomSnackBarContext); + const [isDragging, setIsDragging] = useState(false); + // dragenter/dragleave also fire for child elements, so count them + const dragCounter = useRef(0); + + const addFiles = async (files: FileList | File[]) => { + const all = Array.from(files); + const accepted = all.filter((file) => isAcceptedFile(file, accept)); + if (accepted.length < all.length) { + showSnackBar(t('toolMultipleInput.invalidFileType'), 'error'); + } + if (accepted.length > 0) await onFiles(accepted); + }; + + const dropHandlers = { + onDragEnter: (event: React.DragEvent) => { + event.preventDefault(); + dragCounter.current += 1; + setIsDragging(true); + }, + onDragOver: (event: React.DragEvent) => { + event.preventDefault(); + }, + onDragLeave: (event: React.DragEvent) => { + event.preventDefault(); + dragCounter.current = Math.max(0, dragCounter.current - 1); + if (dragCounter.current === 0) setIsDragging(false); + }, + onDrop: async (event: React.DragEvent) => { + event.preventDefault(); + dragCounter.current = 0; + setIsDragging(false); + if (event.dataTransfer.files.length > 0) { + await addFiles(event.dataTransfer.files); + } + } + }; + + return { isDragging, dropHandlers, addFiles }; +}