diff options
| author | Kostya <mail@sartin.in> | 2026-10-15 19:35:48 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-15 19:36:15 +0300 |
| commit | c263497ec8b05bc0f9254ed2f7ecb4dba6f67d4e (patch) | |
| tree | d9b721c72a4ffa2d6256adc305716c8432cb190c /ui-web/src/Events.tsx | |
| parent | c67d5ff663b876a15a3831f5a2b564c4002e99b6 (diff) | |
| download | guess-market-c263497ec8b05bc0f9254ed2f7ecb4dba6f67d4e.tar.gz guess-market-c263497ec8b05bc0f9254ed2f7ecb4dba6f67d4e.tar.xz guess-market-c263497ec8b05bc0f9254ed2f7ecb4dba6f67d4e.zip | |
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.
Diffstat (limited to 'ui-web/src/Events.tsx')
| -rw-r--r-- | ui-web/src/Events.tsx | 370 |
1 files changed, 370 insertions, 0 deletions
diff --git a/ui-web/src/Events.tsx b/ui-web/src/Events.tsx new file mode 100644 index 0000000..e33b4e2 --- /dev/null +++ b/ui-web/src/Events.tsx | |||
| @@ -0,0 +1,370 @@ | |||
| 1 | import { useState } from 'react'; | ||
| 2 | import { | ||
| 3 | api, | ||
| 4 | EventDetail, | ||
| 5 | eventPath, | ||
| 6 | EventSummary, | ||
| 7 | feeLabel, | ||
| 8 | FeeTiming, | ||
| 9 | MarketData, | ||
| 10 | Mechanism, | ||
| 11 | mechanismLabel, | ||
| 12 | money, | ||
| 13 | num, | ||
| 14 | OrderBook, | ||
| 15 | same, | ||
| 16 | SEP, | ||
| 17 | Status, | ||
| 18 | } from './api'; | ||
| 19 | import type { Act } from './App'; | ||
| 20 | import { LineChart, priceSeries } from './Chart'; | ||
| 21 | import { Equalizer } from './graphics'; | ||
| 22 | |||
| 23 | const STATUS_PILL: Record<Status, [string, string]> = { | ||
| 24 | DRAFT: ['', 'IDLE'], | ||
| 25 | ACTIVE: ['active', 'ACTIVE'], | ||
| 26 | SETTLED: ['closed', 'CLOSED'], | ||
| 27 | }; | ||
| 28 | |||
| 29 | function Filter<T extends string>(props: { | ||
| 30 | label: string; | ||
| 31 | value: T | null; | ||
| 32 | options: [T, string][]; | ||
| 33 | onChange: (value: T | null) => void; | ||
| 34 | }) { | ||
| 35 | return ( | ||
| 36 | <div className="filter"> | ||
| 37 | <span>{props.label}</span> | ||
| 38 | <button className={props.value === null ? 'toggle on' : 'toggle'} onClick={() => props.onChange(null)}> | ||
| 39 | All | ||
| 40 | </button> | ||
| 41 | {props.options.map(([value, text]) => ( | ||
| 42 | <button | ||
| 43 | key={value} | ||
| 44 | className={props.value === value ? 'toggle on' : 'toggle'} | ||
| 45 | onClick={() => props.onChange(value)} | ||
| 46 | > | ||
| 47 | {text} | ||
| 48 | </button> | ||
| 49 | ))} | ||
| 50 | </div> | ||
| 51 | ); | ||
| 52 | } | ||
| 53 | |||
| 54 | export function Events(props: { | ||
| 55 | data: MarketData; | ||
| 56 | user: string; | ||
| 57 | selected: EventSummary | undefined; | ||
| 58 | onSelect: (key: string) => void; | ||
| 59 | act: Act; | ||
| 60 | onTrade: () => void; | ||
| 61 | onResolve: () => void; | ||
| 62 | }) { | ||
| 63 | const { data, selected } = props; | ||
| 64 | const [method, setMethod] = useState<Mechanism | null>(null); | ||
| 65 | const [status, setStatus] = useState<Status | null>(null); | ||
| 66 | const [fee, setFee] = useState<FeeTiming | null>(null); | ||
| 67 | |||
| 68 | const shown = data.events.filter( | ||
| 69 | (e) => | ||
| 70 | (method === null || e.mechanism === method) && | ||
| 71 | (status === null || e.status === status) && | ||
| 72 | (fee === null || e.commissionTiming === fee), | ||
| 73 | ); | ||
| 74 | const detail = selected && data.details[selected.key]; | ||
| 75 | |||
| 76 | return ( | ||
| 77 | <> | ||
| 78 | <section className="card side events"> | ||
| 79 | <Filter | ||
| 80 | label="Method" | ||
| 81 | value={method} | ||
| 82 | onChange={setMethod} | ||
| 83 | options={[ | ||
| 84 | ['LMSR', 'LMSR'], | ||
| 85 | ['ORDER_BOOK', 'Order Book'], | ||
| 86 | ]} | ||
| 87 | /> | ||
| 88 | <Filter | ||
| 89 | label="Status" | ||
| 90 | value={status} | ||
| 91 | onChange={setStatus} | ||
| 92 | options={[ | ||
| 93 | ['DRAFT', 'Not started'], | ||
| 94 | ['ACTIVE', 'Active'], | ||
| 95 | ['SETTLED', 'Closed'], | ||
| 96 | ]} | ||
| 97 | /> | ||
| 98 | <Filter | ||
| 99 | label="Fee" | ||
| 100 | value={fee} | ||
| 101 | onChange={setFee} | ||
| 102 | options={[ | ||
| 103 | ['ON_PURCHASE', 'On purchase'], | ||
| 104 | ['ON_CLOSE', 'On close'], | ||
| 105 | ]} | ||
| 106 | /> | ||
| 107 | <div className="list"> | ||
| 108 | {shown.map((e) => ( | ||
| 109 | <button | ||
| 110 | key={e.key} | ||
| 111 | className={e.key === selected?.key ? 'listrow on' : 'listrow'} | ||
| 112 | style={{ flexDirection: 'column', alignItems: 'stretch', gap: 6 }} | ||
| 113 | onClick={() => props.onSelect(e.key)} | ||
| 114 | > | ||
| 115 | <span className="row"> | ||
| 116 | <span className="sm ink2">#{e.displayId}</span> | ||
| 117 | <span className="bold ellipsis">{e.name}</span> | ||
| 118 | </span> | ||
| 119 | <span className="row sm ink2"> | ||
| 120 | <span>{mechanismLabel(e.mechanism)}</span> | ||
| 121 | <span className={'pill ' + STATUS_PILL[e.status][0]}>{STATUS_PILL[e.status][1]}</span> | ||
| 122 | <span>{feeLabel(e)}</span> | ||
| 123 | <span className="grow" /> | ||
| 124 | <span>{money(e.accountBalance)}</span> | ||
| 125 | </span> | ||
| 126 | </button> | ||
| 127 | ))} | ||
| 128 | </div> | ||
| 129 | <span className="sm ink2"> | ||
| 130 | {shown.length} of {data.events.length} events shown | ||
| 131 | </span> | ||
| 132 | </section> | ||
| 133 | |||
| 134 | {detail ? ( | ||
| 135 | <EventView detail={detail} user={props.user} act={props.act} onTrade={props.onTrade} onResolve={props.onResolve} /> | ||
| 136 | ) : ( | ||
| 137 | <section className="card main" /> | ||
| 138 | )} | ||
| 139 | </> | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | |||
| 143 | function EventView(props: { detail: EventDetail; user: string; act: Act; onTrade: () => void; onResolve: () => void }) { | ||
| 144 | const { detail, user, act } = props; | ||
| 145 | const e = detail.summary; | ||
| 146 | const markets = detail.state.markets; | ||
| 147 | const isMm = same(e.marketMaker, user); | ||
| 148 | const live = e.status === 'ACTIVE' ? 'LIVE' : e.status === 'SETTLED' ? 'ENDED' : 'IDLE'; | ||
| 149 | const yes = priceSeries(detail); | ||
| 150 | |||
| 151 | return ( | ||
| 152 | <section className="card main"> | ||
| 153 | <div className="head" style={{ flexDirection: 'column', gap: 9 }}> | ||
| 154 | <div className="row" style={{ alignItems: 'flex-start', gap: 14, width: '100%' }}> | ||
| 155 | <div className="grow"> | ||
| 156 | <h2 className="xl bold"> | ||
| 157 | {e.displayId}. {e.name} | ||
| 158 | </h2> | ||
| 159 | <p className="base ink2 ellipsis" title={e.description}> | ||
| 160 | {e.description} | ||
| 161 | </p> | ||
| 162 | </div> | ||
| 163 | {/* Open = go trade on an active event, or the Market Maker starting their own */} | ||
| 164 | {e.status === 'ACTIVE' && ( | ||
| 165 | <button className="btn primary" onClick={props.onTrade}> | ||
| 166 | Trade | ||
| 167 | </button> | ||
| 168 | )} | ||
| 169 | {e.status === 'DRAFT' && isMm && ( | ||
| 170 | <button | ||
| 171 | className="btn primary" | ||
| 172 | onClick={() => act(() => api(eventPath(e.name) + '/open', {}), `Event started`)} | ||
| 173 | > | ||
| 174 | Start Event | ||
| 175 | </button> | ||
| 176 | )} | ||
| 177 | {e.status === 'ACTIVE' && isMm && ( | ||
| 178 | <button className="btn" onClick={props.onResolve}> | ||
| 179 | Close & Resolve… | ||
| 180 | </button> | ||
| 181 | )} | ||
| 182 | </div> | ||
| 183 | <div className="meta"> | ||
| 184 | <span className={'pill ' + STATUS_PILL[e.status][0]}> | ||
| 185 | <Equalizer /> | ||
| 186 | {live} | ||
| 187 | </span> | ||
| 188 | <span> | ||
| 189 | <span className="ink2">Method</span> <b>{mechanismLabel(e.mechanism)}</b> | ||
| 190 | </span> | ||
| 191 | <span> | ||
| 192 | <span className="ink2">Status</span> <b>{e.status}</b> | ||
| 193 | </span> | ||
| 194 | <span> | ||
| 195 | <span className="ink2">Market Maker</span> <b>{e.marketMaker}</b> | ||
| 196 | </span> | ||
| 197 | <span> | ||
| 198 | <span className="ink2">Fee</span> <b>{feeLabel(e)}</b> | ||
| 199 | </span> | ||
| 200 | <span> | ||
| 201 | <span className="ink2">Contract</span> <b>{money(e.accountBalance)}</b> | ||
| 202 | </span> | ||
| 203 | </div> | ||
| 204 | </div> | ||
| 205 | |||
| 206 | <div className="body"> | ||
| 207 | {e.status === 'SETTLED' && ( | ||
| 208 | <div className="notice"> | ||
| 209 | <span className="pill resolved">RESOLVED</span> | ||
| 210 | <span> | ||
| 211 | Winning option: <b>{detail.settledMarket}</b> {SEP} contract emptied to its holders | ||
| 212 | </span> | ||
| 213 | </div> | ||
| 214 | )} | ||
| 215 | |||
| 216 | {e.mechanism === 'LMSR' ? ( | ||
| 217 | <> | ||
| 218 | <div className="pair"> | ||
| 219 | {markets.map((m, i) => ( | ||
| 220 | <div key={m.key} className={i === 0 ? 'optcard yes' : 'optcard no'}> | ||
| 221 | <div className="row"> | ||
| 222 | <span className="bold grow ellipsis"> | ||
| 223 | {i === 0 ? 'YES' : 'NO'} <span className="sm">{m.name}</span> | ||
| 224 | </span> | ||
| 225 | <span className="bold" style={{ fontSize: 22 }}> | ||
| 226 | {money(m.price)} | ||
| 227 | </span> | ||
| 228 | </div> | ||
| 229 | <div className="bar"> | ||
| 230 | <i style={{ width: `${num(m.price) * 100}%` }} /> | ||
| 231 | </div> | ||
| 232 | <span className="sm ink2"> | ||
| 233 | {m.volume} shares {SEP} p = {num(m.price).toFixed(2)} | ||
| 234 | </span> | ||
| 235 | </div> | ||
| 236 | ))} | ||
| 237 | </div> | ||
| 238 | <div className="section"> | ||
| 239 | <div className="section-title"> | ||
| 240 | Share price over time <span className="grow" /> | ||
| 241 | <span className="sm ink2" style={{ fontWeight: 400 }}> | ||
| 242 | YES / NO {SEP} LMSR | ||
| 243 | </span> | ||
| 244 | </div> | ||
| 245 | <LineChart | ||
| 246 | range={[0, 1]} | ||
| 247 | series={[ | ||
| 248 | { points: yes, tone: 'yes' }, | ||
| 249 | { points: yes.map(([t, v]) => [t, 1 - v]), tone: 'no' }, | ||
| 250 | ]} | ||
| 251 | /> | ||
| 252 | </div> | ||
| 253 | </> | ||
| 254 | ) : ( | ||
| 255 | <> | ||
| 256 | <div className="pair"> | ||
| 257 | {detail.orderBooks.map((book, i) => ( | ||
| 258 | <Book key={book.optionKey} book={book} last={markets[i]?.price} side={i === 0 ? 'YES' : 'NO'} /> | ||
| 259 | ))} | ||
| 260 | </div> | ||
| 261 | <div className="section"> | ||
| 262 | <div className="section-title"> | ||
| 263 | Last trade price over time <span className="grow" /> | ||
| 264 | <span className="sm ink2" style={{ fontWeight: 400 }}> | ||
| 265 | last trade YES | ||
| 266 | </span> | ||
| 267 | </div> | ||
| 268 | <LineChart range={[0, 1]} series={[{ points: yes, tone: 'accent' }]} /> | ||
| 269 | </div> | ||
| 270 | </> | ||
| 271 | )} | ||
| 272 | |||
| 273 | <div className="section"> | ||
| 274 | <div className="section-title"> | ||
| 275 | Trade history <span className="sm ink2">newest first</span> | ||
| 276 | </div> | ||
| 277 | <div className="table"> | ||
| 278 | <div className="thead"> | ||
| 279 | <span className="w66">#</span> | ||
| 280 | <span className="grow">User</span> | ||
| 281 | <span className="w150">Option</span> | ||
| 282 | <span className="w92 num">Shares</span> | ||
| 283 | <span className="w92 num">Paid</span> | ||
| 284 | </div> | ||
| 285 | {detail.history.map((t, i) => ( | ||
| 286 | <div className="trow" key={i}> | ||
| 287 | <span className="w66 ink2 bold">#{detail.history.length - i}</span> | ||
| 288 | <span className="grow ellipsis">{t.userName}</span> | ||
| 289 | <span className={`w150 bold ellipsis ${t.optionName === e.optionNames[0] ? 'yes' : 'no'}`}> | ||
| 290 | {t.optionName} | ||
| 291 | </span> | ||
| 292 | <span className="w92 num">{t.quantity} shares</span> | ||
| 293 | <span className="w92 num bold accent">{money(t.pricePaid)}</span> | ||
| 294 | </div> | ||
| 295 | ))} | ||
| 296 | {detail.history.length === 0 && <div className="trow ink2">No trades yet</div>} | ||
| 297 | </div> | ||
| 298 | </div> | ||
| 299 | |||
| 300 | <div className="section"> | ||
| 301 | <div className="section-title">Participants</div> | ||
| 302 | <div className="table"> | ||
| 303 | <div className="thead"> | ||
| 304 | <span className="grow">User</span> | ||
| 305 | <span className="w78 num">YES</span> | ||
| 306 | <span className="w78 num">NO</span> | ||
| 307 | <span className="w92 num">Value</span> | ||
| 308 | <span className="w92 num">Fees paid</span> | ||
| 309 | </div> | ||
| 310 | {detail.participants.map((p) => ( | ||
| 311 | <div className="trow" key={p.user}> | ||
| 312 | <span className="grow ellipsis bold"> | ||
| 313 | {p.user} | ||
| 314 | {p.tag && ` (${p.tag})`} | ||
| 315 | </span> | ||
| 316 | <span className="w78 num">{p.yes}</span> | ||
| 317 | <span className="w78 num">{p.no}</span> | ||
| 318 | <span className="w92 num">{p.value}</span> | ||
| 319 | <span className="w92 num">{p.fees}</span> | ||
| 320 | </div> | ||
| 321 | ))} | ||
| 322 | {detail.participants.length === 0 && <div className="trow ink2">Nobody yet</div>} | ||
| 323 | </div> | ||
| 324 | </div> | ||
| 325 | </div> | ||
| 326 | </section> | ||
| 327 | ); | ||
| 328 | } | ||
| 329 | |||
| 330 | function Book({ book, last, side }: { book: OrderBook; last: string | undefined; side: string }) { | ||
| 331 | const mid = book.bestBid && book.bestAsk ? money((num(book.bestBid) + num(book.bestAsk)) / 2) : '—'; | ||
| 332 | return ( | ||
| 333 | <div className="table"> | ||
| 334 | <div className="row" style={{ padding: '10px 11px 0' }}> | ||
| 335 | <span className={side === 'YES' ? 'bold yes' : 'bold no'}>{side} Order Book</span> | ||
| 336 | <span className="sm ink2 ellipsis grow">{book.optionName}</span> | ||
| 337 | <span className="sm ink2">{book.orders.length} orders</span> | ||
| 338 | </div> | ||
| 339 | <div className="stats"> | ||
| 340 | {[ | ||
| 341 | ['Last', money(last)], | ||
| 342 | ['Best bid', book.bestBid ?? '—'], | ||
| 343 | ['Best ask', book.bestAsk ?? '—'], | ||
| 344 | ['Mid', mid], | ||
| 345 | ['Spread', book.spread ?? '—'], | ||
| 346 | ].map(([label, value]) => ( | ||
| 347 | <span key={label}> | ||
| 348 | <span className="xs ink2">{label}</span> | ||
| 349 | <b>{value}</b> | ||
| 350 | </span> | ||
| 351 | ))} | ||
| 352 | </div> | ||
| 353 | <div className="thead"> | ||
| 354 | <span className="w54 num">Price</span> | ||
| 355 | <span className="w54 num">Side</span> | ||
| 356 | <span className="w54 num">Qty</span> | ||
| 357 | <span className="grow">User</span> | ||
| 358 | </div> | ||
| 359 | {book.orders.map((o, i) => ( | ||
| 360 | <div className={o.isBid ? 'trow bid' : 'trow ask'} key={i}> | ||
| 361 | <span className="w54 num bold">{o.price}</span> | ||
| 362 | <span className={o.isBid ? 'w54 num yes' : 'w54 num no'}>{o.side}</span> | ||
| 363 | <span className="w54 num">{o.quantity}</span> | ||
| 364 | <span className="grow ellipsis">{o.user}</span> | ||
| 365 | </div> | ||
| 366 | ))} | ||
| 367 | {book.orders.length === 0 && <div className="trow ink2">No resting orders</div>} | ||
| 368 | </div> | ||
| 369 | ); | ||
| 370 | } | ||