Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/web/src/components/AppSidebarLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ function routePanelsOpen(routeThreadRef: ReturnType<typeof useThreadRouteRef>) {
};
}

function NavigationHistoryShortcuts() {
export function NavigationHistoryShortcuts() {
const usagePageOpen = useLocation({ select: (location) => location.pathname === "/usage" });
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
const routeThreadRef = useThreadRouteRef();

Expand All @@ -239,6 +240,7 @@ function NavigationHistoryShortcuts() {
...routePanelsOpen(routeThreadRef),
editableFocus: isEditableFocused(event.target),
modelPickerOpen: isModelPickerOpen(),
usagePageOpen,
},
});
if (command !== "navigation.back" && command !== "navigation.forward") return;
Expand All @@ -251,7 +253,7 @@ function NavigationHistoryShortcuts() {

window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [keybindings, routeThreadRef]);
}, [keybindings, routeThreadRef, usagePageOpen]);

return null;
}
Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/components/CommandPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ const EMPTY_BROWSE_ENTRIES: FilesystemBrowseResult["entries"] = [];

export function CommandPalette({ children }: { children: ReactNode }) {
const navigate = useNavigate();
const usagePageOpen = useLocation({ select: (location) => location.pathname === "/usage" });
const [state, dispatch] = useReducer(reduceCommandPaletteUiState, {
open: false,
mode: "command",
Expand Down Expand Up @@ -287,6 +288,7 @@ export function CommandPalette({ children }: { children: ReactNode }) {
previewFocus: isPreviewFocused(),
previewOpen,
modelPickerOpen: composerHandleRef.current?.isModelPickerOpen() ?? false,
usagePageOpen,
},
});
if (command === "appearance.cycle") {
Expand Down Expand Up @@ -352,6 +354,7 @@ export function CommandPalette({ children }: { children: ReactNode }) {
theme,
themeHalves,
toggleMode,
usagePageOpen,
]);

useEffect(
Expand Down
52 changes: 52 additions & 0 deletions apps/web/src/components/UsageToggleShortcut.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
import { describe, expect, it, vi } from "vite-plus/test";
import { userEvent } from "vite-plus/test/browser";

import { render } from "../../test/render";

const state = vi.hoisted(() => ({ navigate: vi.fn() }));

// One key toggles Usage: it opens Usage elsewhere and goes back from it.
const keybindings = compileResolvedKeybindingsConfig([
{ key: "mod+u", command: "navigation.back", when: "usagePageOpen" },
{ key: "mod+u", command: "usage.open", when: "!usagePageOpen" },
]);

vi.mock(import("@effect/atom-react"), async (original) => ({
...(await original()),
useAtomValue: (() => keybindings) as never,
}));
vi.mock(import("@tanstack/react-router"), async (original) => ({
...(await original()),
useNavigate: (() => state.navigate) as never,
}));

import { NavigationHistoryShortcuts } from "./AppSidebarLayout";
import { CommandPalette } from "./CommandPalette";

// regression: #21357
describe("usage toggle shortcut on Usage", () => {
it("goes back", async () => {
vi.clearAllMocks();
using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
await using _screen = await render(<NavigationHistoryShortcuts />, { router: "/usage" });

using back = vi.spyOn(window.history, "back").mockImplementation(() => undefined);
await userEvent.keyboard("{Control>}u{/Control}");

expect(back).toHaveBeenCalledOnce();
});

// Both listeners sit on window, so the palette must not claim the key whatever their order.
it("does not reopen Usage from the command palette", async () => {
vi.clearAllMocks();
using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
await using _screen = await render(<CommandPalette>{null}</CommandPalette>, {
router: "/usage",
});

await userEvent.keyboard("{Control>}u{/Control}");

expect(state.navigate).not.toHaveBeenCalled();
});
});
Loading