diff --git a/apps/web/src/components/AppSidebarLayout.browser.test.tsx b/apps/web/src/components/AppSidebarLayout.browser.test.tsx index c97ea5e6412aa..046a636a0d245 100644 --- a/apps/web/src/components/AppSidebarLayout.browser.test.tsx +++ b/apps/web/src/components/AppSidebarLayout.browser.test.tsx @@ -1,7 +1,14 @@ import { page, userEvent } from "vite-plus/test/browser"; +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; import { describe, expect, it, vi } from "vite-plus/test"; -import { render } from "../../test/render"; +import { act, render } from "../../test/render"; +import { + __resetClientSettingsPersistenceForTests, + __setClientSettingsForTests, + updateClientSettings, +} from "../hooks/useSettings"; +import { AppSidebarLayout } from "./AppSidebarLayout"; vi.mock(import("../state/server"), async (original) => { const actual = await original(); @@ -12,10 +19,93 @@ vi.mock(import("../state/server"), async (original) => { ]); return { ...actual, primaryServerKeybindingsAtom: Atom.make(keybindings) }; }); -// The thread list needs live environments; the shortcut lives in the shell around it. -vi.mock(import("./Sidebar"), () => ({ default: (() => null) as never })); +vi.mock("../env", () => ({ isElectron: true })); +vi.mock("./Sidebar", () => ({ default: () => null })); +vi.mock("./BackgroundThreadAnnouncer", () => ({ BackgroundThreadAnnouncer: () => null })); +vi.mock("./sidebar/SidebarChrome", () => ({ SidebarBrandWidthProbe: () => null })); +const storedSettings = vi.hoisted(() => ({ current: Promise.resolve(null) })); -import { AppSidebarLayout } from "./AppSidebarLayout"; +vi.mock("../localApi", () => ({ + ensureLocalApi: () => ({ + persistence: { + getClientSettings: () => storedSettings.current, + setClientSettings: async () => undefined, + }, + }), +})); + +function setup(historySwipeNavigation: boolean) { + storedSettings.current = Promise.resolve(null); + __resetClientSettingsPersistenceForTests(); + __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, historySwipeNavigation }); +} + +function wheel(deltaX: number) { + window.dispatchEvent(new WheelEvent("wheel", { deltaX, clientY: 200 })); +} + +describe("desktop history swipe navigation", () => { + it.each(["back", "forward"] as const)( + "disables %s swipes and can re-enable them without reloading", + async (direction) => { + setup(false); + using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); + using _available = vi + .spyOn(window.navigation, direction === "back" ? "canGoBack" : "canGoForward", "get") + .mockReturnValue(true); + await using screen = await render(Thread); + using navigate = vi.spyOn(window.history, direction).mockImplementation(() => undefined); + const deltaX = direction === "back" ? -140 : 140; + + await act(() => { + wheel(deltaX); + wheel(0); + }); + expect(navigate).not.toHaveBeenCalled(); + await expect.element(screen.getByTestId("history-swipe-indicator")).not.toBeInTheDocument(); + + await act(() => updateClientSettings({ historySwipeNavigation: true })); + await act(() => wheel(deltaX)); + await expect.element(screen.getByTestId("history-swipe-indicator")).toBeInTheDocument(); + + await act(() => updateClientSettings({ historySwipeNavigation: false })); + await expect.element(screen.getByTestId("history-swipe-indicator")).not.toBeInTheDocument(); + await act(() => wheel(0)); + expect(navigate).not.toHaveBeenCalled(); + + await act(() => updateClientSettings({ historySwipeNavigation: true })); + await act(() => { + wheel(deltaX); + wheel(0); + }); + expect(navigate).toHaveBeenCalledOnce(); + }, + ); + + it("keeps swipes off until the saved settings load", async () => { + const saved = Promise.withResolvers(); + storedSettings.current = saved.promise; + __resetClientSettingsPersistenceForTests(); + using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); + using _available = vi.spyOn(window.navigation, "canGoBack", "get").mockReturnValue(true); + await using _screen = await render(Thread); + using navigate = vi.spyOn(window.history, "back").mockImplementation(() => undefined); + + await act(() => { + wheel(-140); + wheel(0); + }); + expect(navigate).not.toHaveBeenCalled(); + + saved.resolve({ historySwipeNavigation: false }); + await act(() => saved.promise); + await act(() => { + wheel(-140); + wheel(0); + }); + expect(navigate).not.toHaveBeenCalled(); + }); +}); describe("sidebar toggle shortcut", () => { it("leaves the key to a focused terminal when its rule excludes terminalFocus", async () => { diff --git a/apps/web/src/components/AppSidebarLayout.tsx b/apps/web/src/components/AppSidebarLayout.tsx index 4b7a6d36a98cd..e9e0427899fa6 100644 --- a/apps/web/src/components/AppSidebarLayout.tsx +++ b/apps/web/src/components/AppSidebarLayout.tsx @@ -32,7 +32,11 @@ import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore"; import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; import { cn, isMacPlatform } from "../lib/utils"; import { primaryServerKeybindingsAtom } from "../state/server"; -import { useEnvironmentIdentificationMode } from "../hooks/useSettings"; +import { + useClientSettings, + useClientSettingsHydrated, + useEnvironmentIdentificationMode, +} from "../hooks/useSettings"; import { PanelAnimationSuppressionProvider, usePanelAnimationSettings, @@ -422,6 +426,8 @@ export function AppSidebarLayout({ showSidebar?: boolean; }) { const gt = useGT(); + const historySwipeNavigation = useClientSettings((settings) => settings.historySwipeNavigation); + const settingsHydrated = useClientSettingsHydrated(); const sidebarArea = workspaceAreaProps("sidebar", 0, gt("Threads")); const [sidebarRouteReveal] = useState(() => ({ observedKey: null, @@ -499,7 +505,9 @@ export function AppSidebarLayout({ {children} - {isElectron ? : null} + {isElectron && settingsHydrated && historySwipeNavigation ? ( + + ) : null} ); diff --git a/apps/web/src/components/HistorySwipeNavigation.tsx b/apps/web/src/components/HistorySwipeNavigation.tsx index 2924f82cd3136..d38d0a0bf211e 100644 --- a/apps/web/src/components/HistorySwipeNavigation.tsx +++ b/apps/web/src/components/HistorySwipeNavigation.tsx @@ -187,6 +187,7 @@ function SwipeIndicator({ direction, progress, y, bounds, tracking }: SwipeIndic return (