import { useEffect, useState, type CSSProperties } from 'react' import { paint } from '../material/paint' import type { Figures } from './figure' /** * How long ago something happened, kept current while it is on screen. * * A list of events is the one place a venue's condition is legible without a * chart: somebody can see in the timestamps whether anything happened today, * which is a question a flat line answers ambiguously and a column of stale * relative times answers wrongly. So the figure is re-read on a timer — a row * that says "2m" for an hour is worse than one that says nothing, because it * reads as fresh. * * Which unit applies is arithmetic and is here. How the unit is written is a * fact about a language and belongs to `figure.ts`, which asks CLDR for it: * English writes `10s` and `1d`, French `10min` and `1j`, Russian `10 мин` with * the space it puts there. So there is no `s`, `m`, `h` or `d` in this file for * anyone to translate. */ /** The units a span is stated in, as `Figures.span` names them. */ export type Unit = 'second' | 'minute' | 'hour' | 'day' /** * How long between two moments, as a count and the unit it is counted in. * * Both moments are milliseconds since the epoch or a `Date`, which is what * everything else in this package that takes a moment takes. A chain states its * timestamps in seconds; multiplying by a thousand at the call site is one * visible step, where a function taking one of each is a silent thousand-fold * error the first time the arguments are read in the wrong order. * * Never negative: a clock a little ahead of the reader's is not a thing that * happens in the future, it is a thing that happened just now. */ export function ago(at: Date | number, now: Date | number): readonly [number, Unit] { const s = Math.max(0, Math.floor((Number(now) - Number(at)) / 1000)) if (s < 60) return [s, 'second'] const m = Math.floor(s / 60) if (m < 60) return [m, 'minute'] const h = Math.floor(m / 60) if (h < 24) return [h, 'hour'] return [Math.floor(h / 24), 'day'] } /** How often the figure is worked out again. Fifteen seconds, which is under * the minute the first unit steps at, so nothing is ever a step behind. */ const BEAT = 15_000 /** Who is drawing a moment right now. */ const watching = new Set<(at: number) => void>() /** The one timer, running only while something is watching it. */ let beat: ReturnType | undefined /** * One clock, however many moments are on the page. * * A moment is a cell, and a list of activity is fifty of them. A timer inside * each is fifty timers on fifty phases: the column re-reads at fifty different * instants, so a reader watching it sees the figures step raggedly one at a * time rather than the column moving at once. Everything drawing a moment is * drawing the same moment, so there is one reading of it. * * The timer starts with the first watcher and stops with the last, so a page * with no moment on it has nothing running. */ export const watch = (tell: (at: number) => void): (() => void) => { watching.add(tell) beat ??= setInterval(() => { const at = Date.now() for (const one of watching) one(at) }, BEAT) return () => { watching.delete(tell) if (watching.size > 0) return clearInterval(beat) beat = undefined } } /** * The voice a moment is drawn in: quiet, because it qualifies the row rather * than being the row, and tabular, because a column of these is read down. */ const when: CSSProperties = { color: paint.quiet, fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap', } /** * A moment, as the time since it, in a real `time` element. * * The element and not a text primitive, for the same reason `Code` is: `time` * is already semantic, `dateTime` carries the machine-readable moment for * anything parsing the page, and the primitive would lay the row's shortest * cell out as a flex box. * * The exact moment is on it as well, in the reader's own calendar rather than * in UTC — an abbreviation is for scanning and the tooltip is where somebody * goes when scanning was not enough. */ export function Ago({ at, figures }: { at: Date | number; figures: Figures }) { const [now, setNow] = useState(() => Date.now()) useEffect(() => watch(setNow), []) return ( ) }