@@ -52,6 +52,7 @@ import { useAtomCommand } from "../../state/use-atom-command";
5252import { isPreviewSupportedInRuntime } from "../../previewStateStore";
5353import { formatRelativeTimeLabel } from "../../timestampFormat";
5454import ChatView from "../ChatView";
55+ import { growKeyAction } from "./grow.keys";
5556import { ExpandedImageDialog } from "../chat/ExpandedImageDialog";
5657import { WORK_REASON_LOOK, useReportImages, useThreadReport } from "../home/ThreadReportCard";
5758import { 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
0 commit comments