@@ -2,14 +2,21 @@ import { useAtomValue } from "@effect/atom-react";
22import * as Schema from "effect/Schema" ;
33import {
44 useEffect ,
5+ useRef ,
56 useState ,
67 useSyncExternalStore ,
78 type CSSProperties ,
89 type ReactNode ,
910} from "react" ;
1011import { useLocation , useNavigate } from "@tanstack/react-router" ;
1112
13+ import { isCommandPaletteOpen } from "../commandPaletteBus" ;
1214import { isElectron } from "../env" ;
15+ import {
16+ releaseThreadUndoShortcut ,
17+ threadUndoClaimsShortcut ,
18+ undoLatestThreadAction ,
19+ } from "../hooks/showThreadUndoNotice" ;
1320import { getLocalStorageItem , removeLocalStorageItem } from "../hooks/useLocalStorage" ;
1421import {
1522 isRichTextBoldShortcut ,
@@ -199,6 +206,19 @@ function SidebarControl() {
199206 ) ;
200207}
201208
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+
202222function NavigationHistoryShortcuts ( ) {
203223 const keybindings = useAtomValue ( primaryServerKeybindingsAtom ) ;
204224 const routeThreadRef = useThreadRouteRef ( ) ;
@@ -215,17 +235,8 @@ function NavigationHistoryShortcuts() {
215235 const command = resolveShortcutCommand ( event , keybindings , {
216236 context : {
217237 terminalFocus : isTerminalFocused ( ) ,
218- terminalOpen : routeThreadRef
219- ? selectThreadTerminalUiState (
220- useTerminalUiStateStore . getState ( ) . terminalUiStateByThreadKey ,
221- routeThreadRef ,
222- ) . terminalOpen
223- : false ,
224238 previewFocus : isPreviewFocused ( ) ,
225- previewOpen : routeThreadRef
226- ? selectActiveRightPanel ( useRightPanelStore . getState ( ) . byThreadKey , routeThreadRef ) ===
227- "preview"
228- : false ,
239+ ...routePanelsOpen ( routeThreadRef ) ,
229240 editableFocus : isEditableFocused ( event . target ) ,
230241 modelPickerOpen : isModelPickerOpen ( ) ,
231242 } ,
@@ -245,6 +256,67 @@ function NavigationHistoryShortcuts() {
245256 return null ;
246257}
247258
259+ const MODIFIER_KEYS = new Set ( [ "Alt" , "Control" , "Meta" , "Shift" ] ) ;
260+
261+ // Undoes the thread actions in the sidebar's notice. Mounted wherever the
262+ // thread sidebar can show; Settings replaces it. Listens in the capture phase
263+ // so mod+z reaches it before the composer's own undo handler consumes the key.
264+ function ThreadUndoShortcut ( ) {
265+ const keybindings = useAtomValue ( primaryServerKeybindingsAtom ) ;
266+ const routeThreadRef = useThreadRouteRef ( ) ;
267+ // A collapsed sidebar hides the notice, so text fields keep their own undo.
268+ const noticeVisible = useSidebarVisibility ( ) ;
269+ // Outlives the effect: undoing an archive navigates, which re-runs it mid-hold.
270+ const heldUndoKey = useRef < string | null > ( null ) ;
271+
272+ useEffect ( ( ) => {
273+ const onKeyDown = ( event : KeyboardEvent ) => {
274+ // A held key undoes once. Cancel its repeats so they cannot fall through
275+ // to the editor's own undo, even after the last notice is gone.
276+ if ( event . repeat ) {
277+ if ( event . code === heldUndoKey . current ) {
278+ event . preventDefault ( ) ;
279+ event . stopPropagation ( ) ;
280+ }
281+ return ;
282+ }
283+ // A modifier tapped mid-hold leaves the held key repeating.
284+ if ( ! MODIFIER_KEYS . has ( event . key ) ) heldUndoKey . current = null ;
285+ const command = resolveShortcutCommand ( event , keybindings , {
286+ context : {
287+ terminalFocus : isTerminalFocused ( ) ,
288+ previewFocus : isPreviewFocused ( ) ,
289+ ...routePanelsOpen ( routeThreadRef ) ,
290+ editableFocus :
291+ isEditableFocused ( event . target ) && ! ( noticeVisible && threadUndoClaimsShortcut ( ) ) ,
292+ modelPickerOpen : isModelPickerOpen ( ) ,
293+ } ,
294+ } ) ;
295+ if ( command !== "thread.undo" ) {
296+ if ( ! MODIFIER_KEYS . has ( event . key ) ) releaseThreadUndoShortcut ( ) ;
297+ return ;
298+ }
299+ if ( isCommandPaletteOpen ( ) || isModelPickerOpen ( ) ) return ;
300+ if ( undoLatestThreadAction ( ) ) {
301+ heldUndoKey . current = event . code ;
302+ event . preventDefault ( ) ;
303+ event . stopPropagation ( ) ;
304+ }
305+ } ;
306+ // Pastes from a menu, dictation and drops edit text without a keypress.
307+ const onInput = ( ) => releaseThreadUndoShortcut ( ) ;
308+
309+ window . addEventListener ( "keydown" , onKeyDown , true ) ;
310+ window . addEventListener ( "input" , onInput , true ) ;
311+ return ( ) => {
312+ window . removeEventListener ( "keydown" , onKeyDown , true ) ;
313+ window . removeEventListener ( "input" , onInput , true ) ;
314+ } ;
315+ } , [ keybindings , noticeVisible , routeThreadRef ] ) ;
316+
317+ return null ;
318+ }
319+
248320// Settings swaps the thread sidebar out of the tree. Keep the lightweight
249321// project projection subscribed so returning to a draft never renders the
250322// zero-project state while the environment snapshot reconnects.
@@ -357,6 +429,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
357429 { children }
358430 < SidebarControl />
359431 < NavigationHistoryShortcuts />
432+ { isOnSettings ? null : < ThreadUndoShortcut /> }
360433 < MainAppLocationTracker />
361434 </ SidebarProvider >
362435 </ PanelAnimationSuppressionProvider >
0 commit comments