import { Grid, tracks } from '@hanzo/ui/grid'
import { Heading, Paragraph, SizableText, XStack, YStack } from '@hanzo/gui'
import type { ReactNode } from 'react'
import { measure, Panel } from './panel'
import { paint } from '../material/paint'
/**
* The frame anything with a name and some facts is read in.
*
* A token, a position, a proposal, a role and a treasury asset are the same
* page: something is named, a few things are known about it, and beneath that
* sit the blocks that let you do something with it. Written once per subject
* they drift within a week — one grows a badge row the next has not got, one
* starts abbreviating an address the other spells out — and a reader who moves
* between them has to learn the page twice.
*
* So the frame is here and the pages are what they put in it. Nothing in this
* file reads a chain, names a subject or knows a route; it lays out what it is
* handed and enforces the one rule that cannot be left to a call site, which is
* what a fact does when there is nothing to say.
*/
const { plain, quiet } = paint
/**
* The gutter of the fact list, in pixels rather than a token, because two
* things have to agree on it and only a number can.
*
* `tracks` floors each track at one Nth of the row MINUS the gutters — so a
* track list computed against a gutter of zero and then drawn with a real one
* asks for more tracks than fit, and auto-fill quietly hands back one. The list
* renders as a single column at every width, which looks like a layout choice
* rather than arithmetic that cancelled itself.
*/
const gutter = 16
/**
* What a value may be, which is anything that draws.
*
* `ReactNode` admits `true` and `false`, and both render as nothing at all —
* so `known && `, the ordinary conditional child, spells a fact with
* a blank where its value goes and type-checks. Out of the union it is
* unspellable, and a call that may not have a value goes through `fact` below.
*/
type Drawn = Exclude, boolean>
/**
* One term, and either what was read for it or the sentence standing where the
* value would be.
*
* The two are alternatives in the type because of what goes wrong otherwise: a
* fact renders anyway. A zero for a supply that never answered, a dash for a
* price nobody quoted, an em dash meaning "none" to whoever wrote it and "we
* did not look" to whoever reads it. Those are indistinguishable on screen and
* they are not the same fact — one is a thing nobody holds, the other is a node
* that timed out — so there is no way to spell a fact here without either a
* value or a stated reason there is none.
*
* `unread` is that reason, and it is about the read: the register did not
* answer, nobody quotes this, the record was never opened. A thing that was
* read and found to be nothing is a VALUE — "none", "not restricted", "no
* ticker" — and belongs on the other side of the union.
*/
export type Fact = { term: string } & (
| { value: Drawn; unread?: undefined }
| { value?: undefined; unread: string }
)
/**
* A fact out of a read that may not have answered, so the branch is written
* once instead of at every call site. `why` is what the page will say in the
* value's place, so it names what failed rather than announcing that something
* did.
*/
export const fact = (term: string, value: ReactNode, why: string): Fact =>
// The four ways a value can arrive as nothing. `false` is on the list
// because `known && ` is how a call site spells a conditional child
// in this language, and `false` renders as nothing at all — so without it the
// one shape the type exists to make unspellable is reachable by the most
// ordinary expression in the file. Zero is NOT on the list: a supply of zero
// is a reading.
value === undefined || value === null || value === '' || value === false
? { term, unread: why }
: { term, value: value as Drawn }
/**
* The facts themselves, which a block can also use on its own — the frame below
* is one caller of this and not its owner.
*
* A definition list, because that is what a screen reader navigates these by,
* so the grid goes on the `dl` itself. Each term sits ABOVE its value in a cell
* of its own, and that is the whole arrangement: a term-then-value ROW spends
* one of its two tracks on short labels, which leaves a wide screen running a
* single tall list down its left with nothing beside it. Stacked, the same
* facts go three across a desktop, two a tablet and one a phone, off the same
* track list the rest of the surface lays blocks out with, and no breakpoint is
* written down anywhere.
*
* The pair travels inside a box because a grid places CHILDREN: left bare, a
* term would land in one cell and its value in the next, and three facts would
* read as six. `dl > div > dt, dd` is the spelling the content model gives for
* grouping exactly this, so the list is still a list to anything reading it.
*
* A value arrives rendered, because whoever read it knows how it should read: a
* figure comes in the mono face, an address comes as code, a sentence comes as
* prose. What this decides is the other case, and it decides it the same way
* every time.
*/
export function Facts({ of, min = 260, max = 3 }: { of: readonly Fact[]; min?: number; max?: number }) {
return (
{of.map((f) => (
// Floored at zero width by hand. The rule that does this for every
// other grid on the surface is scoped to `Grid`'s own slot and this
// list is a `dl`, and a grid child left at its initial `auto` minimum is
// one an address can widen from the inside — forty characters setting
// the track, and every sibling column squeezed to pay for it.
{f.term}
{/* A block that flows its contents inline. A value is often several
pieces — a figure and the unit it is in, a badge after it — and
those are a sentence rather than a stack; a block also lets an
address bring its own row of controls. */}
{f.unread === undefined ? (
f.value
) : (
{f.unread}
)}
))}
)
}
export interface DetailProps {
/**
* Where this came from, named.
*
* A rendered trail rather than a route and a word: navigation belongs to the
* application, and a component that took an address would need the
* application's router to follow it.
*/
trail?: ReactNode
/** What this page is about, said as a person would say it. */
title: ReactNode
/** The line under it — a ticker, an address, an issuer. One line, not a
* summary. */
subtitle?: ReactNode
/** States worth seeing before anything else: restricted, frozen, out of
* range. Badges, in a row that wraps. */
badges?: ReactNode
/**
* What a person can do about the thing on this page.
*
* Beside the title rather than at the bottom, because a screen that describes
* something and offers no way to act on it sends the reader back to a list to
* find the verb.
*/
actions?: ReactNode
/** What is known, which includes what could not be read. */
facts?: readonly Fact[]
/**
* The blocks. Two across where there is room, stacked where there is not, on
* the same track the rest of the surface pairs blocks on. One that wants the
* full width — a table, or the only block there is — says so with the grid's
* own `Cell col="1 / -1"` rather than a second arrangement invented here.
*/
children?: ReactNode
}
/**
* The title track and the action track, named once.
*
* A grid rather than a row pushed apart, because the point is the EDGE: every
* page in the family puts its title on the same left rule and its actions on
* the same right one, and a flex row justified apart puts them wherever the two
* contents happen to end. `minmax(0, …)` is what lets a long title wrap instead
* of widening its own track and shoving the actions off the line.
*
* What it does not promise is a title track of one width across every route.
* The verbs' track is `auto`, so it is as wide as the verbs and nought wide
* where there are none — a page with nothing to do on it gives its heading the
* whole row. Reserving the width instead would buy an unmoving wrap point with
* a band of dead ground on every reading page in the family.
*/
const HEAD = 'minmax(0, 1fr) auto'
export function Detail({ trail, title, subtitle, badges, actions, facts, children }: DetailProps) {
return (
{trail ? (
{trail}
) : null}
{title}
{/* In the last track, so the verbs end on the row's right rule at
every route that offers any. Kept out of the document where there
are none: an `auto` track sized to an empty box is nought pixels
wide, so a placeholder reserves nothing — the title simply has the
row to itself, which is what a page with nothing to do on it
should look like. */}
{actions ? (
{actions}
) : null}
{subtitle ? (
{subtitle}
) : null}
{badges ? (
{badges}
) : null}
{facts?.length ? (
) : null}
{/* Sized to content, stated. A grid row is as tall as its tallest cell,
so a block holding two rows beside one holding ten is a block with a
void under it — which reads as unfinished rather than as a row. */}
{children ? (
{children}
) : null}
)
}