|
| 1 | +import { AlertTriangle, BarChart3, Brain, CheckCircle2 } from "lucide-react"; |
| 2 | +import { useMemo } from "react"; |
| 3 | +import { |
| 4 | + Bar, |
| 5 | + BarChart, |
| 6 | + CartesianGrid, |
| 7 | + Cell, |
| 8 | + ReferenceLine, |
| 9 | + ResponsiveContainer, |
| 10 | + Tooltip, |
| 11 | + XAxis, |
| 12 | + YAxis, |
| 13 | +} from "recharts"; |
| 14 | + |
| 15 | +import { featureLabel, formatFeatureValue } from "@/lib/labels"; |
| 16 | +import type { PredictionResult } from "@/types/healthcare"; |
| 17 | + |
| 18 | +import { DataTable, NarrativePanel, PanelHeading } from "./shared-ui"; |
| 19 | +import type { ChartFeature } from "./types"; |
| 20 | + |
| 21 | +export function Assessment({ result }: { result: PredictionResult }) { |
| 22 | + const topFeatures = useMemo(() => { |
| 23 | + return result.feature_explanation.features |
| 24 | + .slice() |
| 25 | + .sort((a, b) => b.magnitude - a.magnitude) |
| 26 | + .slice(0, 8) |
| 27 | + .map((feature) => ({ |
| 28 | + ...feature, |
| 29 | + label: featureLabel(feature.feature), |
| 30 | + valueLabel: formatFeatureValue(feature.feature, feature.value), |
| 31 | + directionLabel: feature.impact > 0 ? "Raises risk" : "Lowers risk", |
| 32 | + })); |
| 33 | + }, [result.feature_explanation.features]); |
| 34 | + |
| 35 | + return ( |
| 36 | + <section className="grid gap-5 xl:grid-cols-[0.95fr_1.05fr]"> |
| 37 | + <div className="space-y-5"> |
| 38 | + <NarrativePanel title="Explanation" icon={<Brain className="h-4 w-4" />} text={result.explanation} /> |
| 39 | + <NarrativePanel |
| 40 | + title="Recommendation" |
| 41 | + icon={<CheckCircle2 className="h-4 w-4" />} |
| 42 | + text={result.recommendation} |
| 43 | + /> |
| 44 | + {result.safety.alerts.length > 0 ? ( |
| 45 | + <div className="rounded-lg border border-danger/40 bg-danger/10 p-4 text-sm text-[#ffc1bc]"> |
| 46 | + <div className="mb-2 flex items-center gap-2 font-semibold text-ink"> |
| 47 | + <AlertTriangle className="h-4 w-4 text-danger" /> |
| 48 | + Safety Alerts |
| 49 | + </div> |
| 50 | + <ul className="space-y-1"> |
| 51 | + {result.safety.alerts.map((alert) => ( |
| 52 | + <li key={alert}>{alert}</li> |
| 53 | + ))} |
| 54 | + </ul> |
| 55 | + </div> |
| 56 | + ) : null} |
| 57 | + </div> |
| 58 | + <FeatureImpactPanel features={topFeatures} method={result.feature_explanation.method} /> |
| 59 | + </section> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +function FeatureImpactPanel({ features, method }: { features: ChartFeature[]; method: string }) { |
| 64 | + return ( |
| 65 | + <div className="console-panel rounded-lg p-5"> |
| 66 | + <PanelHeading icon={<BarChart3 className="h-4 w-4" />} title="Feature Impact" /> |
| 67 | + <div className="mt-4 h-80"> |
| 68 | + <ResponsiveContainer width="100%" height="100%"> |
| 69 | + <BarChart data={features} layout="vertical" margin={{ top: 8, right: 18, bottom: 8, left: 28 }}> |
| 70 | + <CartesianGrid stroke="rgba(255,255,255,0.08)" horizontal={false} /> |
| 71 | + <XAxis type="number" stroke="#9aa4b2" tickLine={false} axisLine={false} /> |
| 72 | + <YAxis |
| 73 | + type="category" |
| 74 | + dataKey="label" |
| 75 | + width={135} |
| 76 | + stroke="#9aa4b2" |
| 77 | + tickLine={false} |
| 78 | + axisLine={false} |
| 79 | + tick={{ fontSize: 12 }} |
| 80 | + /> |
| 81 | + <Tooltip |
| 82 | + cursor={{ fill: "rgba(255,255,255,0.04)" }} |
| 83 | + formatter={(value) => [Number(value).toFixed(4), "Impact"]} |
| 84 | + contentStyle={{ |
| 85 | + background: "#101318", |
| 86 | + border: "1px solid rgba(255,255,255,0.1)", |
| 87 | + borderRadius: 8, |
| 88 | + }} |
| 89 | + /> |
| 90 | + <ReferenceLine x={0} stroke="rgba(244,246,248,0.45)" strokeDasharray="3 3" /> |
| 91 | + <Bar dataKey="impact" radius={[0, 4, 4, 0]}> |
| 92 | + {features.map((feature) => ( |
| 93 | + <Cell key={feature.feature} fill={feature.impact >= 0 ? "#d05245" : "#2f9b6a"} /> |
| 94 | + ))} |
| 95 | + </Bar> |
| 96 | + </BarChart> |
| 97 | + </ResponsiveContainer> |
| 98 | + </div> |
| 99 | + <div className="mt-4 flex items-center justify-between gap-3 text-xs text-muted"> |
| 100 | + <span>Method: {method}</span> |
| 101 | + <span>Top {features.length} by absolute impact</span> |
| 102 | + </div> |
| 103 | + <DataTable |
| 104 | + rows={features.map((feature) => ({ |
| 105 | + Feature: feature.label, |
| 106 | + Value: feature.valueLabel, |
| 107 | + Direction: feature.directionLabel, |
| 108 | + Impact: feature.impact.toFixed(4), |
| 109 | + }))} |
| 110 | + compact |
| 111 | + /> |
| 112 | + </div> |
| 113 | + ); |
| 114 | +} |
0 commit comments