"use client"; import { ArrowDown, ArrowUp } from "lucide-react"; import { useMemo } from "react"; import { useTranslation } from "react-i18next"; import { Area, AreaChart } from "@/components/charts/area-chart"; import { Grid } from "@/components/charts/grid"; import { ChartTooltip } from "@/components/charts/tooltip"; import { XAxis } from "@/components/charts/x-axis"; import { Badge } from "@/components/ui/badge"; import { Card } from "@/components/ui/card"; import type { LabCardData } from "@/lib/ai-chat"; import type { LabFlag } from "@/lib/patients"; import { cn } from "@/lib/utils"; type BadgeVariant = "default" | "secondary" | "destructive" | "outline"; const labFlagVariant: Record = { normal: "outline", low: "secondary", high: "secondary", critical: "destructive", }; // Plot the headline lab series. labTrend.points are most-recent-last numbers; we // synthesise evenly spaced dates ending today purely so the date x-axis spaces // the readings — the values are what matter. function toChartData(points: number[]): { date: Date; value: number }[] { const today = new Date(); return points.map((value, i) => { const date = new Date(today); date.setDate(today.getDate() - (points.length - 1 - i)); return { date, value }; }); } // The flag of the most recent reading of the headline lab, for the corner badge. function headlineFlag(data: LabCardData): LabFlag | null { const matching = data.labs.filter((l) => l.name === data.labTrend.label); const last = matching[matching.length - 1] ?? data.labs[data.labs.length - 1]; return last?.flag ?? null; } export function LabChartCard({ data }: { data: LabCardData }) { const { t } = useTranslation(); const chartData = useMemo( () => toChartData(data.labTrend.points), [data.labTrend.points], ); const flag = headlineFlag(data); const latest = data.labTrend.points[data.labTrend.points.length - 1]; return (
{data.name}

{data.labTrend.label} · {data.labTrend.unit}

{/* High/low indicator, top-right corner. */} {flag && flag !== "normal" ? ( {flag === "low" ? ( ) : ( )} {t(`chat.labCard.flags.${flag}`)} ) : ( {latest} )}
{data.labs.length > 0 ? ( ) : null}
); }