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
|
|
@ -3,6 +3,8 @@ import { NavLink, Route, Routes, Link } from 'react-router-dom';
|
|||
|
||||
import { api } from '@/api/client';
|
||||
import { Dashboard } from '@/pages/Dashboard';
|
||||
import { ScenarioDetail } from '@/pages/ScenarioDetail';
|
||||
import { Scenarios } from '@/pages/Scenarios';
|
||||
import { WhatIf } from '@/pages/WhatIf';
|
||||
|
||||
export function App() {
|
||||
|
|
@ -18,6 +20,7 @@ export function App() {
|
|||
</Link>
|
||||
<nav className="flex gap-4 text-sm">
|
||||
<NavTab to="/">Dashboard</NavTab>
|
||||
<NavTab to="/scenarios">Scenarios</NavTab>
|
||||
<NavTab to="/what-if">What if</NavTab>
|
||||
</nav>
|
||||
</div>
|
||||
|
|
@ -36,6 +39,8 @@ export function App() {
|
|||
<main className="flex-1 max-w-7xl w-full mx-auto px-6 py-8">
|
||||
<Routes>
|
||||
<Route path="/" element={<Dashboard />} />
|
||||
<Route path="/scenarios" element={<Scenarios />} />
|
||||
<Route path="/scenarios/:id" element={<ScenarioDetail />} />
|
||||
<Route path="/what-if" element={<WhatIf />} />
|
||||
</Routes>
|
||||
</main>
|
||||
|
|
|
|||
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>
|
||||
);
|
||||
}
|
||||
144
frontend/src/pages/Scenarios.tsx
Normal file
144
frontend/src/pages/Scenarios.tsx
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
/**
|
||||
* Scenarios list — both Cartesian (engine-generated) and user-defined.
|
||||
* Filter chips at the top; each row links to a detail page.
|
||||
*
|
||||
* The Cartesian set is whatever the latest /recompute produced (default
|
||||
* 120 scenarios). User scenarios survive recomputes.
|
||||
*/
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { useState } from 'react';
|
||||
|
||||
import { api, type Scenario } from '@/api/client';
|
||||
import { gbp } from '@/lib/format';
|
||||
|
||||
type Filter = 'all' | 'cartesian' | 'user';
|
||||
|
||||
export function Scenarios() {
|
||||
const [filter, setFilter] = useState<Filter>('all');
|
||||
const scenarios = useQuery({
|
||||
queryKey: ['scenarios', filter],
|
||||
queryFn: () => api.scenarios.list(filter === 'all' ? undefined : filter),
|
||||
});
|
||||
|
||||
return (
|
||||
<section className="space-y-6">
|
||||
<header className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-semibold tracking-tight">Scenarios</h1>
|
||||
<p className="text-sm text-slate-500">
|
||||
Persisted plans. Cartesian rebuilt from <code>/recompute</code>; user-defined survive.
|
||||
</p>
|
||||
</div>
|
||||
<FilterChips value={filter} onChange={setFilter} />
|
||||
</header>
|
||||
|
||||
{scenarios.isLoading ? (
|
||||
<p className="text-slate-500">Loading…</p>
|
||||
) : scenarios.isError ? (
|
||||
<ErrorBox error={scenarios.error} />
|
||||
) : scenarios.data && scenarios.data.length > 0 ? (
|
||||
<ScenarioTable scenarios={scenarios.data} />
|
||||
) : (
|
||||
<EmptyState filter={filter} />
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
function FilterChips({ value, onChange }: { value: Filter; onChange: (v: Filter) => void }) {
|
||||
const opts: { key: Filter; label: string }[] = [
|
||||
{ key: 'all', label: 'All' },
|
||||
{ key: 'cartesian', label: 'Cartesian' },
|
||||
{ key: 'user', label: 'User' },
|
||||
];
|
||||
return (
|
||||
<div className="inline-flex rounded-md border border-slate-200 bg-white p-0.5">
|
||||
{opts.map((o) => (
|
||||
<button
|
||||
key={o.key}
|
||||
type="button"
|
||||
onClick={() => onChange(o.key)}
|
||||
className={`px-3 py-1 text-sm rounded ${
|
||||
value === o.key
|
||||
? 'bg-slate-900 text-white'
|
||||
: 'text-slate-600 hover:text-slate-900'
|
||||
}`}
|
||||
>
|
||||
{o.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ScenarioTable({ scenarios }: { scenarios: Scenario[] }) {
|
||||
return (
|
||||
<div className="rounded-lg border border-slate-200 bg-white overflow-hidden">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-slate-50 text-slate-600 text-xs uppercase tracking-wide">
|
||||
<tr>
|
||||
<th className="text-left px-4 py-2">Name / ID</th>
|
||||
<th className="text-left px-4 py-2">Kind</th>
|
||||
<th className="text-left px-4 py-2">Jurisdiction</th>
|
||||
<th className="text-left px-4 py-2">Strategy</th>
|
||||
<th className="text-left px-4 py-2">Leave UK</th>
|
||||
<th className="text-left px-4 py-2">Glide</th>
|
||||
<th className="text-right px-4 py-2">Spending</th>
|
||||
<th className="text-right px-4 py-2">NW seed</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{scenarios.map((s) => (
|
||||
<tr key={s.id} className="border-t border-slate-100 hover:bg-slate-50">
|
||||
<td className="px-4 py-2">
|
||||
<Link to={`/scenarios/${s.id}`} className="text-blue-700 hover:underline">
|
||||
{s.name ?? s.external_id}
|
||||
</Link>
|
||||
{s.name && (
|
||||
<div className="text-xs text-slate-400">{s.external_id}</div>
|
||||
)}
|
||||
</td>
|
||||
<td className="px-4 py-2">
|
||||
<KindBadge kind={s.kind} />
|
||||
</td>
|
||||
<td className="px-4 py-2 capitalize">{s.jurisdiction}</td>
|
||||
<td className="px-4 py-2">{s.strategy}</td>
|
||||
<td className="px-4 py-2">y{s.leave_uk_year}</td>
|
||||
<td className="px-4 py-2">{s.glide_path}</td>
|
||||
<td className="px-4 py-2 text-right tabular-nums">{gbp(s.spending_gbp)}</td>
|
||||
<td className="px-4 py-2 text-right tabular-nums">{gbp(s.nw_seed_gbp)}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function KindBadge({ kind }: { kind: Scenario['kind'] }) {
|
||||
const styles = kind === 'user' ? 'bg-emerald-100 text-emerald-800' : 'bg-slate-100 text-slate-600';
|
||||
return (
|
||||
<span className={`text-xs px-2 py-0.5 rounded ${styles}`}>{kind}</span>
|
||||
);
|
||||
}
|
||||
|
||||
function EmptyState({ filter }: { filter: Filter }) {
|
||||
return (
|
||||
<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 scenarios{filter !== 'all' ? ` (${filter})` : ''}.</p>
|
||||
<p className="text-sm mt-2">
|
||||
Run <code className="px-1">python -m fire_planner recompute-all</code> to populate the
|
||||
Cartesian set, or create a user scenario via <code>POST /scenarios</code>.
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ErrorBox({ error }: { error: unknown }) {
|
||||
return (
|
||||
<div className="rounded-md border border-red-200 bg-red-50 p-4 text-red-800 text-sm">
|
||||
{String((error as Error)?.message ?? error)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue