import { ReactNode, useContext, useEffect, useRef, useState } from 'react'; import { Box, useTheme } from '@mui/material'; import Typography from '@mui/material/Typography'; import InputHeader from '../InputHeader'; import InputFooter from './InputFooter'; import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext'; import { isArray } from 'lodash'; import MusicNoteIcon from '@mui/icons-material/MusicNote'; interface MultiAudioInputComponentProps { accept: string[]; title?: string; type: 'audio'; value: MultiAudioInput[]; onChange: (file: MultiAudioInput[]) => void; } export interface MultiAudioInput { file: File; order: number; } export default function ToolMultipleAudioInput({ value, onChange, accept, title, type }: MultiAudioInputComponentProps) { const fileInputRef = useRef(null); const handleFileChange = (event: React.ChangeEvent) => { const files = event.target.files; if (files) onChange([ ...value, ...Array.from(files).map((file) => ({ file, order: value.length })) ]); }; const handleImportClick = () => { fileInputRef.current?.click(); }; function handleClear() { onChange([]); } function fileNameTruncate(fileName: string) { const maxLength = 15; if (fileName.length > maxLength) { return fileName.slice(0, maxLength) + '...'; } return fileName; } const sortList = () => { const list = [...value]; list.sort((a, b) => a.order - b.order); onChange(list); }; const reorderList = (sourceIndex: number, destinationIndex: number) => { if (destinationIndex === sourceIndex) { return; } const list = [...value]; if (destinationIndex === 0) { list[sourceIndex].order = list[0].order - 1; sortList(); return; } if (destinationIndex === list.length - 1) { list[sourceIndex].order = list[list.length - 1].order + 1; sortList(); return; } if (destinationIndex < sourceIndex) { list[sourceIndex].order = (list[destinationIndex].order + list[destinationIndex - 1].order) / 2; sortList(); return; } list[sourceIndex].order = (list[destinationIndex].order + list[destinationIndex + 1].order) / 2; sortList(); }; return ( {value?.length ? ( value.map((file, index) => ( {fileNameTruncate(file.file.name)} { const updatedFiles = value.filter((_, i) => i !== index); onChange(updatedFiles); }} > ✖ )) ) : ( No files selected )} ); }