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
37 changes: 17 additions & 20 deletions src/bundled/channels/ChannelSidebarItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type { RelaySession } from "../../features/relay/session";
import type { UnreadSnapshot } from "../../features/relay/unread";
import type { PresenceStatus } from "../../features/presence/presence";
import { ChannelSidebarItem } from "./ChannelSidebarItem";
import { ContextMenuRoot } from "../../shared/design-system/ui/Menu";

afterEach(cleanup);

Expand Down Expand Up @@ -245,25 +244,23 @@ it.each(["ContextMenu", "F10"])(
channelType: "stream" as const,
};
render(
<ContextMenuRoot>
<ChannelSidebarItem
channel={channel}
session={owner().session}
working={false}
selected={undefined}
collapsed={false}
onToggle={onToggle}
draft={false}
draftSelected={false}
sessions={[{ id: "child", name: "Plan", channelType: "session" }]}
onSelect={onSelect}
onNewSession={vi.fn()}
onOpenThread={vi.fn()}
menuEnabled
sectionKey="group:work"
onOpenMenu={onOpenMenu}
/>
</ContextMenuRoot>,
<ChannelSidebarItem
channel={channel}
session={owner().session}
working={false}
selected={undefined}
collapsed={false}
onToggle={onToggle}
draft={false}
draftSelected={false}
sessions={[{ id: "child", name: "Plan", channelType: "session" }]}
onSelect={onSelect}
onNewSession={vi.fn()}
onOpenThread={vi.fn()}
menuEnabled
sectionKey="group:work"
onOpenMenu={onOpenMenu}
/>,
);
const parent = screen.getByRole("button", { name: "Alpha" });
fireEvent.keyDown(parent, { key, shiftKey: key === "F10" });
Expand Down
49 changes: 46 additions & 3 deletions src/bundled/channels/ChannelSidebarItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { UserStatusDisplay } from "../../features/user-status/StatusDisplay";
import { memo } from "react";
import { memo, useLayoutEffect, useRef, type ReactNode } from "react";
import { Avatar } from "../../shared/design-system/ui/Avatar";
import { ContextMenuTrigger } from "../../shared/design-system/ui/Menu";
import {
ContextMenuRoot,
ContextMenuTrigger,
MenuPopup,
} from "../../shared/design-system/ui/Menu";
import type { ChannelSummary, Profile } from "../../features/relay/contracts";
import type { RelaySession } from "../../features/relay/session";
import { ChatCircleIcon } from "../../shared/design-system/icons/index";
Expand Down Expand Up @@ -34,6 +38,11 @@ export const ChannelSidebarItem = memo(function ChannelSidebarItem({
menuEnabled,
sectionKey,
onOpenMenu,
menuOpen = false,
menuAnchor,
menuContent,
onCloseMenu,
menuFinalFocus,
}: {
channel: ChannelSummary;
profile?: Profile | undefined;
Expand All @@ -56,15 +65,26 @@ export const ChannelSidebarItem = memo(function ChannelSidebarItem({
sectionKey: string,
anchor?: HTMLElement,
) => void;
menuOpen?: boolean;
menuAnchor?: HTMLElement | undefined;
/** Only the open row receives content, so closed rows keep equal props. */
menuContent?: ReactNode;
onCloseMenu?: () => void;
menuFinalFocus?: (channelId: string) => HTMLElement | false;
}) {
const peer =
channel.channelType === "dm" && channel.participants?.length === 1
? channel.participants[0]
: undefined;
const presence = usePresenceStatus(peer ? session.presence : undefined, peer);
// Keep the closing row's items through the popup's exit transition.
const lastMenuContent = useRef<ReactNode>(undefined);
useLayoutEffect(() => {
if (menuContent !== undefined) lastMenuContent.current = menuContent;
}, [menuContent]);
const Icon =
channel.channelType === "dm" ? ChatCircleIcon : channelIcon(channel);
return (
const row = (
<ChannelSidebarRow
channel={channel}
icon={
Expand Down Expand Up @@ -176,4 +196,27 @@ export const ChannelSidebarItem = memo(function ChannelSidebarItem({
{...(onHideDm ? { onHideDm } : {})}
/>
);
if (!menuEnabled) return row;
return (
<ContextMenuRoot
open={menuOpen}
onOpenChange={(open) => {
if (open) {
if (sectionKey) onOpenMenu?.(channel, sectionKey);
} else if (menuOpen) onCloseMenu?.();
}}
onOpenChangeComplete={(open) => {
if (!open) lastMenuContent.current = undefined;
}}
>
{row}
<MenuPopup
aria-label={`Actions for ${channel.name}`}
anchor={menuOpen ? menuAnchor : undefined}
finalFocus={() => menuFinalFocus?.(channel.id) ?? false}
>
{menuContent ?? lastMenuContent.current}
</MenuPopup>
</ContextMenuRoot>
);
});
31 changes: 30 additions & 1 deletion src/features/channel-navigation/ChannelSidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import type { Navigation } from "../navigation/controller";
import { ChannelSidebar } from "./ChannelSidebar";
import { ChannelNavigationProvider } from "./ChannelNavigationState";

const { rowRender } = vi.hoisted(() => ({ rowRender: vi.fn() }));
const { rowRender, menuRender } = vi.hoisted(() => ({
rowRender: vi.fn(),
menuRender: vi.fn(),
}));
// Observe the real row below ChannelSidebarItem's production memo boundary.
// React hooks, the parent, the item and the row implementation remain real.
vi.mock("../../bundled/channels/ChannelSidebarRow", async (original) => {
Expand All @@ -25,6 +28,22 @@ vi.mock("../../bundled/channels/ChannelSidebarRow", async (original) => {
},
};
});
// Observe the real row menu provider and popup the same way.
vi.mock("../../shared/design-system/ui/Menu", async (original) => {
const actual =
await original<typeof import("../../shared/design-system/ui/Menu")>();
return {
...actual,
ContextMenuRoot: (props: Parameters<typeof actual.ContextMenuRoot>[0]) => {
menuRender("root", props);
return <actual.ContextMenuRoot {...props} />;
},
MenuPopup: (props: Parameters<typeof actual.MenuPopup>[0]) => {
menuRender("popup", props);
return <actual.MenuPopup {...props} />;
},
};
});
beforeEach(() => {
// jsdom has no layout; browser journeys own geometry. Only supply the API.
vi.stubGlobal(
Expand Down Expand Up @@ -111,10 +130,20 @@ it("does not rebuild unchanged rows on channel switches and refreshes session ac
const mounted = render(h.view("alpha"));
await screen.findByRole("button", { name: "gamma" });
rowRender.mockClear();
menuRender.mockClear();
mounted.rerender(h.view("beta"));
expect(rowRender.mock.calls.map(([props]) => props.channel.id)).not.toContain(
"gamma",
);
// Unchanged rows keep their menu provider and popup behind the memo too.
expect(
menuRender.mock.calls.filter(([, props]) =>
String(props["aria-label"] ?? "").includes("gamma"),
),
).toEqual([]);
expect(
menuRender.mock.calls.filter(([kind]) => kind === "root"),
).toHaveLength(rowRender.mock.calls.length);
expect(screen.getByRole("button", { name: "beta" })).toHaveAttribute(
"aria-current",
"page",
Expand Down
65 changes: 29 additions & 36 deletions src/features/channel-navigation/ChannelSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import type { OpenTarget } from "../navigation/targets";
import { Panel } from "../../shared/design-system/ui/Panel";
import { Button } from "../../shared/design-system/ui/Button";
import {
ContextMenuRoot,
MenuItem,
MenuIcon,
MenuPopup,
Expand Down Expand Up @@ -525,6 +524,17 @@ function ReadySidebar({
setReadWrite(undefined);
closeMenu();
}, [closeMenu]);
const rowMenuFinalFocus = useCallback(
(channelId: string) =>
startingSession.current
? (document
.getElementById("new-session-prompt")
?.querySelector<HTMLElement>('[role="textbox"]') ?? false)
: (sidebar.list.current?.querySelector<HTMLButtonElement>(
`[data-channel-id="${CSS.escape(channelId)}"]`,
) ?? false),
[sidebar.list],
);
useLayoutEffect(() => {
if (!rowFocus) return;
sidebar.list.current
Expand Down Expand Up @@ -849,7 +859,7 @@ function ReadySidebar({
menuEnabled &&
rowMenu?.channelId === channel.id &&
rowMenu.sectionKey === section.key;
const channelItem = (
return (
<ChannelSidebarItem
profile={
channel.channelType === "dm" &&
Expand All @@ -876,42 +886,25 @@ function ReadySidebar({
menuEnabled={menuEnabled}
sectionKey={section.key}
onOpenMenu={openRowMenu}
menuOpen={menuOpen}
menuAnchor={menuOpen ? rowMenu.anchor : undefined}
menuContent={
menuOpen ? (
<>
{actions}
{readWrite?.pending && (
<p role="status">Saving…</p>
)}
{readWrite?.error && (
<p role="alert">{readWrite.error}</p>
)}
</>
) : undefined
Comment thread
loganj marked this conversation as resolved.
}
onCloseMenu={closeRowMenu}
menuFinalFocus={rowMenuFinalFocus}
/>
);
if (!menuEnabled) return channelItem;
return (
<ContextMenuRoot
key={channel.id}
open={menuOpen}
onOpenChange={(open) => {
if (open) openRowMenu(channel, section.key);
else if (menuOpen) closeRowMenu();
}}
>
{channelItem}
<MenuPopup
aria-label={`Actions for ${channel.name}`}
anchor={menuOpen ? rowMenu.anchor : undefined}
finalFocus={() =>
startingSession.current
? (document
.getElementById("new-session-prompt")
?.querySelector<HTMLElement>(
'[role="textbox"]',
) ?? false)
: (sidebar.list.current?.querySelector<HTMLButtonElement>(
`[data-channel-id="${CSS.escape(channel.id)}"]`,
) ?? false)
}
>
{actions}
{readWrite?.pending && <p role="status">Saving…</p>}
{readWrite?.error && (
<p role="alert">{readWrite.error}</p>
)}
</MenuPopup>
</ContextMenuRoot>
);
})}
</details>
);
Expand Down
Loading