Public: anyone can read this; the crew can change it.
/explore narrows by two plain links, 'live now' and 'changed this week', that stack and survive a reload #14532
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/14481-explore-narrows-by-two-plain-links-live → main 6 commits fork 3 threads · 3 unresolved
4 files
changed +133
−4
docs/features/explore.md +4 −2
| @@ -51,16 +51,18 @@ What a person filters on `/explore` an agent filters with the same words. Each f | ||
| 51 | 51 | |---|---|---|---| |
| 52 | 52 | | Best / Forks / Shipped / Liked / Use / Fresh / Newest | `sort=score` `forks` `ships` `likes` `use` `fresh` `new` (`name` by hand) | `?sort=forks` | `{ sort: "forks" }` | |
| 53 | 53 | | All / Apps / People / Forges (admin) | `kind=app` `person` `forge` | `?kind=app` | `{ kind: "app" }` | |
| 54 | | Any time / Today / This week / This month | `since=1d` `7d` `30d` (`updated=` is the same) | `?since=7d` | `{ since: "7d" }` | | |
| 54 | | Any time / Today / This week / This month | `since=1d` `7d` `30d` (`updated=` is the same; `week=1` is `7d`, and `updated`/`since` win beside it) | `?since=7d` | `{ since: "7d" }` | | |
| 55 | 55 | | Show archived | `archived=yes` | `?archived=yes` | `{ archived: "yes" }` | |
| 56 | 56 | | (owner link on a row) | `owner=name` | `?owner=name` | `{ owner: "name" }` | |
| 57 | 57 | | (type it) at least N forks by people | `forks=3` (0–1000) | `?forks=3` | `{ forks: "3" }` | |
| 58 | 58 | | (type it) the same floor, as shared links say it | `min_forks=3` (`forks=` wins when both are given); not a whole number from 0 to 1000 is refused naming `min_forks`: a 303 on the page, a `422` on the API | `?min_forks=3` | `{ min_forks: "3" }` | |
| 59 | 59 | | (type it) at least N merges in 30 days | `ships=1` (0–1000) | `?ships=1` | `{ ships: "1" }` | |
| 60 | | (type it) awake in the last 30 days | `live=yes` (`no` is no filter) | `?live=yes` | `{ live: "yes" }` | | |
| 60 | | Live now (under Sort and time) | `live=yes` (`1` is the same, `no` or `0` is no filter; any other value is ignored) | `?live=yes` | `{ live: "yes" }` | | |
| 61 | 61 | | Mine (signed in, beside the kind tabs) | `mine=yes` (`no` is no filter) | `?mine=yes` | `{ mine: "yes" }` | |
| 62 | 62 | | the search box | `q=words` | `?q=words` | `{ q: "words" }` | |
| 63 | 63 | |
| 64 | **Live now** means awake in the last 30 days, not a deployment up at this moment. `/explore?live=1&week=1` shows apps that are both live and changed this week, and reloads the same; the link toggles, keeps the sort, and a bad `live` or `week` is ignored, never an error (`test/explore-quick-filters.test.ts`). The terminal needs no door: its line already takes `live:yes` and `updated:7d`. | |
| 65 | ||
| 64 | 66 | The one line still works everywhere: `?q=kind:app sort:forks garden` and `{ q: "kind:app sort:forks garden" }` read the same as the separate parameters, and old `/explore?q=…` links keep working. When a parameter and the same key in `q` are both given, the parameter wins (`?q=sort:name&sort=forks` sorts by forks). A parameter's value is one word; one the explorer does not take is an unknown word like any typed in `q`, and is refused the same way (below). The terminal's search line takes only the line. It takes `forks:N`, `ships:N` and `live:yes` there too (`screens/dashboard.ts` reads the line with `readExploreLine`): `forks:1 live:no` leaves the rows the page leaves, and `forks:abc` or `live:maybe` is named under `Read as:` as `forks:abc is not a count`, `live:maybe is not an answer`. |
| 65 | 67 | |
| 66 | 68 | A bad `min_forks`, `since`, `updated` or `kind` parameter (`?since=9y`, `?kind=nope`) is refused naming the parameter (#14046, #14392): the API answers `422`: `{ "error": "invalid", "field": "since", "message": "since is not a window — use 1d, 7d, 30d" }`, and the page answers `303` to the same address without it, then draws the same sentence once over the unfiltered list. The sentence names the parameter and never repeats the value. `since` is judged only when `updated` is not given, `min_forks` only when `forks` is not. |
src/actions/explore.ts +7 −1
| @@ -154,6 +154,8 @@ export function badExploreParam(get: (name: string) => string | null | undefined | ||
| 154 | 154 | return null; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | const LIVE_WORDS: Record<string, string> = { 1: "yes", yes: "yes", 0: "no", no: "no" }; | |
| 158 | ||
| 157 | 159 | /** |
| 158 | 160 | * The one line a request asks for: `q`, then each separate parameter as its |
| 159 | 161 | * `key:value` word. A value is one word (whitespace removed, so it cannot |
| @@ -165,9 +167,13 @@ export function exploreLineFrom(get: (name: string) => string | null | undefined | ||
| 165 | 167 | for (const key of EXPLORE_PARAMS) { |
| 166 | 168 | // `since` is the same window as `updated`, and `min_forks` the same floor as `forks`. |
| 167 | 169 | const raw = get(key) || (key === "updated" ? get("since") : key === "forks" ? get("min_forks") : "") || ""; |
| 168 | const value = raw.replace(/\s+/g, "").slice(0, 100); | |
| 170 | let value = raw.replace(/\s+/g, "").slice(0, 100); | |
| 171 | // `live` also reads 1 and 0; a value it does not know is ignored, not an error. | |
| 172 | if (key === "live") value = LIVE_WORDS[value.toLowerCase()] ?? ""; | |
| 169 | 173 | if (value !== "") given.set(key, value); |
| 170 | 174 | } |
| 175 | // `week=1` is `updated:7d`; any other value, and `updated`/`since` beside it, win or are ignored. | |
| 176 | if (!given.has("updated") && (get("week") ?? "").trim() === "1") given.set("updated", "7d"); | |
| 171 | 177 | // A parameter wins: the same key typed in `q` is dropped, not left to the reader's order. |
| 172 | 178 | const typed = cappedQuery(get("q")) |
| 173 | 179 | .split(/\s+/) |
src/web/studio/explore.ts +10 −1
| @@ -349,7 +349,7 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 349 | 349 | ? html`` |
| 350 | 350 | : html` Did you mean <a href="${href(readExploreLine(suggestion.line).query)}">${suggestion.fix}</a>?` |
| 351 | 351 | }</p>`} |
| 352 | <details class="mb small" data-more-filters${query.sort !== "score" || query.updated !== "all" || query.archived ? " open" : ""}> | |
| 352 | <details class="mb small" data-more-filters${query.sort !== "score" || query.updated !== "all" || query.live === true || query.archived ? " open" : ""}> | |
| 353 | 353 | <summary class="muted sort-toggle">Sort and time</summary> |
| 354 | 354 | <div class="bar"> |
| 355 | 355 | ${seg( |
| @@ -366,6 +366,15 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 366 | 366 | current: query.updated === w.updated, |
| 367 | 367 | })), |
| 368 | 368 | )} |
| 369 | <div role="group" aria-label="Live now: awake in the last 30 days"> | |
| 370 | ${seg([ | |
| 371 | { | |
| 372 | label: "Live now", | |
| 373 | href: href(query.live === true ? dropped(query, "live") : { ...query, live: true }), | |
| 374 | current: query.live === true, | |
| 375 | }, | |
| 376 | ])} | |
| 377 | </div> | |
| 369 | 378 | <a |
| 370 | 379 | class="muted home-doors" |
| 371 | 380 | href="${href({ ...query, archived: !query.archived })}" |
test/explore-quick-filters.test.ts added +112
| @@ -0,0 +1,112 @@ | ||
| 1 | /** /explore?live=1 and ?week=1: two plain links that narrow the ranked list and stack (#14481). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 9 | import { createIssue } from "../src/core/issues.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | import { proveAll } from "./helpers/verified.ts"; | |
| 14 | import { promote } from "./helpers/members.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | let dir: string; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | const DAY = 86_400_000; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | dir = await mkdtemp(join(tmpdir(), "forge-explore-quick-")); | |
| 24 | app = createApp({ dataDir: dir, port: 0 }); | |
| 25 | const made = await createUser(app.db, { origin: "seed", username: "olive", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: false }); | |
| 26 | if (!made.ok) throw new Error(made.error); | |
| 27 | proveAll(app); | |
| 28 | promote(app.db, made.value); | |
| 29 | // [name, merged days ago | null, awake] | |
| 30 | const plan: [string, number | null, boolean][] = [ | |
| 31 | ["both", 1, true], | |
| 32 | ["liveonly", 20, true], | |
| 33 | ["weekonly", 2, false], | |
| 34 | ["neither", 20, false], | |
| 35 | ]; | |
| 36 | for (const [n, ago, awake] of plan) { | |
| 37 | const repo = await createRepo(app, made.value, { name: n, visibility: "public" }); | |
| 38 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 39 | const rec = insertApp(app.db, { repoId: repo.value.id, slug: `olive-${n}`, access: "public" }); | |
| 40 | if (!rec.ok) throw new Error(rec.error); | |
| 41 | if (ago !== null) { | |
| 42 | const issue = createIssue(app.db, { repoId: repo.value.id, authorId: made.value.id, title: "change", kind: "pull" }); | |
| 43 | if (!issue.ok) throw new Error(issue.error); | |
| 44 | app.db.query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', 'b', '', '', ?2)`).run(issue.value.id, new Date(Date.now() - ago * DAY).toISOString()); | |
| 45 | } | |
| 46 | if (awake) | |
| 47 | app.db.run("INSERT INTO meter (repo_id, env, placement, started_at, beat_at, ended_at) VALUES (?1, 'production', 'owned', ?2, ?3, ?3)", [ | |
| 48 | repo.value.id, | |
| 49 | new Date(Date.now() - 2 * DAY - 3_600_000).toISOString(), | |
| 50 | new Date(Date.now() - 2 * DAY).toISOString(), | |
| 51 | ]); | |
| 52 | } | |
| 53 | servers = await serve(app); | |
| 54 | }, 120_000); | |
| 55 | afterAll(async () => { | |
| 56 | await servers.stop(); | |
| 57 | app.db.close(); | |
| 58 | await rm(dir, { recursive: true, force: true }); | |
| 59 | }); | |
| 60 | ||
| 61 | const get = async (qs: string) => { | |
| 62 | const res = await fetch(`${servers.url}/explore${qs}`, { redirect: "manual" }); | |
| 63 | return { status: res.status, html: await res.text() }; | |
| 64 | }; | |
| 65 | const names = (html: string): string[] => { | |
| 66 | const list = /<ul class="list">([\s\S]*?)<\/ul>/.exec(html); | |
| 67 | return list === null ? [] : [...list[1]!.matchAll(/<li[^>]*>[\s\S]*?<a href="\/olive\/([^"?]*)">/g)].map((m) => m[1]!).sort(); | |
| 68 | }; | |
| 69 | const link = (html: string, label: string): { tag: string; href: string } => { | |
| 70 | const m = new RegExp(`<a\\s[^>]*>\\s*${label}\\s*</a`).exec(html); | |
| 71 | if (m === null) throw new Error(`no link ${label}`); | |
| 72 | return { tag: m[0], href: /href="([^"]*)"/.exec(m[0])![1]!.replaceAll("&", "&") }; | |
| 73 | }; | |
| 74 | const ALL = ["both", "liveonly", "neither", "weekonly"]; | |
| 75 | ||
| 76 | test("live=1 keeps only apps awake", async () => { | |
| 77 | expect(names((await get("?kind=app&live=1")).html)).toEqual(["both", "liveonly"]); | |
| 78 | }); | |
| 79 | ||
| 80 | test("week=1 keeps only apps changed in under 7 days", async () => { | |
| 81 | expect(names((await get("?kind=app&week=1")).html)).toEqual(["both", "weekonly"]); | |
| 82 | }); | |
| 83 | ||
| 84 | test("both together intersect", async () => { | |
| 85 | expect(names((await get("?kind=app&live=1&week=1")).html)).toEqual(["both"]); | |
| 86 | }); | |
| 87 | ||
| 88 | test("a bad value is ignored: no redirect, no narrowing", async () => { | |
| 89 | for (const qs of ["?kind=app&live=maybe", "?kind=app&week=soon", "?kind=app&week=0&live=x"]) { | |
| 90 | const res = await get(qs); | |
| 91 | expect(res.status).toBe(200); | |
| 92 | expect(names(res.html)).toEqual(ALL); | |
| 93 | } | |
| 94 | }); | |
| 95 | ||
| 96 | test("the links sit on the page, show active when on, and keep the sort", async () => { | |
| 97 | const bare = (await get("?kind=app&sort=forks")).html; | |
| 98 | expect(link(bare, "Live now").tag).not.toContain("aria-current"); | |
| 99 | expect(link(bare, "Live now").href).toContain("sort=forks"); | |
| 100 | expect(link(bare, "Live now").href).toContain("live=yes"); | |
| 101 | const on = (await get("?kind=app&sort=forks&live=1&week=1")).html; | |
| 102 | expect(link(on, "Live now").tag).toContain('aria-current="page"'); | |
| 103 | // The week is the one "This week" link in Sort and time, not a second control. | |
| 104 | expect(link(on, "This week").tag).toContain('aria-current="page"'); | |
| 105 | expect(on).not.toContain("Changed this week"); | |
| 106 | expect(on).toContain('aria-label="Live now: awake in the last 30 days"'); | |
| 107 | // Following the link survives a reload: the address alone says it. | |
| 108 | expect(names((await get(link(bare, "Live now").href.replace("/explore", ""))).html)).toEqual(["both", "liveonly"]); | |
| 109 | // Active, the link takes the filter off and keeps the sort. | |
| 110 | expect(link(on, "Live now").href).toContain("sort=forks"); | |
| 111 | expect(link(on, "Live now").href).not.toContain("live"); | |
| 112 | }); |
new above
offline