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
49 changes: 49 additions & 0 deletions apps/web/src/components/ChatView.shell.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions apps/web/src/components/sidebar/ChromeIconButton.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -15,6 +15,7 @@ export function ChromeIconButton({
tooltip,
size,
selected = false,
expanded,
glide = false,
disabled = false,
side,
Expand All @@ -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.
Expand All @@ -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<HTMLButtonElement>) => void;
readonly children: ReactNode;
readonly testId?: string;
readonly className?: string;
Expand All @@ -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}
Expand All @@ -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);
}}
/>
}
Expand Down
47 changes: 35 additions & 12 deletions apps/web/src/components/sidebar/NavigationRail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
/**
Expand Down Expand Up @@ -294,10 +300,14 @@ function joinDetails(...parts: Array<string | null>): 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();
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -375,6 +388,7 @@ export function useNavigationDestinations(options: { readonly listOpen: boolean
attention,
hasPullRequests,
hasScheduledTasks,
isMobile,
keybindings,
options.listOpen,
pullRequestsNeedingYou,
Expand All @@ -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;
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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;
Expand All @@ -521,10 +543,11 @@ function DestinationButton({
label={label}
tooltip={<DestinationTooltip destination={destination} />}
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)}
>
<Icon aria-hidden="true" />
<CornerMarks needsYou={destination.needsYou} unread={destination.unread} />
Expand Down
Loading