Skip to content

Commit a9e2279

Browse files
committed
Merge pull request 'fix(web): Grow's shortcut keys no longer die when a stale conversation is remembered' (pingdotgg#37) from fix/grow-shortcuts into yu-v2
2 parents 1cbf244 + 2e86d41 commit a9e2279

4 files changed

Lines changed: 189 additions & 53 deletions

File tree

‎apps/web/src/components/RightPanelTabs.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ const SURFACE_DISABLED_REASONS = {
163163
/** Overlays that must win over the launcher's letter shortcuts. */
164164
const LAUNCHER_SHORTCUT_BLOCKING_LAYERS = [
165165
"[data-manager-overlay]",
166+
"[data-home-overlay]",
166167
'[data-slot="dialog-popup"]',
167168
'[data-slot="alert-dialog-popup"]',
168169
'[data-slot="command-dialog-popup"]',

‎apps/web/src/components/grow/GrowReturns.tsx‎

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { useAtomCommand } from "../../state/use-atom-command";
5252
import { isPreviewSupportedInRuntime } from "../../previewStateStore";
5353
import { formatRelativeTimeLabel } from "../../timestampFormat";
5454
import ChatView from "../ChatView";
55+
import { growKeyAction } from "./grow.keys";
5556
import { ExpandedImageDialog } from "../chat/ExpandedImageDialog";
5657
import { WORK_REASON_LOOK, useReportImages, useThreadReport } from "../home/ThreadReportCard";
5758
import { Button } from "../ui/button";
@@ -256,71 +257,66 @@ export function GrowReturns(props: {
256257
setWhatIfOpen(false);
257258
}
258259

260+
// A conversation only shows while its thread is still in the relations; a key
261+
// handler that trusted `thread` alone kept the deck's keys dead behind a deck.
262+
const conversationOpen = thread !== null && relations.byKey.has(thread);
263+
useEffect(() => {
264+
if (ready && thread !== null && !conversationOpen) useReturnsStore.setState({ thread: null });
265+
}, [conversationOpen, ready, thread]);
266+
259267
useEffect(() => {
260268
const onKey = (event: KeyboardEvent) => {
261-
if (event.metaKey || event.ctrlKey || event.altKey || event.isComposing) return;
262269
if (isTyping(event.target)) return;
263-
// While a conversation is open, the deck's keys stay off; Esc goes back.
264-
if (thread !== null) {
265-
if (event.key === "o") {
266-
useReturnsStore.setState({ thread: null });
267-
event.preventDefault();
268-
}
269-
return;
270-
}
270+
const action = growKeyAction(event, { conversationOpen, item: current });
271+
if (action === null) return;
271272
const item = current;
272-
const step = (direction: 1 | -1) => select(growStep(shown, item?.key ?? null, direction));
273-
const used = () => event.preventDefault();
274-
switch (event.key) {
275-
case "j":
276-
case "ArrowDown":
277-
step(1);
278-
return used();
279-
case "k":
280-
case "ArrowUp":
281-
step(-1);
282-
return used();
283-
case "u":
284-
if (undo()) used();
285-
return;
286-
case "/":
273+
switch (action.type) {
274+
case "closeThread":
275+
useReturnsStore.setState({ thread: null });
276+
break;
277+
case "step":
278+
select(growStep(shown, item?.key ?? null, action.direction));
279+
break;
280+
case "undo":
281+
if (!undo()) return;
282+
break;
283+
case "search":
287284
document.querySelector<HTMLElement>("[data-grow-search]")?.focus();
288-
return used();
289-
}
290-
if (item === null) return;
291-
const open = item.kind === "return" || item.kind === "urgent";
292-
if (event.key === "s" && item.kind === "return" && item.shippable) {
293-
actions.ship(item);
294-
used();
295-
} else if (event.key === "d" && item.kind === "return") {
296-
actions.done(item);
297-
used();
298-
} else if ((event.key === "w" || event.key === "r") && open) {
299-
setWhatIfOpen(true);
300-
used();
301-
} else if (event.key === "x" && open) {
302-
actions.letGo(item);
303-
used();
304-
} else if (event.key === "a" && open && !item.thread.statusProof) {
305-
void actions.askProof(item);
306-
used();
307-
} else if (event.key === "o") {
308-
actions.openThread(item);
309-
used();
310-
} else if ((event.key === "p" || event.key === "Enter") && item.playUrl !== null) {
311-
if (wide) useReturnsStore.setState({ playing: playing === item.key ? null : item.key });
312-
else window.open(item.playUrl, "_blank", "noopener");
313-
used();
285+
break;
286+
case "whatIf":
287+
setWhatIfOpen(true);
288+
break;
289+
case "ship":
290+
if (item !== null) actions.ship(item);
291+
break;
292+
case "done":
293+
if (item !== null) actions.done(item);
294+
break;
295+
case "letGo":
296+
if (item !== null) actions.letGo(item);
297+
break;
298+
case "askProof":
299+
if (item !== null) void actions.askProof(item);
300+
break;
301+
case "openThread":
302+
if (item !== null) actions.openThread(item);
303+
break;
304+
case "play":
305+
if (item === null || item.playUrl === null) return;
306+
if (wide) useReturnsStore.setState({ playing: playing === item.key ? null : item.key });
307+
else window.open(item.playUrl, "_blank", "noopener");
308+
break;
314309
}
310+
event.preventDefault();
315311
};
316312
window.addEventListener("keydown", onKey);
317313
return () => window.removeEventListener("keydown", onKey);
318-
}, [actions, current, playing, shown, thread, wide]);
314+
}, [actions, conversationOpen, current, playing, shown, wide]);
319315

320316
// Esc leaves an open conversation even from its message box, which keeps Esc
321317
// from bubbling; caught before it, and left alone while a menu or dialog is open.
322318
useEffect(() => {
323-
if (thread === null) return;
319+
if (!conversationOpen) return;
324320
const onEscape = (event: KeyboardEvent) => {
325321
if (event.key !== "Escape" || event.isComposing) return;
326322
if (document.querySelector('[role="menu"], [role="listbox"], [role="dialog"]')) return;
@@ -330,7 +326,7 @@ export function GrowReturns(props: {
330326
};
331327
window.addEventListener("keydown", onEscape, true);
332328
return () => window.removeEventListener("keydown", onEscape, true);
333-
}, [thread]);
329+
}, [conversationOpen]);
334330

335331
if (!ready) return <ReturnsSkeleton />;
336332

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { growKeyAction, type GrowKeyState } from "./grow.keys";
4+
5+
const key = (value: string, modifiers: Record<string, boolean> = {}) => ({
6+
key: value,
7+
metaKey: false,
8+
ctrlKey: false,
9+
altKey: false,
10+
isComposing: false,
11+
...modifiers,
12+
});
13+
14+
const returnItem = (overrides: Record<string, unknown> = {}) =>
15+
({
16+
kind: "return",
17+
shippable: true,
18+
playUrl: "https://preview.example",
19+
thread: { statusProof: null },
20+
...overrides,
21+
}) as unknown as NonNullable<GrowKeyState["item"]>;
22+
23+
const deck = (item: GrowKeyState["item"] = returnItem()): GrowKeyState => ({
24+
conversationOpen: false,
25+
item,
26+
});
27+
28+
describe("growKeyAction", () => {
29+
it("maps each Grow shortcut to its action on a return", () => {
30+
const actions = ["s", "d", "w", "r", "x", "a", "o", "p", "Enter", "j", "k", "u", "/"].map(
31+
(value) => growKeyAction(key(value), deck())?.type,
32+
);
33+
expect(actions).toEqual([
34+
"ship",
35+
"done",
36+
"whatIf",
37+
"whatIf",
38+
"letGo",
39+
"askProof",
40+
"openThread",
41+
"play",
42+
"play",
43+
"step",
44+
"step",
45+
"undo",
46+
"search",
47+
]);
48+
});
49+
50+
it("moves between cards with j, k and the arrows", () => {
51+
expect(growKeyAction(key("j"), deck())).toEqual({ type: "step", direction: 1 });
52+
expect(growKeyAction(key("ArrowDown"), deck())).toEqual({ type: "step", direction: 1 });
53+
expect(growKeyAction(key("k"), deck())).toEqual({ type: "step", direction: -1 });
54+
expect(growKeyAction(key("ArrowUp"), deck())).toEqual({ type: "step", direction: -1 });
55+
expect(growKeyAction(key("j"), deck(null))).toEqual({ type: "step", direction: 1 });
56+
});
57+
58+
it("leaves a key alone when its action does not apply", () => {
59+
expect(growKeyAction(key("s"), deck(returnItem({ shippable: false })))).toBeNull();
60+
expect(growKeyAction(key("a"), deck(returnItem({ thread: { statusProof: {} } })))).toBeNull();
61+
expect(growKeyAction(key("p"), deck(returnItem({ playUrl: null })))).toBeNull();
62+
expect(growKeyAction(key("s"), deck(returnItem({ kind: "urgent" })))).toBeNull();
63+
expect(growKeyAction(key("x"), deck(returnItem({ kind: "letGo" })))).toBeNull();
64+
expect(growKeyAction(key("s"), deck(null))).toBeNull();
65+
});
66+
67+
it("ignores keys held with a modifier or typed through an input method", () => {
68+
expect(growKeyAction(key("s", { ctrlKey: true }), deck())).toBeNull();
69+
expect(growKeyAction(key("s", { metaKey: true }), deck())).toBeNull();
70+
expect(growKeyAction(key("s", { altKey: true }), deck())).toBeNull();
71+
expect(growKeyAction(key("s", { isComposing: true }), deck())).toBeNull();
72+
});
73+
74+
it("keeps the deck's keys off while a conversation shows, and o goes back", () => {
75+
const conversation: GrowKeyState = { conversationOpen: true, item: returnItem() };
76+
expect(growKeyAction(key("o"), conversation)).toEqual({ type: "closeThread" });
77+
for (const value of ["s", "d", "w", "x", "a", "j", "k", "p", "Enter"]) {
78+
expect(growKeyAction(key(value), conversation)).toBeNull();
79+
}
80+
});
81+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { GrowReturn } from "./returns.logic";
2+
3+
export type GrowKeyAction =
4+
| { readonly type: "step"; readonly direction: 1 | -1 }
5+
| { readonly type: "undo" }
6+
| { readonly type: "search" }
7+
| { readonly type: "ship" }
8+
| { readonly type: "done" }
9+
| { readonly type: "whatIf" }
10+
| { readonly type: "letGo" }
11+
| { readonly type: "askProof" }
12+
| { readonly type: "openThread" }
13+
| { readonly type: "closeThread" }
14+
| { readonly type: "play" };
15+
16+
export interface GrowKeyState {
17+
/** A conversation is showing in place of the deck. */
18+
readonly conversationOpen: boolean;
19+
readonly item: GrowReturn | null;
20+
}
21+
22+
/**
23+
* What a key does on the Grow screen. Pure, so each key has a test; the screen
24+
* only supplies whether a conversation is showing and which item is current.
25+
* While a conversation shows, the deck's keys stay off and `o` goes back.
26+
*/
27+
export function growKeyAction(
28+
event: Pick<KeyboardEvent, "key" | "metaKey" | "ctrlKey" | "altKey" | "isComposing">,
29+
state: GrowKeyState,
30+
): GrowKeyAction | null {
31+
if (event.metaKey || event.ctrlKey || event.altKey || event.isComposing) return null;
32+
if (state.conversationOpen) return event.key === "o" ? { type: "closeThread" } : null;
33+
switch (event.key) {
34+
case "j":
35+
case "ArrowDown":
36+
return { type: "step", direction: 1 };
37+
case "k":
38+
case "ArrowUp":
39+
return { type: "step", direction: -1 };
40+
case "u":
41+
return { type: "undo" };
42+
case "/":
43+
return { type: "search" };
44+
}
45+
const { item } = state;
46+
if (item === null) return null;
47+
const open = item.kind === "return" || item.kind === "urgent";
48+
if (event.key === "s" && item.kind === "return" && item.shippable) return { type: "ship" };
49+
if (event.key === "d" && item.kind === "return") return { type: "done" };
50+
if ((event.key === "w" || event.key === "r") && open) return { type: "whatIf" };
51+
if (event.key === "x" && open) return { type: "letGo" };
52+
if (event.key === "a" && open && !item.thread.statusProof) return { type: "askProof" };
53+
if (event.key === "o") return { type: "openThread" };
54+
if ((event.key === "p" || event.key === "Enter") && item.playUrl !== null) {
55+
return { type: "play" };
56+
}
57+
return null;
58+
}

0 commit comments

Comments
 (0)