Skip to content

Commit 9dd9426

Browse files
t3dotggclaude
andcommitted
fix(web): custom thread undo rules see the open terminal and preview
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0f18e37 commit 9dd9426

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useAtomValue } from "@effect/atom-react";
22
import * as Schema from "effect/Schema";
33
import {
44
useEffect,
5+
useRef,
56
useState,
67
useSyncExternalStore,
78
type CSSProperties,
@@ -205,6 +206,19 @@ function SidebarControl() {
205206
);
206207
}
207208

209+
function routePanelsOpen(routeThreadRef: ReturnType<typeof useThreadRouteRef>) {
210+
if (!routeThreadRef) return { terminalOpen: false, previewOpen: false };
211+
return {
212+
terminalOpen: selectThreadTerminalUiState(
213+
useTerminalUiStateStore.getState().terminalUiStateByThreadKey,
214+
routeThreadRef,
215+
).terminalOpen,
216+
previewOpen:
217+
selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, routeThreadRef) ===
218+
"preview",
219+
};
220+
}
221+
208222
function NavigationHistoryShortcuts() {
209223
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
210224
const routeThreadRef = useThreadRouteRef();
@@ -221,17 +235,8 @@ function NavigationHistoryShortcuts() {
221235
const command = resolveShortcutCommand(event, keybindings, {
222236
context: {
223237
terminalFocus: isTerminalFocused(),
224-
terminalOpen: routeThreadRef
225-
? selectThreadTerminalUiState(
226-
useTerminalUiStateStore.getState().terminalUiStateByThreadKey,
227-
routeThreadRef,
228-
).terminalOpen
229-
: false,
230238
previewFocus: isPreviewFocused(),
231-
previewOpen: routeThreadRef
232-
? selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, routeThreadRef) ===
233-
"preview"
234-
: false,
239+
...routePanelsOpen(routeThreadRef),
235240
editableFocus: isEditableFocused(event.target),
236241
modelPickerOpen: isModelPickerOpen(),
237242
},
@@ -258,27 +263,30 @@ const MODIFIER_KEYS = new Set(["Alt", "Control", "Meta", "Shift"]);
258263
// so mod+z reaches it before the composer's own undo handler consumes the key.
259264
function ThreadUndoShortcut() {
260265
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
266+
const routeThreadRef = useThreadRouteRef();
261267
// A collapsed sidebar hides the notice, so text fields keep their own undo.
262268
const noticeVisible = useSidebarVisibility();
269+
// Outlives the effect: undoing an archive navigates, which re-runs it mid-hold.
270+
const heldUndoKey = useRef<string | null>(null);
263271

264272
useEffect(() => {
265-
let heldUndoKey: string | null = null;
266273
const onKeyDown = (event: KeyboardEvent) => {
267274
// A held key undoes once. Cancel its repeats so they cannot fall through
268275
// to the editor's own undo, even after the last notice is gone.
269276
if (event.repeat) {
270-
if (event.code === heldUndoKey) {
277+
if (event.code === heldUndoKey.current) {
271278
event.preventDefault();
272279
event.stopPropagation();
273280
}
274281
return;
275282
}
276283
// A modifier tapped mid-hold leaves the held key repeating.
277-
if (!MODIFIER_KEYS.has(event.key)) heldUndoKey = null;
284+
if (!MODIFIER_KEYS.has(event.key)) heldUndoKey.current = null;
278285
const command = resolveShortcutCommand(event, keybindings, {
279286
context: {
280287
terminalFocus: isTerminalFocused(),
281288
previewFocus: isPreviewFocused(),
289+
...routePanelsOpen(routeThreadRef),
282290
editableFocus:
283291
isEditableFocused(event.target) && !(noticeVisible && threadUndoClaimsShortcut()),
284292
modelPickerOpen: isModelPickerOpen(),
@@ -290,7 +298,7 @@ function ThreadUndoShortcut() {
290298
}
291299
if (isCommandPaletteOpen() || isModelPickerOpen()) return;
292300
if (undoLatestThreadAction()) {
293-
heldUndoKey = event.code;
301+
heldUndoKey.current = event.code;
294302
event.preventDefault();
295303
event.stopPropagation();
296304
}
@@ -304,7 +312,7 @@ function ThreadUndoShortcut() {
304312
window.removeEventListener("keydown", onKeyDown, true);
305313
window.removeEventListener("input", onInput, true);
306314
};
307-
}, [keybindings, noticeVisible]);
315+
}, [keybindings, noticeVisible, routeThreadRef]);
308316

309317
return null;
310318
}

0 commit comments

Comments
 (0)