import React, { useState, useEffect, useRef } from 'react'; import { X } from 'lucide-react'; interface ImportListModalProps { isOpen: boolean; onClose: () => void; onImport: (selectedLists: { id: string, title: string }[]) => void; provider: 'google' | 'apple' | 'outlook' | 'synology' | null; lists: { id: string, title: string }[]; isLoading: boolean; } export const ImportListModal: React.FC = ({ isOpen, onClose, onImport, provider, lists, isLoading }) => { const [selectedIds, setSelectedIds] = useState([]); const firstFocusRef = useRef(null); const dialogRef = useRef(null); const providerLabel = provider === 'google' ? 'Google Tasks' : provider === 'outlook' ? 'Microsoft To-Do' : 'Apple Reminders'; // Reset selection and focus first element when opening useEffect(() => { if (isOpen) { setSelectedIds([]); setTimeout(() => firstFocusRef.current?.focus(), 50); } }, [isOpen]); // Focus trap useEffect(() => { if (!isOpen) return; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose(); return; } if (e.key !== 'Tab') return; const dialog = dialogRef.current; if (!dialog) return; const focusable = Array.from( dialog.querySelectorAll('button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])') ); if (focusable.length === 0) return; const first = focusable[0]; const last = focusable[focusable.length - 1]; if (e.shiftKey) { if (document.activeElement === first) { e.preventDefault(); last.focus(); } } else { if (document.activeElement === last) { e.preventDefault(); first.focus(); } } }; document.addEventListener('keydown', handleKeyDown); return () => document.removeEventListener('keydown', handleKeyDown); }, [isOpen, onClose]); if (!isOpen) return null; const toggleSelection = (id: string) => { if (selectedIds.includes(id)) { setSelectedIds(selectedIds.filter(lid => lid !== id)); } else { setSelectedIds([...selectedIds, id]); } }; const handleSelectAll = () => { if (selectedIds.length === lists.length) { setSelectedIds([]); } else { setSelectedIds(lists.map(l => l.id)); } }; return ( ); };