import React, { useMemo, useEffect, useRef } from 'react'; import { Medication } from '../../types'; import { TrashIcon, EditIcon, getMedicationIcon } from '../icons/Icons'; interface ManageMedicationsModalProps { isOpen: boolean; onClose: () => void; medications: Medication[]; // FIX: Changed onDelete to expect the full medication object to match the parent's handler. onDelete: (medication: Medication) => void; onEdit: (medication: Medication) => void; } const ManageMedicationsModal: React.FC = ({ isOpen, onClose, medications, onDelete, onEdit, }) => { const modalRef = useRef(null); const closeButtonRef = useRef(null); useEffect(() => { if (isOpen) { setTimeout(() => closeButtonRef.current?.focus(), 100); } }, [isOpen]); useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') onClose(); }; if (isOpen) { window.addEventListener('keydown', handleKeyDown); } return () => window.removeEventListener('keydown', handleKeyDown); }, [isOpen, onClose]); useEffect(() => { if (!isOpen || !modalRef.current) return; const focusableElements = modalRef.current.querySelectorAll( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' ); const firstElement = focusableElements[0] as HTMLElement; const lastElement = focusableElements[ focusableElements.length - 1 ] as HTMLElement; const handleTabKey = (e: KeyboardEvent) => { if (e.key !== 'Tab') return; if (e.shiftKey) { if (document.activeElement === firstElement) { lastElement.focus(); e.preventDefault(); } } else { if (document.activeElement === lastElement) { firstElement.focus(); e.preventDefault(); } } }; document.addEventListener('keydown', handleTabKey); return () => document.removeEventListener('keydown', handleTabKey); }, [isOpen]); const sortedMedications = useMemo( () => [...medications].sort((a, b) => a.name.localeCompare(b.name)), [medications] ); const handleDeleteConfirmation = (medication: Medication) => { if (window.confirm(`Are you sure you want to delete ${medication.name}?`)) { // FIX: Pass the whole medication object to the onDelete handler. onDelete(medication); } }; if (!isOpen) return null; return (

Manage Medications

{sortedMedications.length > 0 ? (
    {sortedMedications.map(med => { const MedicationIcon = getMedicationIcon(med.icon); return ( // FIX: The Medication type has `_id`, not `id`. Used for the key.
  • {med.name}

    {med.dosage} • {med.frequency}

    {med.notes && (

    Note: "{med.notes}"

    )}
  • ); })}
) : (

No medications have been added yet.

)}
); }; export default ManageMedicationsModal;