import React, { useEffect, useRef } from 'react'; import { HistoricalDose } from '../../types'; import { PillIcon, CheckCircleIcon, XCircleIcon, ClockIcon, } from '../icons/Icons'; interface HistoryModalProps { isOpen: boolean; onClose: () => void; history: { date: string; doses: HistoricalDose[] }[]; } const getStatusIcon = (status: HistoricalDose['status']) => { switch (status) { case 'TAKEN': return ( ); case 'MISSED': return ; default: return ( ); } }; const HistoryModal: React.FC = ({ isOpen, onClose, history, }) => { 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]); if (!isOpen) return null; const formatDate = (dateString: string) => { const date = new Date(dateString); const userTimezoneOffset = date.getTimezoneOffset() * 60000; return new Date(date.getTime() + userTimezoneOffset).toLocaleDateString( undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', } ); }; return (

Medication History

{history.length > 0 ? ( history.map(({ date, doses }) => (

{formatDate(date)}

    {doses.map(dose => (
  • {getStatusIcon(dose.status)}

    {dose.medication.name}

    {dose.medication.dosage}

    {dose.scheduledTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', })}

    {dose.status === 'TAKEN' && dose.takenAt && (

    Taken at{' '} {new Date(dose.takenAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', })}

    )} {dose.status === 'MISSED' && (

    Missed

    )}
  • ))}
)) ) : (

No medication history found.

History will appear here once you start tracking doses.

)}
); }; export default HistoryModal;