import React, { useState, useEffect } from 'react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, ReferenceLine } from 'recharts'; import { X, TrendingUp, TrendingDown, Activity, Heart, Wind, Thermometer } from 'lucide-react'; const API_BASE = `http://${window.location.hostname}:8000`; const PatientDetailModal = ({ patient, onClose }) => { const [vitalsHistory, setVitalsHistory] = useState([]); const [tierChanges, setTierChanges] = useState([]); const [loading, setLoading] = useState(true); const [selectedMetric, setSelectedMetric] = useState('all'); useEffect(() => { fetchPatientHistory(); }, [patient.patient_id]); const fetchPatientHistory = async () => { try { const response = await fetch(`${API_BASE}/api/patients/${patient.patient_id}/vitals-history?limit=200`); const data = await response.json(); setVitalsHistory(data.vitals_history || []); setTierChanges(data.tier_changes || []); setLoading(false); } catch (error) { console.error('Failed to fetch patient history:', error); setLoading(false); } }; const prepareChartData = () => { return vitalsHistory.slice().reverse().map((v, index) => ({ time: index, timeLabel: new Date(v.timestamp * 1000).toLocaleTimeString(), hr: v.hr_bpm, spo2: v.spo2, temp: v.temp_c, tier: v.tier })); }; const calculateTrend = (data, key) => { if (data.length < 2) return null; const recent = data.slice(-5); const first = recent[0][key]; const last = recent[recent.length - 1][key]; const change = last - first; return { direction: change > 0 ? 'up' : change < 0 ? 'down' : 'stable', value: Math.abs(change).toFixed(1) }; }; const chartData = prepareChartData(); const hrTrend = calculateTrend(chartData, 'hr'); const spo2Trend = calculateTrend(chartData, 'spo2'); const tempTrend = calculateTrend(chartData, 'temp'); if (loading) { return (

Loading patient data...

); } return (

{patient.name}

{patient.patient_id} {patient.band_id} Wait: {patient.wait_time_minutes} min
Heart Rate

{patient.last_hr}

bpm

{hrTrend && (

{hrTrend.direction === 'up' ? : hrTrend.direction === 'down' ? : null} {hrTrend.value}

)}
SpO₂

{patient.last_spo2}

%

{spo2Trend && (

{spo2Trend.direction === 'up' ? : spo2Trend.direction === 'down' ? : null} {spo2Trend.value}

)}
Temperature

{patient.last_temp.toFixed(1)}

°C

{tempTrend && (

{tempTrend.direction === 'up' ? : tempTrend.direction === 'down' ? : null} {tempTrend.value}

)}
{['all', 'hr', 'spo2', 'temp'].map(metric => ( ))}
{chartData.length > 0 ? (
{(selectedMetric === 'all' || selectedMetric === 'hr') && (

Heart Rate Over Time

)} {(selectedMetric === 'all' || selectedMetric === 'spo2') && (

Oxygen Saturation Over Time

)} {(selectedMetric === 'all' || selectedMetric === 'temp') && (

Temperature Over Time

)}
) : (

No vital signs history available yet

)} {tierChanges.length > 0 && (

Tier Change History

{tierChanges.map((change, index) => (
{new Date(change.change_time).toLocaleTimeString()} {change.old_tier} {change.new_tier} ({change.reason})
))}
)}

Total Readings

{vitalsHistory.length}

Priority Score

{patient.priority_score.toFixed(1)}

Current Tier

{patient.tier}

); }; export default PatientDetailModal;