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...
{patient.last_hr}
bpm
{hrTrend && (
{hrTrend.direction === 'up' ?
{patient.last_spo2}
%
{spo2Trend && (
{spo2Trend.direction === 'up' ?
{patient.last_temp.toFixed(1)}
°C
{tempTrend && (
{tempTrend.direction === 'up' ?
No vital signs history available yet
Total Readings
{vitalsHistory.length}
Priority Score
{patient.priority_score.toFixed(1)}
Current Tier
{patient.tier}