import React, { ReactNode, useState, useRef } from 'react'; import { useClickOutside } from '@/app/hooks/useClickOutside'; import { SelectedFile } from './UploadSectionFile'; import { useTranslations } from 'next-intl'; interface DropDownItem { file: SelectedFile; convertTo: string; } interface DropDownListProps { children: ReactNode; value: string | null | undefined; callBack: (value: DropDownItem) => void; translatedValue?: string | null | undefined; selectedFiles: SelectedFile[]; } interface LiElementProps extends React.LiHTMLAttributes { 'data-file'?: string; 'data-convert-to'?: string; } export default function DropDownListForImageConvert({ children, value, callBack, translatedValue, selectedFiles }: DropDownListProps) { const [isOpen, setIsOpen] = useState(false); const dropdownRef = useRef(null); const t = useTranslations('Global') useClickOutside(dropdownRef, () => { setIsOpen(false); }, isOpen ); const getDisplayValue = (): string => { if (translatedValue) return translatedValue; return value ? value : t('do-not-convert'); }; const findFileByName = (fileName: string): SelectedFile | undefined => { return selectedFiles.find(file => file.name === fileName); }; const enhancedChildren = React.Children.map(children, (child, index) => { if (React.isValidElement(child)) { const fileName = child.props['data-file']; const convertTo = child.props['data-convert-to']; const isCurrent = value === convertTo; return React.cloneElement(child, { onClick: () => { if (fileName && convertTo) { const file = findFileByName(fileName); if (file) { callBack({ file, convertTo }); } else { console.error(`Файл с именем ${fileName} не найден`); } setIsOpen(false); } }, className: `dropdown-item ${isCurrent ? "current" : ""}`, key: index, }); } return child; }); return (
{isOpen && (
    {enhancedChildren}
)}
); }