Public: anyone can read this; the crew can change it.

The home's list of what needs the owner is driven by four keys: up, down, enter, back #17061

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/17050-the-home-s-list-of-what-needs-the-owner → main 0 commits fork

  • Merged. scout/17050-the-home-s-list-of-what-needs-the-owner was merged into main as 5668ae0.

Review

Sign in to review this change.
7 files changed +112 −6
docs/features/cockpit-keys.md added +18
@@ -0,0 +1,18 @@
1 ---
2 topic: Getting around: keys, the palette and search
3 row: What needs you answers four keys: up, down, enter, back
4 ---
5 # What needs you answers four keys (#17050)
6
7 The keys live on the asks receipt page (where a bulk step lands) and the waits view that renders the same list (at most five cards at a time). The home's live `/live/on-you` fragment ships no script, so they do not run there yet. The arrows drive it, so a TV remote works too.
8
9 | Key | What it does |
10 | --- | --- |
11 | Down / Up | move a visible focus ring over the cards, stopping at the first and the fifth |
12 | Enter | on a focused card, press its primary button, else its first link or button |
13 | Esc, Backspace (only while a card holds focus) | leave: back one page, or home when there is none |
14 | a remote's Back (GoBack, BrowserBack, 461, 10009) | the same, always |
15
16 - Cards carry `data-key-card`; the section carries `data-keys` (how many). The script (`src/web/cockpit_keys.ts`, under 1 KB) reads them and makes no request of its own; Enter presses a button the card already has.
17 - Keys typed in a field, or with Ctrl, Alt or Meta, are left alone.
18 - Tests: `test/cockpit_keys.test.ts`.
scripts/unused-exports-allow.json −1
@@ -175,7 +175,6 @@
175175 "src/web/studio/home_cache.ts setHomeClock",
176176 "src/web/studio/home_last.ts forgetHome",
177177 "src/web/studio/review_evidence.ts rememberStat",
178 "src/snapshot/stream.ts streamLag",
179178 "src/web/sw.ts swSource",
180179 "src/spawn/machines.ts forgetMachines",
181180 "src/spawn/machines.ts registerMachine",
src/web/cockpit_keys.ts added +20
@@ -0,0 +1,20 @@
1 /**
2 * The keys that drive what needs the owner (#17050): up and down move a visible focus over the
3 * cards (`data-key-card`, the first `data-keys` of them), enter takes a card's first action, and
4 * back leaves (escape and backspace only from a focused card, a TV remote's back key always). The
5 * markup is server-rendered; this only moves focus and presses a button already there, so it makes
6 * no request of its own. Zero dependencies, under 1 KB.
7 */
8 export const cockpitKeysJs = `document.addEventListener('keydown',function(e){
9 var s=document.getElementById('needs-you');if(!s||e.altKey||e.ctrlKey||e.metaKey)return;
10 var t=e.target,k=e.key,n=+s.getAttribute('data-keys')||5;
11 if(t&&t.closest&&t.closest('input,textarea,select,[contenteditable]'))return;
12 var c=[].filter.call(s.querySelectorAll('[data-key-card]'),function(x){return x.offsetParent}).slice(0,n);
13 var i=c.indexOf(document.activeElement);
14 if(k==='ArrowDown'||k==='ArrowUp'){if(!c.length)return;
15 i=k==='ArrowDown'?Math.min(i+1,c.length-1):i<0?0:Math.max(i-1,0);
16 c[i].focus();e.preventDefault()}
17 else if(k==='Enter'&&i>=0&&t===c[i]){var b=(c[i].querySelector('[data-variant=primary]')||c[i].querySelector('a,button'));if(b){e.preventDefault();b.click()}}
18 else if(((k==='Escape'||k==='Backspace')&&i>=0)||k==='GoBack'||k==='BrowserBack'||e.keyCode===461||e.keyCode===10009){
19 e.preventDefault();history.length>1?history.back():location.assign('/')}
20 });`;
src/web/studio/asks_page.ts +10 −4
@@ -52,6 +52,7 @@ import {
5252 import { html, type Html } from "../html.ts";
5353 import { noteLine } from "./note.ts";
5454 import { page, redirect } from "../layout.ts";
55 import { cockpitKeysJs } from "../cockpit_keys.ts";
5556 import { emptyList } from "../empty_words.ts";
5657 import { btn, card, disclosure, icon, row } from "../ui.ts";
5758 import {
@@ -238,6 +239,7 @@ function decisionCard(ctx: Ctx, d: Decision, picked: ReadonlySet<string>): Html
238239 ${acts(ctx, d)}`,
239240 {
240241 id: askAnchor(d.id),
242 attrs: KEY_CARD,
241243 head: html`${pick(d, picked)}
242244 <h2>${title}</h2>
243245 <span class="muted small"><b>${KIND_LABEL[d.kind]}</b> · ${d.repo.ownerName}/${d.repo.name}</span
@@ -370,6 +372,9 @@ function filterNote(ctx: Ctx, rows: readonly AskRow<Decision>[], hidden: number)
370372 */
371373 export const NEXT_ASK = "next-ask";
372374
375 /** What the cockpit keys read (`web/cockpit_keys.ts`): a card they can move focus to. */
376 const KEY_CARD = html`data-key-card tabindex="-1"`;
377
373378 /** A row and, under it, the chip that mutes its repository (`asksMute`). */
374379 function mutableRow(ctx: Ctx, r: AskRow<Decision>, picked: ReadonlySet<string>): Html {
375380 return html`${rowCard(ctx, r, picked)}
@@ -409,6 +414,7 @@ function rowCard(ctx: Ctx, r: AskRow<Decision>, picked: ReadonlySet<string>): Ht
409414 {
410415 pad: false,
411416 id: askAnchor(r.id),
417 attrs: KEY_CARD,
412418 head: html`${BULK_KINDS.has(r.kind)
413419 ? html`<label class="check"><input
414420 type="checkbox"
@@ -434,7 +440,7 @@ function rowCard(ctx: Ctx, r: AskRow<Decision>, picked: ReadonlySet<string>): Ht
434440 * own row so it never wraps into the sentence.
435441 */
436442 export const forgeCard = (d: ForgeNeed): Html =>
437 html`<section class="stack">
443 html`<section class="stack" ${KEY_CARD}>
438444 <h2>The forge</h2>
439445 <p role="alert"><span class="dot" data-tone="danger" aria-hidden="true"></span> ${d.links === undefined ? html`<a href="${d.href}">${d.line}</a>` : html`${d.line}${needLinks(d)}`}</p>
440446 <div class="bar">${d.cause.startsWith("probe:") ? btn("Remove", { href: d.href, size: "sm", variant: "primary" }) : null}${btn("Health", { href: "/api/v1/health", size: "sm" })}</div>
@@ -445,7 +451,7 @@ export const forgeCard = (d: ForgeNeed): Html =>
445451 * repository's (and the stack's recommended choice, with a receipt and an undo).
446452 */
447453 export const asleepCard = (a: Asleep): Html =>
448 html`<section class="stack">
454 html`<section class="stack" ${KEY_CARD}>
449455 <h2>A stopped app</h2>
450456 <p><span class="dot" data-tone="warn" aria-hidden="true"></span> <a href="/${a.repo.ownerName}/${a.repo.name}">${asleepLine(a)}</a></p>
451457 <div class="bar">${btn("Open it", { href: `/${a.repo.ownerName}/${a.repo.name}`, size: "sm" })}</div>
@@ -627,7 +633,7 @@ export function needsYouSection(
627633 : more("asks-more", rest.length, html`${rest.map((r) => mutableRow(ctx, r, picked))}`);
628634 // The asks come first, so the first Tab after #needs-you lands on one;
629635 // the bulk step acts on what was picked, after them.
630 const body = html`<section id="needs-you" class="stack">
636 const body = html`<section id="needs-you" class="stack" data-keys="${ASKS_SHOWN}">
631637 <h2 id="asks-sentence">${askSentence(count)}</h2>
632638 ${top?.banner ?? null}
633639 ${needs.map(forgeCard)} ${asleep.map(asleepCard)}
@@ -663,7 +669,7 @@ export function needsYouSection(
663669 ${filtered} ${silencedNote} ${asideNote} ${undo}
664670 ${decisions.map((d) => decisionForm(d, ctx.url.pathname))}
665671 </section>`;
666 return { body, scriptJs: SELECT_JS };
672 return { body, scriptJs: `${SELECT_JS}\n${cockpitKeysJs}` };
667673 }
668674
669675 /** The asks page with a bulk step's preview or receipt on top — where every bulk answer lands. */
src/web/style/components.ts +2
@@ -424,6 +424,8 @@ kbd {
424424 /* Cards stacked in normal flow space themselves. Inside a container that
425425 already manages its own spacing with gap, that margin would double it. */
426426 .card + .card { margin-top: 12px; }
427 /* The card the cockpit keys (web/cockpit_keys.ts) stand on. */
428 [data-key-card]:focus-visible { outline: var(--ring); outline-offset: var(--ring-offset); }
427429 /* The open pulls a held issue waits on: a wrapping line of touch-sized links. */
428430 .held-pulls { display: flex; flex-wrap: wrap; gap: 0 4px; align-items: center; }
429431 .held-pulls a { display: inline-flex; align-items: center; min-height: 44px; padding: 0 8px; }
test/asks.test.ts +1 −1
@@ -165,7 +165,7 @@ describe("the asks page", () => {
165165 expect(headerCount(page)).toBe(1);
166166 // The section is §5's .stack: the page-local .needs-you class is gone
167167 // from the markup, while the words, links and forms stay (#4189).
168 expect(page).toContain('<section id="needs-you" class="stack">');
168 expect(page).toContain('<section id="needs-you" class="stack" data-keys="5">');
169169 expect(page).not.toContain("needs-you ");
170170 expect(page).not.toMatch(/class="[^"]*\bneeds-you\b/);
171171 expect(page).toContain('href="/');
test/cockpit_keys.test.ts added +61
@@ -0,0 +1,61 @@
1 /** The four keys over what needs the owner (#17050). */
2 import { expect, test } from "bun:test";
3 import { forgeCard } from "../src/web/studio/asks_page.ts";
4 import { cockpitKeysJs } from "../src/web/cockpit_keys.ts";
5
6 test("the cards carry the markup the keys read", () => {
7 const out = String(forgeCard({ cause: "errors", line: "Errors.", href: "/x" }));
8 expect(out).toContain("data-key-card");
9 expect(out).toContain('tabindex="-1"');
10 });
11
12 test("the script is under 1 KB and makes no request", () => {
13 expect(cockpitKeysJs.length).toBeLessThan(1024);
14 expect(cockpitKeysJs).not.toMatch(/fetch|XMLHttpRequest|sendBeacon|eval|innerHTML/);
15 });
16
17 function drive(keys: { key: string; keyCode?: number }[], current = -1, primary = false) {
18 const clicked: number[] = [];
19 const back: string[] = [];
20 const cards = [0, 1, 2, 3, 4, 5].map((n) => ({
21 offsetParent: {},
22 focus() { active = this as (typeof cards)[number]; },
23 querySelector: (q: string) => (q.includes("primary") && !primary ? null : { click: () => clicked.push(q.includes("primary") ? n + 100 : n) }),
24 }));
25 let active: (typeof cards)[number] | null = current < 0 ? null : (cards[current] ?? null);
26 let handler: (e: unknown) => void = () => {};
27 const section = { getAttribute: () => "5", querySelectorAll: () => cards };
28 const doc = {
29 addEventListener: (_: string, f: typeof handler) => (handler = f),
30 getElementById: () => section,
31 get activeElement() { return active; },
32 };
33 new Function("document", "history", "location", cockpitKeysJs)(doc, { length: 2, back: () => back.push("back") }, { assign: (u: string) => back.push(u) });
34 for (const k of keys) {
35 const target = k.key === "Enter" ? active : { closest: () => null };
36 handler({ ...k, target, preventDefault() {} });
37 }
38 return { cards, active: () => active === null ? -1 : cards.indexOf(active), clicked, back };
39 }
40
41 test("down and up move focus and stop at the five cards, enter presses the first action, back leaves", () => {
42 const down = Array.from({ length: 9 }, () => ({ key: "ArrowDown" }));
43 expect(drive(down).active()).toBe(4);
44 expect(drive([{ key: "ArrowDown" }, { key: "ArrowDown" }, { key: "ArrowUp" }]).active()).toBe(0);
45 expect(drive([{ key: "ArrowDown" }, { key: "Enter" }]).clicked).toEqual([0]);
46 expect(drive([{ key: "Enter" }]).clicked).toEqual([]);
47 expect(drive([{ key: "Escape" }], 0).back).toEqual(["back"]);
48 expect(drive([{ key: "x", keyCode: 10009 }]).back).toEqual(["back"]);
49 });
50
51 test("escape and backspace are left alone when no card holds focus; the remote's back is not", () => {
52 expect(drive([{ key: "Escape" }]).back).toEqual([]);
53 expect(drive([{ key: "Backspace" }]).back).toEqual([]);
54 expect(drive([{ key: "Backspace" }], 2).back).toEqual(["back"]);
55 expect(drive([{ key: "GoBack" }]).back).toEqual(["back"]);
56 });
57
58 test("enter presses the primary action before the first link", () => {
59 expect(drive([{ key: "Enter" }], 1, true).clicked).toEqual([101]);
60 expect(drive([{ key: "Enter" }], 1, false).clicked).toEqual([1]);
61 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs