From c263497ec8b05bc0f9254ed2f7ecb4dba6f67d4e Mon Sep 17 00:00:00 2001 From: Kostya Date: Thu, 15 Oct 2026 19:35:48 +0300 Subject: ui-web: add browser client for the Ex4 bonus A React + TypeScript single-page client served by Vite, working against the same servlets as the desktop client: login, the events screen with filters, details, order books and trading, and the account screen with deposit, holdings, ledger rows and the user directory. No file upload, as the exercise specifies. It looks like the desktop client on purpose: the three skins of theme.css as CSS variables, the four animations redone with SVG and keyframes, and the desktop's Nerd Fonts, which npm run dev copies from ui-fx instead of keeping a second copy in the repo. The shared chat sits in a drawer above the status bar, as on the desktop, on its own poll because chat lines don't move the market version. Vite forwards /guess-market to Tomcat on 8080, so the page and the API share an origin and the session cookie needs no CORS. The client polls /api/sync once a second and reloads only when the version moved. run-web.bat installs the packages and starts it on localhost:5173. package.sh stages the sources under build/dist/ex4, without node_modules. --- ui-web/src/Chart.tsx | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 ui-web/src/Chart.tsx (limited to 'ui-web/src/Chart.tsx') diff --git a/ui-web/src/Chart.tsx b/ui-web/src/Chart.tsx new file mode 100644 index 0000000..4caf3a5 --- /dev/null +++ b/ui-web/src/Chart.tsx @@ -0,0 +1,94 @@ +import { EventDetail, Ledger, num } from './api'; + +export type Point = [number, number]; + +const seconds = (at: string) => { + const t = Date.parse(at.replace(' ', 'T')); + return Number.isNaN(t) ? 0 : t / 1000; +}; +const now = () => Date.now() / 1000; +const clock = (t: number) => new Date(t * 1000).toTimeString().slice(0, 8); + +export function priceSeries(detail: EventDetail): Point[] { + const first = detail.summary.optionNames[0]; + const current = num(detail.state.markets[0]?.price) || 0.5; + const trades = [...detail.history].reverse(); + if (trades.length === 0) { + return [ + [now() - 60, current], + [now(), current], + ]; + } + const start = seconds(trades[0].at) || now(); + const points: Point[] = [[start - 60, 0.5]]; + for (const t of trades) { + const quantity = num(t.quantity); + const unit = quantity > 0 ? num(t.pricePaid) / quantity : current; + const price = t.optionName === first ? unit : 1 - unit; + points.push([seconds(t.at) || start, Math.max(0, Math.min(1, price))]); + } + return points; +} + +export function balanceSeries(ledger: Ledger): Point[] { + const rows = ledger.entries; + const balance = num(ledger.balance); + if (rows.length === 0) { + return [ + [now() - 60, balance], + [now(), balance], + ]; + } + const start = seconds(rows[0].at) || now(); + const points: Point[] = [[start - 60, Math.max(0, num(rows[0].balanceAfter) - num(rows[0].amount))]]; + for (const row of rows) { + points.push([seconds(row.at) || start, num(row.balanceAfter)]); + } + return points; +} + +const TICKS = [0, 1, 2, 3, 4]; + +export function LineChart(props: { + series: { points: Point[]; tone: 'accent' | 'yes' | 'no' }[]; + range?: [number, number]; + height?: number; +}) { + const all = props.series.flatMap((s) => s.points); + const t0 = Math.min(...all.map((p) => p[0])); + const t1 = Math.max(...all.map((p) => p[0]), t0 + 60); + let [lo, hi] = props.range ?? [Math.min(...all.map((p) => p[1]), 0), Math.max(...all.map((p) => p[1]))]; + if (hi <= lo) hi = lo + 1; + if (!props.range) hi = lo + Math.ceil(((hi - lo) * 1.1) / 4) * 4; // headroom, in four whole steps + + const x = (t: number) => ((t - t0) / (t1 - t0)) * 100; + const y = (v: number) => 100 - ((v - lo) / (hi - lo)) * 100; + const label = (v: number) => (hi - lo <= 1 ? v.toFixed(2).replace(/\.?0+$/, '') || '0' : Math.round(v).toLocaleString()); + + return ( +
+
+ {TICKS.map((i) => ( + {label(hi - ((hi - lo) * i) / 4)} + ))} +
+ + {TICKS.map((i) => ( + + ))} + {props.series.map((s, i) => ( + `${j ? 'L' : 'M'}${x(p[0]).toFixed(2)} ${y(p[1]).toFixed(2)}`).join(' ')} + /> + ))} + +
+ {TICKS.map((i) => ( + {clock(t0 + ((t1 - t0) * i) / 4)} + ))} +
+
+ ); +} -- cgit v1.2.3