From f070e86746881f14453587e31e56d90312b578b8 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 18 Feb 2026 19:55:00 +0700 Subject: [PATCH] refactor: extract keyboard shortcut routing logic to separate module --- .../app/src/hooks/use-keyboard-shortcuts.ts | 41 ++++++++--------- .../keyboard-shortcut-routing.test.ts | 45 +++++++++++++++++++ .../src/keyboard/keyboard-shortcut-routing.ts | 40 +++++++++++++++++ 3 files changed, 104 insertions(+), 22 deletions(-) create mode 100644 packages/app/src/keyboard/keyboard-shortcut-routing.test.ts create mode 100644 packages/app/src/keyboard/keyboard-shortcut-routing.ts diff --git a/packages/app/src/hooks/use-keyboard-shortcuts.ts b/packages/app/src/hooks/use-keyboard-shortcuts.ts index f163b8e1f..a45ce87e1 100644 --- a/packages/app/src/hooks/use-keyboard-shortcuts.ts +++ b/packages/app/src/hooks/use-keyboard-shortcuts.ts @@ -18,7 +18,6 @@ import { } from "@/utils/new-agent-routing"; import { buildHostAgentDetailRoute, - parseHostAgentDraftRouteFromPathname, parseHostAgentRouteFromPathname, parseServerIdFromPathname, } from "@/utils/host-routes"; @@ -26,29 +25,14 @@ import { type MessageInputKeyboardActionKind, type KeyboardShortcutPayload, } from "@/keyboard/actions"; +import { + canToggleFileExplorerShortcut, + resolveSelectedOrRouteAgentKey, +} from "@/keyboard/keyboard-shortcut-routing"; import { resolveKeyboardShortcut } from "@/keyboard/keyboard-shortcuts"; import { resolveKeyboardFocusScope } from "@/keyboard/focus-scope"; import { getShortcutOs } from "@/utils/shortcut-platform"; -function resolveSelectedOrRouteAgentKey(input: { - selectedAgentId?: string; - pathname: string; -}): string | null { - const DRAFT_AGENT_ID = "__new_agent__"; - if (input.selectedAgentId) { - return input.selectedAgentId; - } - const route = parseHostAgentRouteFromPathname(input.pathname); - if (!route) { - const draftRoute = parseHostAgentDraftRouteFromPathname(input.pathname); - if (!draftRoute) { - return null; - } - return `${draftRoute.serverId}:${DRAFT_AGENT_ID}`; - } - return `${route.serverId}:${route.agentId}`; -} - export function useKeyboardShortcuts({ enabled, isMobile, @@ -161,7 +145,16 @@ export function useKeyboardShortcuts({ toggleAgentList(); return true; case "sidebar.toggle.right": - if (!selectedAgentId || !toggleFileExplorer) { + if (!toggleFileExplorer) { + return false; + } + if ( + !canToggleFileExplorerShortcut({ + selectedAgentId, + pathname, + toggleFileExplorer, + }) + ) { return false; } toggleFileExplorer(); @@ -234,7 +227,11 @@ export function useKeyboardShortcuts({ isTauri, focusScope, commandCenterOpen: store.commandCenterOpen, - hasSelectedAgent: Boolean(selectedAgentId && toggleFileExplorer), + hasSelectedAgent: canToggleFileExplorerShortcut({ + selectedAgentId, + pathname, + toggleFileExplorer, + }), }, }); if (!match) { diff --git a/packages/app/src/keyboard/keyboard-shortcut-routing.test.ts b/packages/app/src/keyboard/keyboard-shortcut-routing.test.ts new file mode 100644 index 000000000..a73ad829b --- /dev/null +++ b/packages/app/src/keyboard/keyboard-shortcut-routing.test.ts @@ -0,0 +1,45 @@ +import { describe, expect, it } from "vitest"; + +import { canToggleFileExplorerShortcut } from "./keyboard-shortcut-routing"; + +describe("keyboard-shortcut-routing", () => { + describe("canToggleFileExplorerShortcut", () => { + const toggleFileExplorer = () => undefined; + + it("allows the shortcut on selected-agent routes", () => { + const canToggle = canToggleFileExplorerShortcut({ + selectedAgentId: "server-1:agent-1", + pathname: "/h/server-1/agent/agent-1", + toggleFileExplorer, + }); + + expect(canToggle).toBe(true); + }); + + it("allows the shortcut on draft routes", () => { + const canToggle = canToggleFileExplorerShortcut({ + pathname: "/h/server-1/agent", + toggleFileExplorer, + }); + + expect(canToggle).toBe(true); + }); + + it("blocks the shortcut when no toggle handler exists", () => { + const canToggle = canToggleFileExplorerShortcut({ + pathname: "/h/server-1/agent", + }); + + expect(canToggle).toBe(false); + }); + + it("blocks the shortcut outside agent routes", () => { + const canToggle = canToggleFileExplorerShortcut({ + pathname: "/h/server-1/settings", + toggleFileExplorer, + }); + + expect(canToggle).toBe(false); + }); + }); +}); diff --git a/packages/app/src/keyboard/keyboard-shortcut-routing.ts b/packages/app/src/keyboard/keyboard-shortcut-routing.ts new file mode 100644 index 000000000..4bda73560 --- /dev/null +++ b/packages/app/src/keyboard/keyboard-shortcut-routing.ts @@ -0,0 +1,40 @@ +import { + parseHostAgentDraftRouteFromPathname, + parseHostAgentRouteFromPathname, +} from "@/utils/host-routes"; + +const DRAFT_AGENT_ID = "__new_agent__"; + +export function resolveSelectedOrRouteAgentKey(input: { + selectedAgentId?: string; + pathname: string; +}): string | null { + if (input.selectedAgentId) { + return input.selectedAgentId; + } + const route = parseHostAgentRouteFromPathname(input.pathname); + if (!route) { + const draftRoute = parseHostAgentDraftRouteFromPathname(input.pathname); + if (!draftRoute) { + return null; + } + return `${draftRoute.serverId}:${DRAFT_AGENT_ID}`; + } + return `${route.serverId}:${route.agentId}`; +} + +export function canToggleFileExplorerShortcut(input: { + selectedAgentId?: string; + pathname: string; + toggleFileExplorer?: () => void; +}): boolean { + if (!input.toggleFileExplorer) { + return false; + } + return ( + resolveSelectedOrRouteAgentKey({ + selectedAgentId: input.selectedAgentId, + pathname: input.pathname, + }) !== null + ); +}