refactor: extract keyboard shortcut routing logic to separate module
This commit is contained in:
@@ -18,7 +18,6 @@ import {
|
|||||||
} from "@/utils/new-agent-routing";
|
} from "@/utils/new-agent-routing";
|
||||||
import {
|
import {
|
||||||
buildHostAgentDetailRoute,
|
buildHostAgentDetailRoute,
|
||||||
parseHostAgentDraftRouteFromPathname,
|
|
||||||
parseHostAgentRouteFromPathname,
|
parseHostAgentRouteFromPathname,
|
||||||
parseServerIdFromPathname,
|
parseServerIdFromPathname,
|
||||||
} from "@/utils/host-routes";
|
} from "@/utils/host-routes";
|
||||||
@@ -26,29 +25,14 @@ import {
|
|||||||
type MessageInputKeyboardActionKind,
|
type MessageInputKeyboardActionKind,
|
||||||
type KeyboardShortcutPayload,
|
type KeyboardShortcutPayload,
|
||||||
} from "@/keyboard/actions";
|
} from "@/keyboard/actions";
|
||||||
|
import {
|
||||||
|
canToggleFileExplorerShortcut,
|
||||||
|
resolveSelectedOrRouteAgentKey,
|
||||||
|
} from "@/keyboard/keyboard-shortcut-routing";
|
||||||
import { resolveKeyboardShortcut } from "@/keyboard/keyboard-shortcuts";
|
import { resolveKeyboardShortcut } from "@/keyboard/keyboard-shortcuts";
|
||||||
import { resolveKeyboardFocusScope } from "@/keyboard/focus-scope";
|
import { resolveKeyboardFocusScope } from "@/keyboard/focus-scope";
|
||||||
import { getShortcutOs } from "@/utils/shortcut-platform";
|
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({
|
export function useKeyboardShortcuts({
|
||||||
enabled,
|
enabled,
|
||||||
isMobile,
|
isMobile,
|
||||||
@@ -161,7 +145,16 @@ export function useKeyboardShortcuts({
|
|||||||
toggleAgentList();
|
toggleAgentList();
|
||||||
return true;
|
return true;
|
||||||
case "sidebar.toggle.right":
|
case "sidebar.toggle.right":
|
||||||
if (!selectedAgentId || !toggleFileExplorer) {
|
if (!toggleFileExplorer) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
!canToggleFileExplorerShortcut({
|
||||||
|
selectedAgentId,
|
||||||
|
pathname,
|
||||||
|
toggleFileExplorer,
|
||||||
|
})
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
toggleFileExplorer();
|
toggleFileExplorer();
|
||||||
@@ -234,7 +227,11 @@ export function useKeyboardShortcuts({
|
|||||||
isTauri,
|
isTauri,
|
||||||
focusScope,
|
focusScope,
|
||||||
commandCenterOpen: store.commandCenterOpen,
|
commandCenterOpen: store.commandCenterOpen,
|
||||||
hasSelectedAgent: Boolean(selectedAgentId && toggleFileExplorer),
|
hasSelectedAgent: canToggleFileExplorerShortcut({
|
||||||
|
selectedAgentId,
|
||||||
|
pathname,
|
||||||
|
toggleFileExplorer,
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!match) {
|
if (!match) {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user