'use client'
import { SizableText, XStack, YStack } from '@hanzo/gui'
import type { ReactNode } from 'react'
import { useIdentity, type Of } from './asset'
import { NONE } from './figure'
import { Picture } from './picture'
import { paint } from '../material/paint'
import { weight } from '../material/weight'
/**
* A token, wearing its own face.
*
* Every list on this surface named tokens in bare text — the picker, the market
* table, the pool rows, the token page. A person scanning forty rows for the one
* they hold is reading forty strings, and reading is slower than recognising.
* That is the whole argument for a mark: not decoration, a shorter path to the
* row you came for.
*
* One component draws every asset on the surface. Four screens and the picker
* each had their own spelling of "mark, then symbol, then name" and they had
* drifted: the picker put the name above the symbol and the tables put it
* below, the trigger sized its mark at 28 where the reference draws 24, and the
* chooser's rows sized theirs at 34 where the reference draws 36.
*
* An asset a build serves a mark for wears it. `asset.ts` next door answers
* where from, and nothing is drawn here — a mark is a brand, and a brand
* redrawn from memory is a different brand.
*
* Most tokens have no mark. The indexer returns id, symbol, name and decimals
* and no image, measured against its own schema, so the fallback is not an edge
* case: it is what a list of this chain's tokens mostly looks like. That
* fallback is the asset's own letters — derived from what is there, always
* resolves, never fetches — rather than a placeholder picture, which says the
* same nothing for every row and helps a reader scan none of them.
*
* The letters are achromatic. `@luxfi/ui/tokens.css` states the rule plainly —
* one achromatic ladder plus exactly two hues, reserved for error and success,
* and nothing else carries colour. A wall of tinted discs breaks that twice: it
* spends colour on identity, and it gives the reader a second thing that looks
* like state and is not.
*
* So it varies in lightness rather than in hue, taken from the asset's own
* address. Same asset, same shade, on every screen and across reloads — the
* only property that makes it recognisable — and the page stays monochrome.
*/
/**
* A shade for one address, stable across screens and across reloads.
*
* Returned as a step 0..11 rather than a percentage, so the two lightnesses a
* mark needs — its ground and its letters — are derived from one number and
* cannot drift apart.
*
* FNV-1a rather than a sum of character codes. The input is hex, so its
* alphabet is sixteen characters wide, and a plain sum lands every address in
* the same narrow band: measured across six real tokens, the first version put
* all six between 180 and 344 with two of them seventeen apart. Mixing on every
* character spreads them.
*/
export const shade = (address: string | undefined): number => {
let h = 0x811c9dc5
for (const c of (address ?? '').toLowerCase()) {
h ^= c.charCodeAt(0)
h = Math.imul(h, 0x01000193) >>> 0
}
return h % 12
}
/**
* How large a mark is drawn, in the four places this surface draws one, each
* measured on the reference.
*
* `pill` sits beside a figure on a trade card, where the control holds one line
* and the amount beside it is the largest thing on the card. Twenty-four, with
* the chain badge on it at twelve.
*
* `row` is a mark in a table, as tall as the two lines beside it, which is what
* makes a list of tokens scan as a column of objects rather than a column of
* strings. Thirty-two, against a symbol line and a name line that together
* stand thirty-three.
*
* `card` is a row on one of the door's product cards, which lists tokens as
* the card's own artwork rather than as a table. The same thirty-two, and the
* only place where the name is read before the symbol: nobody is scanning a
* column here, so the name leads and the ticker qualifies it. Both at
* twenty-four, on one line.
*
* `list` is a row in the chooser, which is a taller row than a table's because
* it is a target rather than a cell. Thirty-six.
*
* `panel` is the row under a sum that says what the sum is an amount of. The
* chooser's thirty-six, because it is the same object at the same distance —
* what differs is that it names one asset rather than offering a column of
* them, so the symbol is read rather than scanned.
*
* `page` is the mark for whatever the whole screen is about. Fifty-six,
* measured on the reference's token screen, where it opens the header at 160
* with the two words starting at 228 — the mark, twelve, and the name over the
* ticker beside it.
*
* `unit` is the smallest, and it stands beside a name that says what unit the
* figure next to it is in rather than offering a choice of asset. Sixteen,
* measured on the reference's limit card, where the mark beside LUSD is 16 to
* the 24 on the pills under it and its badge is seven.
*
* Exported, because a row can carry a square that is not an asset's — a holder
* mints nothing, so its row wears the chain's mark drawn as a plain image — and
* it has to be the rung its neighbours are drawn at. Stated here rather than
* restated there: a column whose marks change size between rows is a column of
* different objects, which is what a second copy of this number goes on to
* produce the day one of them moves.
*/
export const SIZE = { unit: 16, pill: 24, row: 32, card: 32, list: 36, panel: 36, page: 56 } as const
export type Size = keyof typeof SIZE
/**
* The rung and the weight the word a reader meets first takes in each of those
* places — which is the ticker where a column is being scanned and the name
* where none is.
*
* Measured on the reference: 16 at 535 on the pill beside a figure, 14 at 600
* in a table, 24 at 485 on a door card, 16 at 485 on the row under a sum, 24 in
* a 29 box at the top of a token screen. Each is a different rung and a
* different weight, and writing them where the component draws is how the call
* sites came to disagree about both.
*
* `card` states a size rather than a rung because the ramp has no 24: it climbs
* 10 · 14 · 16 · 18 · 22 · 30 · 40 where the reference climbs
* 10 · 12 · 14 · 16 · 18 · 24 · 36, and `chrome/tokens.ts` already spends its
* two overrides on the two rungs the whole interface reads at. One card's
* artwork does not buy a third.
*
* `page` asks for the same missing 24 and takes the rung below it, because
* here the neighbour is the better answer and not just the cheaper one. At 22
* the name stands in a 27 box over an 18 one, and 27 · 8 · 18 is the 53 the
* reference's two lines stand in — where a stated 24 makes them 55. The rung
* misses by two and the block it is read as lands exactly.
*/
const SAY = {
// A unit is stated, not chosen, so the name is set at the weight of the words
// around it where a pill's is marked — measured on the reference at 16/485
// against the pill's 16/535.
unit: { size: '$3', fontWeight: weight.plain },
pill: { size: '$3', fontWeight: weight.mark },
row: { size: '$2', fontWeight: weight.mark },
card: { fontSize: 24, lineHeight: 29, fontWeight: weight.plain },
list: { size: '$2', fontWeight: weight.mark },
panel: { size: '$3', fontWeight: weight.plain },
page: { size: '$6', fontWeight: weight.mark },
} as const
/**
* The caret that says a control opens a chooser.
*
* Twenty-four, which is what the reference draws beside its own pill. It is not
* a target of its own — the whole control is — so it carries no name and no
* press.
*
* The number lives here and the caret is drawn by the control that opens: an
* asset is the same object whether or not pressing it leads anywhere, and where
* the caret goes and which way it points are facts about the control's box
* rather than about the asset in it. A full-width row puts it at the far end
* and points it on; a pill sized to its pair puts it after the symbol.
*/
export const CARET = 24
/**
* The badge that says which chain an asset is on, at half the mark.
*
* Half is the reference's proportion, and it puts the badge exactly in the
* mark's lower-right quarter with no overhang: at twenty-four and twelve the
* two squares share a corner. It is drawn only where a screen states the chain
* it is trading on — the chooser and the control that opens it. A table already
* scoped to one chain would repeat the same badge down every row, which the
* reference does not do either: its own token table badges the row whose chain
* differs and leaves the rest bare.
*
* A badge that does not arrive leaves the mark alone, which is what an asset on
* a screen that states no chain already draws. The chain is a qualifier and the
* mark is the object, so the row loses a distinction rather than its subject.
*/
function Badge({ src, on }: { src: string; on: number }) {
const px = Math.round(on / 2)
return (
)
}
/** The disc alone: the asset's own mark where this build ships one, its letters
* where it does not. Both rather than either — a mark is recognised faster
* than three letters are read, and three letters always resolve. */
function Disc({ of, px }: { of: Of; px: number }) {
const it = useIdentity(of)
// Twelve steps across a band that stays clear of the page behind it and of
// the text in front. Low enough to read as an object, wide enough that two
// marks side by side are different objects.
const step = shade(it.address)
const ground = 16 + step * 2.5
const ink = 62 + step * 2.5
/**
* What stands there when no mark does — because this build serves none for
* the asset, or because the one it named did not arrive. Both are the same
* question to a reader, and they are answered the same way.
*/
const letters = (
{/* Sized to the disc rather than off the ramp, at the proportion the
reference draws: twelve inside thirty-two, eight times, in its own
token table. Held as a ratio because the disc is drawn at seven
rungs and the reference measures one — a size frozen at twelve
fills a twenty-four across corner to corner, where the corners of
a text box that wide fall outside the circle, and leaves a
fifty-six looking like a mark somebody forgot to finish.
Marked, because three letters this small go thin at the weight the
words beside them are read at. */}
{it.fallback}
)
return (
{it.badge ? : null}
)
}
/**
* The two currencies a pool holds, drawn as one object.
*
* A position, a pool and a market are each named by both their sides, and a
* reader recognises the row by the marks before reading either ticker — the
* same argument that put a disc beside every single asset on the surface. The
* positions table was the one list still naming both sides in bare text.
*
* Side by side rather than overlapping. The reference's own pair mark is on no
* screen this build can capture — its indexer answers 404 and its position list
* needs a wallet — so the discs take the gap every other pair of objects on this
* surface takes, and an overlap invented for them would be geometry nobody
* measured.
*/
export function Coins({
of,
say,
size = 'row',
}: {
of: readonly Of[]
/** How the pool names itself — "USDC / LUX". Absent, this is the marks
* alone, which is what a cell already carrying the name wants. */
say?: string
size?: Size
}) {
const px = SIZE[size]
return (
{of.map((a) => (
))}
{say === undefined ? null : (
{say}
)}
)
}
/** The two words, stacked or on one line. Two primitives and one boolean,
* rather than a style prop a call site could set either way. */
function Line({ across, children, ...rest }: { across: boolean; children: ReactNode } & Record) {
const As = across ? XStack : YStack
return (
{children}
)
}
export interface CoinProps {
symbol?: string
/** The asset's name, drawn under the symbol where a row has both. */
name?: string
address?: string
/** The chain this asset is on, where the screen states one. Draws the badge. */
chain?: string
size?: Size
/** What is held, or offered, drawn at the end of the row. */
amount?: ReactNode
}
/**
* An asset as a reader meets it: its mark, its symbol, its name where there is
* room, and whatever the row states about it.
*
* With no symbol, no name and nothing after it, this is the disc alone — which
* is what a market row whose token the indexer has not named yet, and a wallet
* page about one address, each want.
*/
export function Coin({ symbol, name, address, chain, size = 'row', amount }: CoinProps) {
const px = SIZE[size]
/** Whether this draws words at all. A market row whose token the indexer has
* not named yet, and a page about one address, are both the disc alone. */
const words = symbol !== undefined || name !== undefined
/**
* Which of the two words is read first, and everything that follows from it.
*
* Nobody is scanning a column on a door card or at the top of a screen about
* one asset, so the name leads there and the ticker qualifies it; everywhere
* else the ticker is what a reader is scanning for and the name sits under it,
* out of the column's way.
*
* The gap follows the same fact. Under a ticker the name is that ticker's
* own second line and takes none — two lines already carry more leading than
* the glyphs need, and a gap on top of that separates a name from its own
* ticker. Where the name leads they are two readings rather than one, and the
* reference sets them eight apart.
*/
const lead = size === 'card' || size === 'page'
const proper = name && name !== symbol ? name : undefined
const first = lead && proper ? proper : (symbol ?? NONE)
const second = first === proper ? symbol : proper
return (
{/* The name under the symbol, not beside it — everywhere a reader is
scanning. Side by side, a name is read as part of the ticker — "LUX
Lux", "USDC USD Coin" — and the eye has to find where one ends before
it can compare the next row. Stacked, the tickers form a column of
their own and the names sit out of the way of it, which is the whole
reason a list has a left edge, and it is what makes a row twice the
height of its type, which is the pitch a person scans a table at.
No gap between them, either: two lines already carry more leading than
the glyphs need, and a gap on top of that separates a name from its
own ticker.
A door card is the exception, because nothing on it is being scanned.
Its four rows are the card's artwork, so the reference sets name and
ticker on one line and reads the name first. */}
{words ? (
{/* The word this place reads first, at the one rung it states for
itself, in the ink of the thing the object is called. */}
{first}
{/* The qualifier, a step behind — by weight and size where a column is
being scanned, which is the reference's own 14 at 600 over 12 at
485. The door card is the one place the two are one line at one
size and only the colour separates them. */}
{second === undefined ? null : (
{second}
)}
) : null}
{amount === undefined ? null : (
{amount}
)}
)
}