Public: anyone can read this; the crew can change it.
An app's explore row shows its last-ship date and fork count read from stored state, with no per-row query #13697
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13680-an-app-s-explore-row-shows-its-last-ship → main 4 commits fork
5 files
changed +82
−17
src/actions/explore.ts +13 −5
| @@ -345,14 +345,22 @@ export function exploreLine(query: ExploreQuery): string { | ||
| 345 | 345 | |
| 346 | 346 | const plural = (n: number, one: string, many: string): string => `${n} ${n === 1 ? one : many}`; |
| 347 | 347 | |
| 348 | /** | |
| 349 | * When a row last shipped, in words ("today", "yesterday", "3 days ago") on the caller's one clock. | |
| 350 | */ | |
| 351 | export function shippedAgo(shippedAt: string, now: number): string { | |
| 352 | const days = Math.floor((now - Date.parse(shippedAt)) / 86_400_000); | |
| 353 | if (!Number.isFinite(days)) return ""; | |
| 354 | if (days < 1) return "today"; | |
| 355 | if (days === 1) return "yesterday"; | |
| 356 | return `${days} days ago`; | |
| 357 | } | |
| 358 | ||
| 348 | 359 | /** How long ago a row last shipped, in words; "" when it never did. */ |
| 349 | 360 | function shippedWords(shippedAt: string | null, now: number): string { |
| 350 | 361 | if (shippedAt === null) return ""; |
| 351 | const days = Math.floor((now - Date.parse(shippedAt)) / 86_400_000); | |
| 352 | if (!Number.isFinite(days)) return ""; | |
| 353 | if (days < 1) return "shipped today"; | |
| 354 | if (days === 1) return "shipped yesterday"; | |
| 355 | return `shipped ${days} days ago`; | |
| 362 | const ago = shippedAgo(shippedAt, now); | |
| 363 | return ago === "" ? "" : `shipped ${ago}`; | |
| 356 | 364 | } |
| 357 | 365 | |
| 358 | 366 | /** |
src/web/studio/explore.ts +2 −1
| @@ -16,6 +16,7 @@ import { | ||
| 16 | 16 | exploreNote, |
| 17 | 17 | exploreSuggestion, |
| 18 | 18 | MAKE_IT_YOURS, |
| 19 | shippedAgo, | |
| 19 | 20 | whyOf, |
| 20 | 21 | figureCounts, |
| 21 | 22 | readExploreLine, |
| @@ -42,7 +43,7 @@ function rowHtml(row: ExploreRow, signedIn: boolean, query: ExploreQuery, now: n | ||
| 42 | 43 | // One row: what it does in plain words, then the one signal that explains its place. |
| 43 | 44 | // Apps say their counts and keep the dated `<time>`: the absolute date stays in its title. |
| 44 | 45 | const figures = figureCounts(row, query.sort); |
| 45 | const signal = figures !== "" ? (row.shippedAt === null ? html`${figures}, never shipped` : html`${figures}, shipped ${time(row.shippedAt)}`) : why === undefined ? "" : why.signal === "fresh" && row.shippedAt !== null ? html`shipped ${time(row.shippedAt)}` : html`${why.text}`; | |
| 46 | const signal = figures !== "" ? (row.shippedAt === null ? html`${figures}, never shipped` : html`${figures}, shipped ${time(row.shippedAt, { text: shippedAgo(row.shippedAt, now) })}`) : why === undefined ? "" : why.signal === "fresh" && row.shippedAt !== null ? html`shipped ${time(row.shippedAt, { text: shippedAgo(row.shippedAt, now) })}` : html`${why.text}`; | |
| 46 | 47 | // One tap from a person to their apps; with none, the count is plain text. |
| 47 | 48 | const theirs = href({ kind: "app", sort: "score", text: "", archived: false, updated: "all", owner: row.name }); |
| 48 | 49 | const apps = row.kind === "person" ? `${row.numbers.apps} app${row.numbers.apps === 1 ? "" : "s"}` : ""; |
src/web/ui.ts +22 −9
| @@ -55,13 +55,13 @@ export function timeAgo(iso: string): string { | ||
| 55 | 55 | * `date` words it as the calendar day (`2026-12-02`), for an instant ahead |
| 56 | 56 | * of now, which "ago" cannot name. |
| 57 | 57 | */ |
| 58 | export function time(iso: string, opts: { date?: boolean } = {}): Html { | |
| 58 | export function time(iso: string, opts: { date?: boolean; text?: string } = {}): Html { | |
| 59 | 59 | const parsed = Date.parse(iso); |
| 60 | 60 | if (Number.isNaN(parsed)) return html``; |
| 61 | 61 | const at = new Date(parsed); |
| 62 | 62 | const exact = `${at.toISOString().slice(0, 16).replace("T", " ")} UTC`; |
| 63 | 63 | return html`<time datetime="${at.toISOString()}" title="${exact}" |
| 64 | >${opts.date === true ? at.toISOString().slice(0, 10) : timeAgo(iso)}</time | |
| 64 | >${opts.text ?? (opts.date === true ? at.toISOString().slice(0, 10) : timeAgo(iso))}</time | |
| 65 | 65 | >`; |
| 66 | 66 | } |
| 67 | 67 | |
| @@ -185,7 +185,10 @@ export interface BtnOptions { | ||
| 185 | 185 | extra?: string; |
| 186 | 186 | /** The element's id, for a script or a fragment link that finds it. */ |
| 187 | 187 | id?: string; |
| 188 | /** A pager's link says which way it goes (`rel="next"`): a reader of the list follows it (`bind.ts`). */ | |
| 188 | /** | |
| 189 | * A pager's link says which way it goes (`rel="next"`): a reader of the list follows it | |
| 190 | * (`bind.ts`). | |
| 191 | */ | |
| 189 | 192 | rel?: "next" | "prev"; |
| 190 | 193 | } |
| 191 | 194 | |
| @@ -712,7 +715,9 @@ export function repoCounts(o: { | ||
| 712 | 715 | openPulls?: number | null | undefined; |
| 713 | 716 | /** Some origins report one number for issues *and* pulls; say so. */ |
| 714 | 717 | issuesIncludePulls?: boolean; |
| 715 | /** The repository we hold whose lists these count (`bind.ts`): absent for an origin's own numbers. */ | |
| 718 | /** | |
| 719 | * The repository we hold whose lists these count (`bind.ts`): absent for an origin's own numbers. | |
| 720 | */ | |
| 716 | 721 | of?: { ownerName: string; name: string } | undefined; |
| 717 | 722 | }): Html { |
| 718 | 723 | const issues = o.openIssues ?? null; |
| @@ -777,7 +782,9 @@ export function chartBox(chart: Chart, cells: readonly Cell[], title: string): H | ||
| 777 | 782 | return chartFrame(chart, drawChart(chart, cells, title)); |
| 778 | 783 | } |
| 779 | 784 | |
| 780 | /** The same box around a chart already drawn (`drawChart`'s own markup, from whoever read its rows). */ | |
| 785 | /** | |
| 786 | * The same box around a chart already drawn (`drawChart`'s own markup, from whoever read its rows). | |
| 787 | */ | |
| 781 | 788 | export function chartFrame(chart: Chart, drawn: string): Html { |
| 782 | 789 | return html`<div class="chart-box${chart === "table" ? " is-table" : ""}">${raw(drawn)}</div>`; |
| 783 | 790 | } |
| @@ -971,8 +978,9 @@ export interface RowOptions { | ||
| 971 | 978 | href?: string; |
| 972 | 979 | title: Html | string; |
| 973 | 980 | meta?: Html; |
| 974 | /** Facts about the row, one `dt`/`dd` pair each, in a `dl.row-facts` under the meta. With facts the row's | |
| 975 | * main column is a `<div>`, so the `dl` is valid HTML: not for a `fold` row, whose `<summary>` takes phrasing only. */ | |
| 981 | /** Facts about the row, one `dt`/`dd` pair each, in a `dl.row-facts` under the meta. With facts | |
| 982 | * the row's main column is a `<div>`, so the `dl` is valid HTML: not for a `fold` row, whose | |
| 983 | * `<summary>` takes phrasing only. */ | |
| 976 | 984 | facts?: readonly { label: string; value: Html | string }[]; |
| 977 | 985 | /** Draw `.row-meta` even when `meta` is empty, for a script to write into. */ |
| 978 | 986 | metaSlot?: boolean; |
| @@ -997,7 +1005,9 @@ export interface RowOptions { | ||
| 997 | 1005 | /** The row folds: it is the `<summary>` of a `<details>` holding `body`, |
| 998 | 1006 | * open when `open` says so. Its `lead` is the caret. */ |
| 999 | 1007 | fold?: { id: string; open: boolean; body: Html }; |
| 1000 | /** Why the list's count leaves this row out on purpose (`data-apart`, `bind.ts`): a sealed report. */ | |
| 1008 | /** | |
| 1009 | * Why the list's count leaves this row out on purpose (`data-apart`, `bind.ts`): a sealed report. | |
| 1010 | */ | |
| 1001 | 1011 | apart?: string | undefined; |
| 1002 | 1012 | /** The row is a node of an ARIA tree: the row itself is the `treeitem` (it is what takes focus, |
| 1003 | 1013 | * so it says its level and, for a branch, `aria-expanded`), its `<li>` is `role="none"`, |
| @@ -1092,7 +1102,10 @@ export function tabs(items: TabItem[], ariaLabel: string): Html { | ||
| 1092 | 1102 | /** A segmented control built from links (a GET filter is navigation). */ |
| 1093 | 1103 | export function seg( |
| 1094 | 1104 | items: { label: string; href: string; current: boolean; count?: number }[], |
| 1095 | /** These links are a list's tabs: every item a reader may open is under one (`data-rows-tabs`, `bind.ts`). */ | |
| 1105 | /** | |
| 1106 | * These links are a list's tabs: every item a reader may open is under one (`data-rows-tabs`, | |
| 1107 | * `bind.ts`). | |
| 1108 | */ | |
| 1096 | 1109 | filters = false, |
| 1097 | 1110 | ): Html { |
| 1098 | 1111 | return html`<div class="seg"${filters ? raw(" data-rows-tabs") : null}> |
test/explore-metrics.test.ts added +43
| @@ -0,0 +1,43 @@ | ||
| 1 | /** | |
| 2 | * An explore row's freshness and fork count come from batched reads: the statements do not grow | |
| 3 | * with the rows (#13680). | |
| 4 | */ | |
| 5 | import { expect, test } from "bun:test"; | |
| 6 | import { snappyFixture } from "../bench/snappy.ts"; | |
| 7 | import { explore, parseExploreQuery, shippedAgo } from "../src/actions/explore.ts"; | |
| 8 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 9 | ||
| 10 | async function statementsFor(repos: number): Promise<{ statements: number; rows: number }> { | |
| 11 | const f = await snappyFixture({ repos, issuesPerRepo: 0 }); | |
| 12 | try { | |
| 13 | for (const repo of f.repos) { | |
| 14 | const made = insertApp(f.app.db, { repoId: repo.id, slug: `${repo.ownerName}-${repo.name}`, access: "public" }); | |
| 15 | if (!made.ok) throw new Error(made.error); | |
| 16 | } | |
| 17 | // Passing a clock skips the kept ranking, so every row is read for real. | |
| 18 | const query = parseExploreQuery("kind:app sort:fresh"); | |
| 19 | await explore(f.app, undefined, query, Date.now()); | |
| 20 | f.counter.reset(); | |
| 21 | const rows = await explore(f.app, undefined, query, Date.now()); | |
| 22 | return { statements: f.counter.count, rows: rows.length }; | |
| 23 | } finally { | |
| 24 | await f.close(); | |
| 25 | } | |
| 26 | } | |
| 27 | ||
| 28 | test("a full page of rows costs the same number of statements as a few", async () => { | |
| 29 | const small = await statementsFor(6); | |
| 30 | const large = await statementsFor(30); | |
| 31 | // The fixture makes every third repository public, so 6 repositories show 2 rows and 30 show 10: | |
| 32 | // the statements must not grow with the rows. | |
| 33 | expect(small.rows).toBe(2); | |
| 34 | expect(large.rows).toBe(10); | |
| 35 | expect(large.statements).toBe(small.statements); | |
| 36 | }, 120_000); | |
| 37 | ||
| 38 | test("the last ship is told in days on the caller's clock", () => { | |
| 39 | const now = Date.parse("2026-11-15T12:00:00Z"); | |
| 40 | expect(shippedAgo("2026-11-12T11:00:00Z", now)).toBe("3 days ago"); | |
| 41 | expect(shippedAgo("2026-11-14T11:00:00Z", now)).toBe("yesterday"); | |
| 42 | expect(shippedAgo("2026-11-15T01:00:00Z", now)).toBe("today"); | |
| 43 | }); |
test/explore-parity.test.ts +2 −2
| @@ -323,9 +323,9 @@ describe("separate parameters", () => { | ||
| 323 | 323 | }); |
| 324 | 324 | |
| 325 | 325 | describe("last ship on the row", () => { |
| 326 | test("the page words it as shipped 1d ago with the absolute date in title; the API carries shippedAt", async () => { | |
| 326 | test("the page words it as shipped yesterday with the absolute date in title; the API carries shippedAt", async () => { | |
| 327 | 327 | const html = await (await fetch(`${servers.url}/explore?q=${encodeURIComponent("kind:app sort:fresh")}`)).text(); |
| 328 | expect(html).toMatch(/shipped <time datetime="[^"]+" title="\d{4}-\d\d-\d\d \d\d:\d\d UTC"\s*>1d ago<\/time/); | |
| 328 | expect(html).toMatch(/shipped <time datetime="[^"]+" title="\d{4}-\d\d-\d\d \d\d:\d\d UTC"\s*>yesterday<\/time/); | |
| 329 | 329 | const api = (await (await fetch(`${servers.url}/api/v1/explore?q=${encodeURIComponent("kind:app sort:fresh")}`)).json()) as { items: { name: string; shippedAt: string | null }[] }; |
| 330 | 330 | expect(api.items[0]!.shippedAt).not.toBeNull(); |
| 331 | 331 | }); |
new above
offline