Public: anyone can read this; the crew can change it.
An app's card on /explore says how many forks it has, as a plain link to its fork list #14626
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14589-an-app-s-card-on-explore-says-how-many-f → main 3 commits fork 1 thread · 1 unresolved
7 files
changed +104
−12
src/actions/explore.ts +18 −5
| @@ -400,6 +400,12 @@ function shippedWords(shippedAt: string | null, now: number): string { | ||
| 400 | 400 | return ago === "" ? "" : `shipped ${ago}`; |
| 401 | 401 | } |
| 402 | 402 | |
| 403 | /** | |
| 404 | * Forks by people only (`core/front.ts`): the crew's working forks are on the repository's Forks | |
| 405 | * tab, so the words say which forks these are. | |
| 406 | */ | |
| 407 | export const forkWords = (forks: number): string => plural(forks, "fork by a person", "forks by people"); | |
| 408 | ||
| 403 | 409 | /** |
| 404 | 410 | * The counts of an app row's line, said in full ("3 forks by people, 5 ships, 2 likes"): forks and ships |
| 405 | 411 | * always, likes when there are some or the sort ranks by them, hours awake when the sort ranks by |
| @@ -407,18 +413,25 @@ function shippedWords(shippedAt: string | null, now: number): string { | ||
| 407 | 413 | * is not an app. |
| 408 | 414 | */ |
| 409 | 415 | export function figureCounts(row: ExploreRow, sort: ExploreSort = "score"): string { |
| 410 | if (row.kind !== "app") return ""; | |
| 416 | const { forks, rest } = figureParts(row, sort); | |
| 417 | return [forks, rest].filter((p) => p !== "").join(", "); | |
| 418 | } | |
| 419 | ||
| 420 | /** | |
| 421 | * The line's counts in two pieces: the fork count, then the rest, so a page can treat the forks on | |
| 422 | * their own. | |
| 423 | */ | |
| 424 | export function figureParts(row: ExploreRow, sort: ExploreSort = "score"): { forks: string; rest: string } { | |
| 425 | if (row.kind !== "app") return { forks: "", rest: "" }; | |
| 411 | 426 | const n = row.numbers; |
| 412 | return [ | |
| 413 | // Forks by people only (`core/front.ts`): the crew's working forks are on the repository's | |
| 414 | // Forks tab, so the words say which forks these are. | |
| 415 | plural(n.forks, "fork by a person", "forks by people"), | |
| 427 | const rest = [ | |
| 416 | 428 | plural(n.ships, "ship", "ships"), |
| 417 | 429 | n.likes > 0 || sort === "likes" ? plural(n.likes, "like", "likes") : "", |
| 418 | 430 | sort === "use" ? `${n.awakeHours} h in use` : "", |
| 419 | 431 | ] |
| 420 | 432 | .filter((p) => p !== "") |
| 421 | 433 | .join(", "); |
| 434 | return { forks: forkWords(n.forks), rest }; | |
| 422 | 435 | } |
| 423 | 436 | |
| 424 | 437 | /** |
src/web/studio/explore.ts +9 −3
| @@ -22,7 +22,7 @@ import { | ||
| 22 | 22 | MAKE_IT_YOURS, |
| 23 | 23 | shippedAgo, |
| 24 | 24 | whyOf, |
| 25 | figureCounts, | |
| 25 | figureParts, | |
| 26 | 26 | readExploreLine, |
| 27 | 27 | type ExploreQuery, |
| 28 | 28 | type ExploreRow, |
| @@ -48,8 +48,14 @@ function rowHtml(row: ExploreRow, query: ExploreQuery, now: number, weights: Fro | ||
| 48 | 48 | const why = whyOf(row, query.sort, now, weights); |
| 49 | 49 | // One row: what it does in plain words, then the one signal that explains its place. |
| 50 | 50 | // Apps say their counts and keep the dated `<time>`: the absolute date stays in its title. |
| 51 | const figures = figureCounts(row, query.sort); | |
| 52 | 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}`; | |
| 51 | const parts0 = figureParts(row, query.sort); | |
| 52 | const n = row.numbers; | |
| 53 | // Forks lead the line only when there are some, as a plain link to the app's fork list; at zero | |
| 54 | // the phrase is left out. | |
| 55 | const forkPiece: Html = n.forks === 0 ? html`` : row.repo === undefined ? html`${parts0.forks}` : html`<a href="/${row.repo.owner}/${row.repo.name}/forks">${parts0.forks}</a>`; | |
| 56 | const figured: Html = [forkPiece, html`${parts0.rest}`].filter((p) => p.toString() !== "").reduce((all, p, i) => (i === 0 ? p : html`${all}, ${p}`), html``); | |
| 57 | const figures = figured.toString(); | |
| 58 | const signal = figures !== "" ? (row.shippedAt === null ? html`${figured}, never shipped` : html`${figured}, 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}`; | |
| 53 | 59 | // One tap from a person to their apps; with none, the count is plain text. |
| 54 | 60 | const theirs = href({ kind: "app", sort: "score", text: "", archived: false, updated: "all", owner: row.name }); |
| 55 | 61 | const apps = row.kind === "person" ? `${row.numbers.apps} app${row.numbers.apps === 1 ? "" : "s"}` : ""; |
test/explore-api-numbers.test.ts +2 −1
| @@ -43,7 +43,8 @@ test("GET /api/v1/explore sends them for a seeded app", async () => { | ||
| 43 | 43 | expect([it.forks, it.ships, it.likes, it.updatedAt]).toEqual([0, 0, 0, null]); |
| 44 | 44 | expect(it.figures).toBe("0 forks by people, 0 ships, never shipped"); |
| 45 | 45 | const page = await (await fetch(`${servers.url}/explore?q=kind:app`)).text(); |
| 46 | expect(page).toContain("0 forks by people, 0 ships, never shipped"); | |
| 46 | expect(page).toContain("0 ships, never shipped"); | |
| 47 | expect(page).not.toContain("0 forks by people"); | |
| 47 | 48 | } finally { |
| 48 | 49 | servers.stop(); |
| 49 | 50 | app.close(); |
test/explore-first-view.test.ts +2 −1
| @@ -74,7 +74,8 @@ describe("/explore first view", () => { | ||
| 74 | 74 | test("a row is its name, what it does, and one signal; the filters are one line", () => { |
| 75 | 75 | expect(html).toContain("Does thing number 0 in plain words"); |
| 76 | 76 | // A row's meta is `description · signal`, one signal and no more. |
| 77 | const meta = [...html.matchAll(/class="row-meta">([^<]*)</g)].map((m) => m[1]); | |
| 77 | // The fork count is a link (#14589), so tags inside the meta are read as their text. | |
| 78 | const meta = [...html.matchAll(/class="row-meta">((?:[^<]|<a [^>]*>|<\/a>)*)</g)].map((m) => m[1]!.replace(/<[^>]+>/g, "")); | |
| 78 | 79 | expect(meta).toContain("1 fork by a person, 0 ships, never shipped · Does thing number 0 in plain words"); |
| 79 | 80 | expect(meta.some((m) => m!.endsWith("· Does thing number 1 in plain words"))).toBe(true); |
| 80 | 81 | for (const m of meta) expect(m!.split(" · ").length).toBeLessThanOrEqual(2); |
test/explore-fork-count.test.ts added +70
| @@ -0,0 +1,70 @@ | ||
| 1 | /** | |
| 2 | * #14589: an app's row on /explore says its fork count as a plain link to its fork list; at zero, | |
| 3 | * no link. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { newApp } from "../src/actions/apps.ts"; | |
| 11 | import { createSession } from "../src/core/sessions.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { quietLogs } from "./helpers/globals.ts"; | |
| 15 | import { promote } from "./helpers/members.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | let dataDir: string; | |
| 20 | let app: App; | |
| 21 | let servers: Servers; | |
| 22 | ||
| 23 | beforeAll(async () => { | |
| 24 | dataDir = await mkdtemp(join(tmpdir(), "forge-explore-fork-count-")); | |
| 25 | app = createApp({ dataDir, port: 0 }); | |
| 26 | servers = await serve(app); | |
| 27 | const users = []; | |
| 28 | for (const [i, username] of ["alice", "bob", "carol"].entries()) { | |
| 29 | const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: i === 0 }); | |
| 30 | if (!r.ok) throw new Error(r.error); | |
| 31 | promote(app.db, r.value); | |
| 32 | users.push(r.value); | |
| 33 | } | |
| 34 | for (const name of ["garden", "quiet"]) { | |
| 35 | const made = await newApp(app, users[0]!, { name, description: name, visibility: "public" }); | |
| 36 | if (!made.ok) throw new Error(String(made.error)); | |
| 37 | } | |
| 38 | for (const u of users.slice(1)) { | |
| 39 | const cookie = `forge_session=${createSession(app.db, u.id).token}`; | |
| 40 | const res = await fetch(`${servers.url}/alice/garden/make-it-yours`, { method: "POST", headers: { cookie, origin: servers.url }, redirect: "manual" }); | |
| 41 | if (res.status >= 400) throw new Error(`fork ${res.status}`); | |
| 42 | } | |
| 43 | app.db.run("UPDATE repo SET visibility = 'public'"); | |
| 44 | }, 60_000); | |
| 45 | ||
| 46 | afterAll(async () => { | |
| 47 | servers.stop(); | |
| 48 | app.close(); | |
| 49 | await rm(dataDir, { recursive: true, force: true }); | |
| 50 | }); | |
| 51 | ||
| 52 | const rowOf = (page: string, name: string): string => { | |
| 53 | const at = page.indexOf(`href="/alice/${name}"`); | |
| 54 | expect(at).toBeGreaterThan(-1); | |
| 55 | return page.slice(at, at + 700); | |
| 56 | }; | |
| 57 | ||
| 58 | test("an app with two forks links its count to its fork list", async () => { | |
| 59 | const page = await fetch(`${servers.url}/explore?q=kind:app`).then((r) => r.text()); | |
| 60 | expect(rowOf(page, "garden")).toContain('<a href="/alice/garden/forks">2 forks by people</a>'); | |
| 61 | }); | |
| 62 | ||
| 63 | test("an app with no forks says nothing about forks: no phrase, no link, ships kept", async () => { | |
| 64 | const page = await fetch(`${servers.url}/explore?q=kind:app`).then((r) => r.text()); | |
| 65 | const row = rowOf(page, "quiet").match(/class="row-meta">[^]*?<\/span>/)?.[0] ?? ""; | |
| 66 | expect(row).toContain("never shipped"); | |
| 67 | expect(row).not.toContain("fork"); | |
| 68 | expect(row).toContain("0 ships"); | |
| 69 | expect(page).not.toContain('href="/alice/quiet/forks"'); | |
| 70 | }); |
test/explore.test.ts +2 −1
| @@ -243,7 +243,8 @@ describe("explore", () => { | ||
| 243 | 243 | const page = await (await fetch(`${base}/explore?q=${encodeURIComponent("kind:app sort:ships")}`)).text(); |
| 244 | 244 | expect(page).toContain('value="kind:app sort:ships"'); |
| 245 | 245 | expect(page.indexOf('href="/bob/busy"')).toBeLessThan(page.indexOf('href="/alice/loved"')); |
| 246 | expect(page).toContain("0 forks by people, 3 ships, shipped <time datetime="); // the sort's own figure, with the dated time | |
| 246 | expect(page).toContain("3 ships, shipped <time datetime="); | |
| 247 | expect(page).not.toContain("0 forks by people"); | |
| 247 | 248 | expect(page).not.toContain("data-why"); |
| 248 | 249 | expect(page).toContain("Make it yours"); |
| 249 | 250 | expect(page).not.toContain("alice/secret"); |
test/explorer-why.test.ts +1 −1
| @@ -43,7 +43,7 @@ test("a seeded app's row on /explore carries the line, in one escaped meta line" | ||
| 43 | 43 | insertApp(app.db, { repoId: repo.value.id, slug: "alice-tool", access: "public" }); |
| 44 | 44 | const page = await (await fetch(`${servers.url}/explore?q=kind:app`)).text(); |
| 45 | 45 | const meta = [...page.matchAll(/class="row-meta">([^<]*)</g)].map((m) => m[1]); |
| 46 | expect(meta.some((m) => m === "0 forks by people, 0 ships, never shipped" || m!.startsWith("0 forks by people, 0 ships, never shipped ·"))).toBe(true); | |
| 46 | expect(meta.some((m) => m === "0 ships, never shipped" || m!.startsWith("0 ships, never shipped ·"))).toBe(true); | |
| 47 | 47 | expect(page).not.toContain("<b>x</b>"); |
| 48 | 48 | const api = (await (await fetch(`${servers.url}/api/v1/explore?q=kind:app`)).json()) as { items: { figures?: string }[] }; |
| 49 | 49 | expect(api.items[0]?.figures).toBe("0 forks by people, 0 ships, never shipped"); |
new above
offline
Principle 1/8 and box 1: at zero forks the phrase should not be shown at all. Also this recovers the link by checking figures.startsWith(forkLine) and slicing, with a non-null row.repo!; build the line from separate parts instead.