import { Button, Card, Paragraph, SizableText, XStack, YStack } from '@hanzo/gui' import { Grid } from '@hanzo/ui/grid' import { useEffect, useId, useMemo, useRef, useState, type ComponentProps, type CSSProperties, type ReactNode, } from 'react' import { paint } from '../material/paint' import { order, type Rank, type Sense } from './order' import { reach } from '../material/reach' import { weight } from '../material/weight' import { Field } from './field' /** * The table. Every list on a Lux surface is one of these — markets, tokens, * pools, holdings, activity, positions, a finder's rungs — so it is built here * and nowhere else. * * The engine has no table, and the reason it has none is that a table is not a * box: rows and cells have to be `` and `` for a screen reader to say * "row 4, price" instead of reading forty numbers in a line, and the engine's * boxes are flex containers whose `display` cannot even spell `table-cell` — * its Display union stops at `list-item`. So the elements are the elements, * they carry style objects, and the engine does the parts it actually has: the * search field, the two answers, the sort control. */ /** The page's own colours, named where they are used rather than inherited. */ const { plain, quiet, line, bad } = paint /** What a bar standing in for a value is painted with. The token layer * publishes it for exactly this and resolves it in both grounds. */ const { pending } = paint export interface Column { /** Identity: React's key, and the field a sort names. */ key: string /** The heading. */ head: ReactNode /** What this column shows for a row. */ cell: (row: R) => ReactNode /** Figures. Right edge, so a column of numbers can be read down. */ numeric?: boolean /** * How this column orders rows. Given, the heading becomes a control; absent, * the column does not sort. One prop says both, so there is no way to declare * a sortable column without saying what it sorts on. */ sort?: (row: R) => Rank } /** * What we know about the rows. * * Three constructors, because there are three different facts and one of them * is routinely reported as another: a request that failed renders as a venue * with nothing in it, and a reader who cannot tell the difference believes a * fabrication. A rows array plus a loading flag makes that mistake reachable — * `[]` is the same value whether we asked and got nothing or never found out. * Here `failed` carries the reason and cannot be spelled as an empty list. * * Empty is not a fourth state. It is `read` with no rows: we asked, and the * answer was nothing. That is a fact about the venue, and it is the one case a * caller has to put into its own words. */ export type Rows = | { at: 'reading' } | { at: 'read'; rows: R[] } | { at: 'failed'; why: string } /** Why nothing was read, where the caller states no better sentence. A failure * reported without its reason sends a reader to look at the venue, which is * the one place the fault is not. */ const reason = (e: unknown): string => (e instanceof Error ? e.message : String(e)) /** * A reading, from something that answers. * * `Rows` is what a table takes and nothing in this package made one, so every * screen wrote the same effect: a flag so a late answer cannot set state on a * screen somebody has left, a reset to `reading` when the question changes, and * a rejection turned into a sentence. Three lines, three chances, and the one * that gets missed is the flag — which shows up as a warning in development and * as a stale list in production. * * @param of what the question is about, as one string. The read runs again * when it changes — a pool id, a venue key, an address. Stated * rather than inferred from the reader, because a function literal * is a new value on every render and a dependency on one is a read * that never stops. * @param ask how to read. Called with no arguments; whatever it closes over is * the caller's business. * @param why what to say about a rejection, where the caller has better words * than the error's own — a refusal that answered and a request that * never completed are not the same failure. */ export function useRows( of: string, ask: () => Promise, why: (e: unknown) => string = reason, ): Rows { const [rows, setRows] = useState>({ at: 'reading' }) // Asked through a ref, so the effect below depends on the question and not on // the identity of the function that asks it. Kept current in an effect of its // own rather than during render, and declared above the read so that on the // commit where both the question and the reader changed, the reader is the // new one by the time it is called — effects in one commit run in the order // they are written. const asking = useRef(ask) const saying = useRef(why) useEffect(() => { asking.current = ask saying.current = why }) useEffect(() => { let live = true setRows({ at: 'reading' }) asking .current() .then((got) => live && setRows({ at: 'read', rows: got })) .catch((e: unknown) => live && setRows({ at: 'failed', why: saying.current(e) })) return () => { live = false } }, [of]) return rows } export interface TableProps { /** * What this table lists, as a plural noun in the reader's words — "markets", * "pools", "positions". It names the scrolling region for a screen reader, * which is the whole of what it does: it is prose, so it is translated, and * an address built out of it is an address that moves with the language. */ name: string columns: Column[] /** Row identity, stable across a re-read. */ id: (row: R) => string rows: Rows /** * The four sentences the table says about its own state. * * `name` and `empty` are what the caller says about its venue; these are what * the table says about itself, and they are the same four on every table in * an application. They are props and not English written into this file * because a surface that reads a chain in one language and says "Loading" in * another has published a sentence nobody translated — and a component that * reaches into a catalog has taken that dependency on the caller's behalf. */ words: { /** Said in a live region rather than drawn, while a read is running. */ reading: string /** The label on the search box, where the table asks for itself. */ search: string /** When a search matched none of the rows there are. The rows are all * still here; only the query is hiding them. */ none: string /** The title over a read that failed. The reason is the reading's own. */ failed: string } /** * What "nothing" means here. Required, because "No results" over an empty * table is the sentence a reader mistakes for a broken page — and because * only the caller knows whether nothing means nobody has opened a market yet * or this wallet holds none. */ empty: { title: ReactNode; note?: ReactNode } /** * A summary of a list rather than the list. * * A full list stands under a ruled row of tracked capitals; a summary stands * under a filled band in the running register. The difference is not paint: a * summary shows the first few of something, so a heading that sorts would * order rows the reader cannot see the rest of. One prop, so the band and the * absence of the control cannot come apart. */ brief?: boolean /** The text a row is found by. Given, a search box appears above the table — * unless `query` says the box is somewhere else. */ find?: (row: R) => string /** * What the reader is looking for, where the field that asks is not this * table's. * * A screen with four lists on four tabs has one place for that field, and it * is the row the tabs are on. The filtering stays here: the table is what * knows which rows matched, what to say when none did, and how that differs * from a venue that holds none — moving the box out must not move any of * that with it. Absent, the table asks for itself. */ query?: string /** * Where a row leads, or what can be done to it: a link into the thing, a * button that prepares a call. It renders in a trailing column of its own, * which is why it is not a `Column` — an action is not a value, so it has no * heading and nothing to sort on. */ action?: (row: R) => ReactNode /** * What a row opens to: a chart, a set of terms — anything too tall for a * cell and too particular to be a column every row pays for. * * It rides in the trailing cell beside the action rather than in a column of * its own, so a table that has one is the same table with one more control in * it. What it opens is a row of its own under the row it belongs to, which is * what keeps a screen reader inside the table and a keyboard on the control * it pressed. * * `name` is the word on every one of them; `label` is what this row's control * is called, since a page of controls that all read "Chart" is a page a * screen reader reads out as a dozen identical links. */ detail?: { name: string label: (row: R) => string of: (row: R) => ReactNode } } /** * Where a row's detail is addressed. * * `aria-controls` is a list of ids separated by spaces, and a row is identified * by whatever the caller identifies it by — a pair, a name, an address. `LUX / * USDC` carries two spaces, so the attribute named four ids, none of them an * element, and the control announced itself as expanding something a reader * could not be taken to. Nothing reports that: the attribute is well formed and * the row it should point at is right there in the markup. * * So the identity is percent-encoded, which removes every space and stays one * key per row — two rows cannot collide the way replacing the spaces would let * them. The prefix is the table's own, from `useId`, because a screen holding a * summary and the full list of the same thing has two tables that would * otherwise agree on every id in both. */ export const anchor = (of: string, row: string): string => `${of}${encodeURIComponent(row)}` /** * Whether a query is what is hiding the rows. * * There are two absences and one of them is a fabrication if it is drawn as the * other. A query hides rows only where there are rows to hide: with a venue * that holds nothing and something typed in the field, "no market matches that" * says markets exist and none of them answered — which is precisely the reading * this component was built to keep off a screen, one level down from the one it * catches. */ export const hiding = (asked: string, of: number): boolean => asked !== '' && of > 0 /** Placeholder rows. Enough to read as a list, few enough to be a promise. */ const WAITING = 5 /** * How wide each placeholder bar is, by column. * * Fixed rather than random: a width that changes every render is motion, and a * skeleton is a promise about layout, so it holds still. Varying by column is * what makes five rows read as a table of data instead of a wireframe grid. */ const BARS = ['64%', '42%', '78%', '52%', '70%', '38%', '58%', '46%'] /** * The heading voice, in one object because it is worn twice: by a plain `` * and by the span inside a sorting one. A `