import { useEffect, useState, type ReactElement } from 'react' import { useBridge, useData, usePublish, type PanelRef } from '@jaspers-ai/sdk' import { display as d } from './format' import { Bar, Empty, Loading, Section, Signed, SymbolField, useEvery, usePanelStateValue, useStateWriter } from './shared' import { num, text } from './values' import { addSymbol, readWatchlist, REFRESH_MS, removeSymbol, watchlistOutput, WATCHLIST_MAX, yahooPage } from './views' import './styles.css' // A list of symbols with their quotes, asked again every minute. The field adds a symbol and the // cross removes one; both write the whole list to state, which is also how the orchestrator sets it. export function WatchlistView({ panel }: { panel: PanelRef }): ReactElement { const state = usePanelStateValue(panel) const write = useStateWriter(panel) if (state === undefined) return const symbols = readWatchlist(state) const set = (next: string[]): void => write([['symbols', next]]) return (
set(addSymbol(symbols, symbol))} /> Watchlist · {symbols.length} of {WATCHLIST_MAX}
{symbols.length ? ( set(removeSymbol(symbols, symbol))} /> ) : ( Add a symbol: AAPL, ^GSPC, BTC-USD, EURUSD=X. )}
) } function Quotes({ panel, symbols, onRemove }: { panel: PanelRef; symbols: string[]; onRemove: (symbol: string) => void }): ReactElement { const bridge = useBridge() const run = useData('yfinance/quote', { symbols }) useEvery(run.refetch, REFRESH_MS) const [updated, setUpdated] = useState(null) useEffect(() => { if (run.data) setUpdated(new Date().toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', second: '2-digit' })) }, [run.data]) const quotes = run.data ?? [] usePublish(panel, watchlistOutput(symbols, quotes)) const bySymbol = new Map(quotes.map((q) => [text(q['symbol']) ?? '', q])) return (
{symbols.map((symbol) => { const q = bySymbol.get(symbol) const hint = num(q?.['priceHint']) const low = num(q?.['dayLow']) const high = num(q?.['dayHigh']) return ( ) })}
Symbol Name Last Change % Volume Market cap Day range
{q ? text(q['name']) : {run.loading ? '…' : 'not found'}} {q ? d.price(num(q['price']), hint) : ''} {q && } {q && } {q ? d.big(num(q['volume'])) : ''} {q ? d.big(num(q['marketCap'])) : ''} {low !== null && high !== null ? `${d.price(low, hint)}–${d.price(high, hint)}` : ''}
) }