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/Users.tsx | 298 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 298 insertions(+) create mode 100644 ui-web/src/Users.tsx (limited to 'ui-web/src/Users.tsx') diff --git a/ui-web/src/Users.tsx b/ui-web/src/Users.tsx new file mode 100644 index 0000000..b2593d9 --- /dev/null +++ b/ui-web/src/Users.tsx @@ -0,0 +1,298 @@ +import { useState } from 'react'; +import { api, EventSummary, feeLabel, MarketData, mechanismLabel, money, num, same, SEP } from './api'; +import type { Act } from './App'; +import { balanceSeries, LineChart } from './Chart'; +import { Sparkline } from './graphics'; + +export function Users(props: { + data: MarketData; + user: string; + selected: EventSummary | undefined; + onSelect: (key: string) => void; + act: Act; + onDeposit: () => void; +}) { + const { data, user, selected } = props; + const account = data.account; + const balance = num(account.balance); + + const myEvents = data.events.flatMap((e) => { + const me = data.details[e.key]?.participants.find((p) => same(p.user, user)); + const owner = same(e.marketMaker, user); + return owner || (me && (me.yes > 0 || me.no > 0)) ? [{ e, me, owner }] : []; + }); + const isMm = myEvents.some((row) => row.owner); + + return ( + <> +
+
+ User + Balance +
+ {/* A directory of everyone on the server; you can only ever act as yourself */} +
+ {data.users.map((u) => { + const me = same(u.username, user); + return ( +
+ + + {u.username} + + + {u.marketMaker ? 'Market Maker' : 'Trader'} + {me ? ' (you)' : u.online ? '' : ' · offline'} + + + {money(u.balance)} +
+ ); + })} +
+ +
+ +
+
+
+
+

{user}

+ + {account.blocked ? 'BLOCKED' : isMm ? 'MARKET MAKER' : 'TRADER'} + +
+

Active in {myEvents.length} events

+
+ +
+
Balance
+
{money(account.balance)}
+
+
+ +
+ {account.blocked && ( +
+ BLOCKED + + This account reached a negative balance. All trading actions are disabled until you deposit enough + funds to bring it back above zero. + +
+ )} + +
+
+ Account balance over time + + every trade, fee and payout + +
+ +
+ +
+
Events participation / owner
+
+
+ Event + Role + Type + YES + NO + P/L +
+ {myEvents.map(({ e, me, owner }) => { + const pnl = e.status === 'SETTLED' ? (me?.pnl ?? '$0.00') : '—'; + return ( +
props.onSelect(e.key)} + > + {e.name} + {owner ? 'Market Maker' : 'Trader'} + {mechanismLabel(e.mechanism)} + {me?.yes ?? 0} + {me?.no ?? 0} + {pnl} +
+ ); + })} + {myEvents.length === 0 &&
You are not part of any event yet
} +
+
+ + {/* key: a different event starts with a fresh form */} + {selected ? ( + + ) : ( +
+ Select an event to trade. +
+ )} + +
+
+ Account activity + + newest first + +
+
+
+ Time + Type + Details + Amount + Balance +
+ {/* seq is not unique across entries, so the position is the key */} + {[...account.entries].reverse().map((row, i) => ( +
+ {row.at} + {row.type} + {row.note} + {row.amount} + {row.balanceAfter} +
+ ))} + {account.entries.length === 0 && ( +
Nothing yet. Deposit funds to start trading.
+ )} +
+
+
+
+ + ); +} + +function TradePanel(props: { data: MarketData; user: string; act: Act; event: EventSummary }) { + const { data, user, event: e } = props; + const detail = data.details[e.key]; + const markets = detail?.state.markets ?? []; + const me = detail?.participants.find((p) => same(p.user, user)); + const held = [me?.yes ?? 0, me?.no ?? 0]; + const isBook = e.mechanism === 'ORDER_BOOK'; + + const [option, setOption] = useState(0); + const [side, setSide] = useState<'BUY' | 'SELL'>('BUY'); + const [quantity, setQuantity] = useState('50'); + const [price, setPrice] = useState('0.62'); + + const qty = Number(quantity); + const qtyOk = Number.isInteger(qty) && qty > 0; + const unit = isBook ? Number(price) : num(markets[option]?.price); + const priceOk = unit > 0; + const cost = qty * unit; + + let hint: string; + let allowed = false; + if (!qtyOk) hint = 'Shares must be a positive whole number.'; + else if (!priceOk) hint = 'Price per share must be positive.'; + else if (data.account.blocked) hint = 'Trading is disabled because this account is blocked.'; + else if (e.status !== 'ACTIVE') hint = 'Trading is only available for ACTIVE events.'; + else if (side === 'SELL' && !same(e.marketMaker, user) && qty > held[option]) + hint = `Cannot sell ${qty} shares; you only hold ${held[option]}.`; + else if (side === 'BUY' && num(data.account.balance) < cost) + hint = `Insufficient balance. Required: ${money(cost)}, available: ${money(data.account.balance)}`; + else { + allowed = true; + hint = + side === 'SELL' + ? 'Sell order will match existing bids or rest as an ask.' + : isBook + ? 'Buy order will match existing asks or rest as a bid.' + : 'Orders execute immediately against the market maker.'; + } + + const submit = () => + props.act( + () => + api('/orders', { + event: e.name, + option: markets[option].key, + side, + price: isBook ? price.trim() : markets[option].price, + quantity: String(qty), + }), + `Order placed ${SEP} ${side === 'BUY' ? 'Bought' : 'Sold'} ${qty} ${e.optionNames[option]}`, + ); + + return ( +
+
+ + {e.displayId}. {e.name} + + + {mechanismLabel(e.mechanism)} {SEP} Status: {e.status} {SEP} Fee: {feeLabel(e)} + +
+
+ {markets.map((m, i) => ( + + ))} +
+
+ + + {isBook && ( + + )} + + +
+ {hint} +
+ ); +} -- cgit v1.2.3