diff --git a/web/src/lib/routes.ts b/web/src/lib/routes.ts index c51b687..609e95e 100644 --- a/web/src/lib/routes.ts +++ b/web/src/lib/routes.ts @@ -46,7 +46,7 @@ export const DEFAULT_WINDOW: WindowName = '3600' * Window names this site used to emit, mapped to what they became. * * A link somebody sent last week still has to land on the right page. The - * router rewrites these to the block count rather than serving them, so the + * router rewrites these to the current form rather than serving them, so the * address bar never keeps a name the site no longer stands behind. */ export const LEGACY_WINDOWS: Record = { @@ -57,6 +57,23 @@ export const LEGACY_WINDOWS: Record = { week: '100800', } +/** + * The window as a path segment: `3600-blocks`. + * + * The unit is in the segment because the count alone is not a distinct shape — + * a bare `3600` is also a perfectly good block height, and `isBlockRef` says so + * two lines before the window is even considered. Shipping `/quantus/3600` + * sent the standings straight to block 3,600. + * + * That is the trap this module's own header warns about: "Shape works until two + * kinds of thing can look alike... Naming the kind costs six characters and + * removes the whole class of problem." A window named for its block count had + * to name the unit too, or it was just a number again. + */ +export function windowPath(window: WindowName): string { + return `${window}-blocks` +} + /** What the address bar currently says the page should show. */ export interface Route { /** `null` means "whichever chain the backend puts first". */ @@ -123,15 +140,22 @@ export const EMPTY: Route = { } function asWindow(segment: string | undefined): WindowName | null { - const current = WINDOWS.find((w) => w.id === segment)?.id + // The empty segment is checked first rather than leaning on `&&`: that form + // returns `''` for an empty path, which is neither a window nor null. + if (!segment) return null + + const suffixed = segment.endsWith('-blocks') ? segment.slice(0, -'-blocks'.length) : null + const current = WINDOWS.find((w) => w.id === suffixed)?.id if (current) return current + // A link from before the rename. Resolved here rather than rejected, so an // address somebody shared still opens the page it named — `href` will then - // write the block count back into the bar. + // write the current form back into the bar. // - // The empty segment is checked separately rather than leaning on `&&`: that - // form returns `''` for an empty path, which is neither a window nor null. - if (!segment) return null + // Bare counts are deliberately *not* accepted, though the site briefly + // emitted them: `/quantus/3600` is indistinguishable from block 3,600, and + // guessing between them would make one of the two silently open the wrong + // page. A window that cannot be told apart from a height is not a window. return LEGACY_WINDOWS[segment] ?? null } @@ -222,7 +246,7 @@ export function href(route: Partial): string { const chain = route.chain ?? '' if (route.block) return `/${chain}/block/${route.block}` if (route.miner) { - const window = route.window ?? DEFAULT_WINDOW + const window = windowPath(route.window ?? DEFAULT_WINDOW) return chain ? `/${chain}/miner/${route.miner}/${window}` : `/miner/${route.miner}/${window}` } if (route.call) return `/${chain}/call/${route.call[0]}/${route.call[1]}` @@ -232,7 +256,7 @@ export function href(route: Partial): string { if (route.runtime !== null && route.runtime !== undefined) { return chain ? `/${chain}/runtime/${route.runtime}` : `/runtime/${route.runtime}` } - return `/${chain}/${route.window ?? DEFAULT_WINDOW}` + return `/${chain}/${windowPath(route.window ?? DEFAULT_WINDOW)}` } /**