diff --git a/apps/web/src/components/ChatView.shell.browser.tsx b/apps/web/src/components/ChatView.shell.browser.tsx index 460cba256..b9197fcf5 100644 --- a/apps/web/src/components/ChatView.shell.browser.tsx +++ b/apps/web/src/components/ChatView.shell.browser.tsx @@ -493,6 +493,55 @@ describe("ChatView (full app): app shell", () => { } }); + it("hides and shows the list from the rail button of the page on screen, and a double-click that opens a page leaves it showing", async () => { + const mounted = await mountChatView({ + viewport: DEFAULT_VIEWPORT, + snapshot: createSnapshotForTargetUser({ + targetMessageId: "msg-user-rail-reclick" as MessageId, + targetText: "rail reclick", + }), + }); + const listOpen = (open: boolean) => + vi.waitFor(() => { + expect(useUiStateStore.getState().sidebarListOpen).toBe(open); + expect( + document + .querySelector('[data-slot="sidebar-wrapper"]') + ?.getAttribute("data-sidebar-state"), + ).toBe(open ? "expanded" : "collapsed"); + }); + // A click as the n-th of a run of quick clicks, the way a double-click lands. + const clickRail = (testId: string, detail: number) => + document + .querySelector(`[data-testid="${testId}"]`)! + .dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail })); + + try { + await expect + .element(page.getByTestId("rail-threads")) + .toHaveAttribute("aria-expanded", "true"); + await page.getByTestId("rail-threads").click(); + await listOpen(false); + await expect + .element(page.getByTestId("rail-threads")) + .toHaveAttribute("aria-expanded", "false"); + await page.getByTestId("rail-threads").click(); + await listOpen(true); + + clickRail("rail-chats", 1); + await waitForURL(mounted.router, (pathname) => pathname === "/chats", "Chats should open."); + await expect.element(page.getByTestId("rail-chats")).toHaveAttribute("aria-current", "page"); + clickRail("rail-chats", 2); + expect(useUiStateStore.getState().sidebarListOpen).toBe(true); + + // A click of its own, on the page now open, hides the list. + clickRail("rail-chats", 1); + await listOpen(false); + } finally { + await mounted.cleanup(); + } + }); + it("counts the pull requests waiting on the user on the rail", async () => { const mounted = await mountChatView({ viewport: DEFAULT_VIEWPORT, diff --git a/apps/web/src/components/sidebar/ChromeIconButton.tsx b/apps/web/src/components/sidebar/ChromeIconButton.tsx index 7e95a6a1c..50add1245 100644 --- a/apps/web/src/components/sidebar/ChromeIconButton.tsx +++ b/apps/web/src/components/sidebar/ChromeIconButton.tsx @@ -1,4 +1,4 @@ -import type { ReactNode } from "react"; +import type { MouseEvent, ReactNode } from "react"; import { cn } from "../../lib/utils"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; @@ -15,6 +15,7 @@ export function ChromeIconButton({ tooltip, size, selected = false, + expanded, glide = false, disabled = false, side, @@ -29,6 +30,8 @@ export function ChromeIconButton({ readonly tooltip: ReactNode; readonly size: "rail" | "titlebar"; readonly selected?: boolean; + /** For a button that also shows and hides something: whether it is showing. */ + readonly expanded?: boolean | undefined; /** * Selection is drawn by a highlight its container slides between buttons * (the rail), so a selected button paints no fill of its own. @@ -46,7 +49,7 @@ export function ChromeIconButton({ * words change in place. Others keep the size they opened at. */ readonly tooltipFollowsContent?: boolean; - readonly onClick: () => void; + readonly onClick: (event: MouseEvent) => void; readonly children: ReactNode; readonly testId?: string; readonly className?: string; @@ -64,6 +67,7 @@ export function ChromeIconButton({ type="button" aria-label={label} aria-current={selected ? "page" : undefined} + aria-expanded={expanded} aria-disabled={disabled ? true : undefined} data-testid={testId} data-selected={selected ? "" : undefined} @@ -78,8 +82,8 @@ export function ChromeIconButton({ : "text-muted-foreground hover:bg-sidebar-accent/60 hover:text-foreground active:bg-sidebar-accent/80", className, )} - onClick={() => { - if (!disabled) onClick(); + onClick={(event) => { + if (!disabled) onClick(event); }} /> } diff --git a/apps/web/src/components/sidebar/NavigationRail.tsx b/apps/web/src/components/sidebar/NavigationRail.tsx index a359c5665..858cf0aa8 100644 --- a/apps/web/src/components/sidebar/NavigationRail.tsx +++ b/apps/web/src/components/sidebar/NavigationRail.tsx @@ -19,7 +19,7 @@ import { SquarePenIcon, WaypointsIcon, } from "lucide-react"; -import { useCallback, useEffect, useLayoutEffect, useMemo, useState } from "react"; +import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { useShallow } from "zustand/react/shallow"; import { useHandleNewThread } from "../../hooks/useHandleNewThread"; @@ -58,6 +58,7 @@ import { type ThreadAttentionCounts, } from "../Sidebar.logic"; import { ChromeIconButton, ChromeTooltip } from "./ChromeIconButton"; +import { destinationHasList } from "./destinationLists"; import { usePendingLinkRequests } from "./linkedComputers"; import { useListNewThreadStore } from "./listNewThreadStore"; import { RailUpdateButton } from "./RailUpdateButton"; @@ -96,6 +97,11 @@ export interface NavigationDestination { readonly shortLabel: string; readonly icon: LucideIcon; readonly selected: boolean; + /** + * The selected destination's list beside the rail, which its button shows + * and hides: whether it is showing. Undefined for the others, and on a phone. + */ + readonly listOpen: boolean | undefined; /** Threads waiting on the user: an amber count on the icon. */ readonly needsYou: number; /** @@ -294,10 +300,14 @@ function joinDetails(...parts: Array): string | null { * does. `listOpen` is whether the thread list beside the rail is showing: the * Threads icon carries the working count only while the list is hidden, since * the list says it otherwise. + * + * `select` takes the click's place in a run of quick clicks (its `detail`: 1 + * for a single click, 2 for a double-click's second, 0 from a key). Choosing + * the destination already on screen shows or hides its list beside the rail. */ export function useNavigationDestinations(options: { readonly listOpen: boolean }): { readonly destinations: readonly NavigationDestination[]; - readonly select: (id: NavigationDestinationId) => void; + readonly select: (id: NavigationDestinationId, clickCount?: number) => void; } { const navigate = useNavigate(); const router = useRouter(); @@ -320,6 +330,7 @@ export function useNavigationDestinations(options: { readonly listOpen: boolean describeActiveScheduledTasks(state.byEnvironment), ); const shortcutContext = useRouteShortcutContext(); + const { isMobile, toggleSidebar } = useSidebar(); const destinations = useMemo(() => { const platform = typeof navigator === "undefined" ? "" : navigator.platform; @@ -339,6 +350,8 @@ export function useNavigationDestinations(options: { readonly listOpen: boolean shortLabel: NAVIGATION_DESTINATION_SHORT_LABELS[id], icon: DESTINATION_ICONS[id], selected: selected === id, + listOpen: + selected === id && !isMobile && destinationHasList(id) ? options.listOpen : undefined, needsYou: id === "pull-requests" ? pullRequestsNeedingYou : (counts?.needsYou ?? 0), unread: id === "chats" && attention.chatsUnread, working: counts?.working ?? 0, @@ -375,6 +388,7 @@ export function useNavigationDestinations(options: { readonly listOpen: boolean attention, hasPullRequests, hasScheduledTasks, + isMobile, keybindings, options.listOpen, pullRequestsNeedingYou, @@ -384,16 +398,23 @@ export function useNavigationDestinations(options: { readonly listOpen: boolean usageTokens, ]); - const { open, setOpen, isMobile } = useSidebar(); + // Whether a click in the current run of quick clicks went somewhere. + const runNavigatedRef = useRef(false); const select = useCallback( - (id: NavigationDestinationId) => { + (id: NavigationDestinationId, clickCount = 1) => { + if (clickCount <= 1) runNavigatedRef.current = false; + if (id === selected && !isMobile && destinationHasList(id)) { + // Already there: the button shows or hides the list, as the title + // row's toggle does. A double-click that came here is one choice, so + // its second click leaves the list as the first left it. + if (!runNavigatedRef.current) toggleSidebar(); + return; + } + // On a phone the threads are the sheet itself, which choosing closed. + if (id === "threads" && selected === "threads") return; + runNavigatedRef.current = true; switch (id) { case "threads": { - if (selected === "threads") { - // Already there: the button's job is to show the list. - if (!isMobile && !open) setOpen(true); - return; - } if (lastThreadsViewHref !== null) { void router.navigate({ href: lastThreadsViewHref }); return; @@ -421,7 +442,7 @@ export function useNavigationDestinations(options: { readonly listOpen: boolean return; } }, - [isMobile, navigate, open, router, selected, setOpen], + [isMobile, navigate, router, selected, toggleSidebar], ); return { destinations, select }; @@ -502,7 +523,8 @@ function DestinationButton({ readonly glide?: boolean; /** On a phone: its short name under the icon, since a finger can't hover for the tooltip. */ readonly named?: boolean; - readonly onSelect: (id: NavigationDestinationId) => void; + /** With the click's place in a run of quick clicks, as `select` takes it. */ + readonly onSelect: (id: NavigationDestinationId, clickCount: number) => void; }) { const Icon = destination.icon; const working = destination.showWorking ? destination.working : 0; @@ -521,10 +543,11 @@ function DestinationButton({ label={label} tooltip={} selected={destination.selected} + expanded={destination.listOpen} glide={glide} side={side} testId={`rail-${destination.id}`} - onClick={() => onSelect(destination.id)} + onClick={(event) => onSelect(destination.id, event.detail)} >