frontend: scenarios list + detail pages with persisted fan chart
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled
/scenarios — table of all scenarios with filter chips (all/cartesian/ user). Cartesian scenarios get a neutral badge; user-defined get an emerald accent. Empty-state nudges the user to run /recompute. /scenarios/:id — params summary + the latest persisted MC projection. Reuses FanChart so chart code is shared with /what-if. 404 on the projection endpoint is treated as "no run yet" (don't retry); other errors surface normally. Nav grew a Scenarios tab. typecheck + 5 tests + build pass. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
bb74bc0add
commit
d2fd765fe0
3 changed files with 286 additions and 0 deletions
137
frontend/src/pages/ScenarioDetail.tsx
Normal file
137
frontend/src/pages/ScenarioDetail.tsx
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
/**
|
||||
* Scenario detail — params + the latest persisted MC projection.
|
||||
*
|
||||
* Reuses FanChart from the What-If page. If the scenario has no MC run
|
||||
* yet, prompts the user to run /recompute.
|
||||
*/
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { Link, useParams } from 'react-router-dom';
|
||||
|
||||
import { api } from '@/api/client';
|
||||
import { ApiError } from '@/api/client';
|
||||
import { FanChart } from '@/components/FanChart';
|
||||
import { gbp, pct } from '@/lib/format';
|
||||
|
||||
export function ScenarioDetail() {
|
||||
const params = useParams<{ id: string }>();
|
||||
const id = Number(params.id);
|
||||
|
||||
const scen = useQuery({
|
||||
queryKey: ['scenarios', id],
|
||||
queryFn: () => api.scenarios.get(id),
|
||||
enabled: Number.isFinite(id),
|
||||
});
|
||||
const proj = useQuery({
|
||||
queryKey: ['scenarios', id, 'projection'],
|
||||
queryFn: () => api.scenarios.projection(id),
|
||||
enabled: Number.isFinite(id),
|
||||
retry: (count, err) => {
|
||||
// Don't retry the 404 — it's the "no run yet" empty state.
|
||||
if (err instanceof ApiError && err.status === 404) return false;
|
||||
return count < 2;
|
||||
},
|
||||
});
|
||||
|
||||
if (!Number.isFinite(id)) {
|
||||
return <p className="text-red-700">Invalid scenario id.</p>;
|
||||
}
|
||||
|
||||
if (scen.isLoading) return <p className="text-slate-500">Loading…</p>;
|
||||
if (scen.isError || !scen.data) {
|
||||
return (
|
||||
<div className="rounded-md border border-red-200 bg-red-50 p-4 text-red-800 text-sm">
|
||||
Couldn't load scenario {id}.
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const s = scen.data;
|
||||
const projection = proj.data;
|
||||
const projection404 = proj.isError && proj.error instanceof ApiError && proj.error.status === 404;
|
||||
|
||||
return (
|
||||
<section className="space-y-6">
|
||||
<div className="text-sm">
|
||||
<Link to="/scenarios" className="text-slate-500 hover:text-slate-900">
|
||||
← Scenarios
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
<header>
|
||||
<h1 className="text-3xl font-semibold tracking-tight">{s.name ?? s.external_id}</h1>
|
||||
<p className="text-sm text-slate-500 mt-1">
|
||||
{s.kind} · {s.jurisdiction} · {s.strategy} · leave UK y{s.leave_uk_year} ·{' '}
|
||||
{s.glide_path} glide · {s.horizon_years}y horizon
|
||||
</p>
|
||||
{s.description && <p className="text-sm text-slate-600 mt-2">{s.description}</p>}
|
||||
</header>
|
||||
|
||||
<div className="grid grid-cols-2 md:grid-cols-4 gap-3">
|
||||
<Stat label="Spending" value={gbp(s.spending_gbp)} />
|
||||
<Stat label="NW seed" value={gbp(s.nw_seed_gbp)} />
|
||||
<Stat label="Annual savings" value={gbp(s.savings_per_year_gbp)} />
|
||||
<Stat label="Horizon" value={`${s.horizon_years}y`} />
|
||||
</div>
|
||||
|
||||
{projection ? (
|
||||
<>
|
||||
<div className="grid grid-cols-2 md:grid-cols-4 gap-3">
|
||||
<Stat label="Success rate" value={pct(projection.success_rate)} accent />
|
||||
<Stat label="Median ending NW" value={gbp(projection.p50_ending_gbp)} />
|
||||
<Stat label="P10 ending" value={gbp(projection.p10_ending_gbp)} />
|
||||
<Stat label="P90 ending" value={gbp(projection.p90_ending_gbp)} />
|
||||
<Stat label="Median lifetime tax" value={gbp(projection.median_lifetime_tax_gbp)} />
|
||||
<Stat
|
||||
label="Median years to ruin"
|
||||
value={projection.median_years_to_ruin ?? 'never'}
|
||||
/>
|
||||
<Stat label="MC paths" value={projection.n_paths.toLocaleString()} />
|
||||
<Stat label="Run at" value={new Date(projection.run_at).toLocaleString()} />
|
||||
</div>
|
||||
|
||||
<div className="rounded-lg border border-slate-200 bg-white p-5">
|
||||
<h2 className="text-lg font-semibold mb-2">Portfolio fan</h2>
|
||||
<FanChart yearly={projection.yearly} height={420} showWithdrawal />
|
||||
</div>
|
||||
</>
|
||||
) : projection404 ? (
|
||||
<div className="rounded-lg border border-slate-200 bg-white p-8 text-center text-slate-500">
|
||||
<p className="font-medium text-slate-700">No projection yet.</p>
|
||||
<p className="text-sm mt-2">
|
||||
Run <code className="px-1">python -m fire_planner recompute-all</code> or{' '}
|
||||
<code>POST /recompute</code> to fill in MC projections for all scenarios.
|
||||
</p>
|
||||
</div>
|
||||
) : proj.isLoading ? (
|
||||
<p className="text-slate-500">Loading projection…</p>
|
||||
) : (
|
||||
<div className="rounded-md border border-red-200 bg-red-50 p-4 text-red-800 text-sm">
|
||||
{String((proj.error as Error)?.message ?? proj.error)}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
function Stat({
|
||||
label,
|
||||
value,
|
||||
accent,
|
||||
}: {
|
||||
label: string;
|
||||
value: string | number;
|
||||
accent?: boolean;
|
||||
}) {
|
||||
return (
|
||||
<div className="rounded-md border border-slate-200 bg-white p-3">
|
||||
<div className="text-xs uppercase tracking-wide text-slate-500">{label}</div>
|
||||
<div
|
||||
className={`text-xl font-semibold tabular-nums mt-1 ${
|
||||
accent ? 'text-emerald-700' : ''
|
||||
}`}
|
||||
>
|
||||
{value}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue