Skip to content
Closed
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
103 changes: 103 additions & 0 deletions apps/web/src/components/sidebar/SidebarListMarkers.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { DndContext, useDndContext, useDraggable } from "@dnd-kit/core";
import { useState } from "react";
import { expect, it, vi } from "vite-plus/test";
import { userEvent } from "vite-plus/test/browser";

import { act, render } from "../../../test/render";
import { sidebarMarkerId } from "../Sidebar.logic";
import { SidebarSectionHeader } from "./SidebarListMarkers";

vi.mock("./SidebarThreadRow", () => ({
SIDEBAR_SORTABLE_DRAG_DISABLED: { draggable: true },
}));

function Shelf({ count }: { count: number }) {
const { active } = useDndContext();
const { setNodeRef, attributes, listeners } = useDraggable({ id: "thread" });
return (
<>
<button ref={setNodeRef} {...attributes} {...listeners}>
Thread
</button>
<ul>
<SidebarSectionHeader
marker="settled-header"
label="Settled"
count={count}
dragging={active !== null}
toggle={{ expanded: false, onToggle: () => {} }}
/>
</ul>
</>
);
}

function Harness() {
const [count, setCount] = useState(0);
return (
<div className="w-64">
<DndContext
onDragEnd={({ over }) => {
if (over?.id === sidebarMarkerId("settled-header")) setCount(1);
}}
>
<Shelf count={count} />
</DndContext>
<button onClick={() => setCount(0)}>Unsettle</button>
</div>
);
}

it("reveals an empty Settled drop target only for a drag and keeps it after a drop", async () => {
await using screen = await render(<Harness />, {
router: false /* shelf drag/drop does not navigate */,
});
const settled = screen.getByTestId("sidebar-settled-shelf-toggle");
await expect.element(settled).not.toBeInTheDocument();

const startDrag = async () => {
const thread = screen.getByRole("button", { name: "Thread" }).element();
const { x, y, width, height } = thread.getBoundingClientRect();
await act(() => {
thread.dispatchEvent(
new PointerEvent("pointerdown", {
bubbles: true,
isPrimary: true,
pointerId: 1,
button: 0,
buttons: 1,
clientX: x + width / 2,
clientY: y + height / 2,
}),
);
});
await expect.element(settled).toBeVisible();
};

await startDrag();
await act(() =>
document.dispatchEvent(
new KeyboardEvent("keydown", { key: "Escape", code: "Escape", bubbles: true }),
),
);
await expect.element(settled).not.toBeInTheDocument();

await startDrag();
const target = settled.element().getBoundingClientRect();
await act(() => {
document.dispatchEvent(
new PointerEvent("pointermove", {
bubbles: true,
pointerId: 1,
buttons: 1,
clientX: target.x + target.width / 2,
clientY: target.y + target.height / 2,
}),
);
});
await act(() => document.dispatchEvent(new PointerEvent("pointerup", { pointerId: 1 })));
await expect.element(screen.getByRole("button", { name: /^Settled\s*1$/ })).toBeVisible();

await userEvent.click(screen.getByRole("button", { name: "Unsettle" }));
await expect.element(settled).not.toBeInTheDocument();
});
31 changes: 20 additions & 11 deletions apps/web/src/components/sidebar/SidebarListMarkers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,31 @@ export function SidebarSectionHeader(props: {
: props.marker === "snoozed-header"
? "snoozed"
: "settled";
// Keep the zero-height marker mounted so an empty shelf remains a valid drop target.
const visible = props.count > 0 || props.dragging === true;
return (
<SortableSidebarMarker
marker={props.marker}
data-testid={`sidebar-${props.marker}`}
className={cn("relative mx-0.5 h-8", props.className)}
className={cn("relative mx-0.5", visible ? "h-8" : "-mb-px h-0", props.className)}
>
<CollapsibleSectionHeader
data-sidebar-row={`shelf:${shelf}`}
onClick={props.toggle.onToggle}
expanded={props.toggle.expanded}
tone={props.isDropTarget ? "accent" : props.dragging ? "emphasized" : "muted"}
data-testid={`sidebar-${shelf}-shelf-toggle`}
accessory={<span className="font-normal tabular-nums">{props.count}</span>}
>
{props.label}
</CollapsibleSectionHeader>
{visible ? (
<CollapsibleSectionHeader
data-sidebar-row={`shelf:${shelf}`}
onClick={props.toggle.onToggle}
expanded={props.toggle.expanded}
tone={props.isDropTarget ? "accent" : props.dragging ? "emphasized" : "muted"}
data-testid={`sidebar-${shelf}-shelf-toggle`}
accessory={
<span className={cn("font-normal tabular-nums", props.dragging && "invisible")}>
{props.count}
</span>
}
showChevron={!props.dragging}
>
{props.label}
</CollapsibleSectionHeader>
) : null}
</SortableSidebarMarker>
);
}
8 changes: 7 additions & 1 deletion apps/web/src/components/ui/collapsible-section-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export function CollapsibleSectionHeader({
tone = "muted",
variant = "default",
accessory,
showChevron = true,
...buttonProps
}: Omit<ComponentProps<"button">, "className" | "style" | "aria-expanded"> & {
expanded: boolean;
tone?: keyof typeof tones;
variant?: "default" | "panel";
accessory?: ReactNode;
showChevron?: boolean;
}) {
return (
<button
Expand All @@ -41,7 +43,11 @@ export function CollapsibleSectionHeader({
{accessory}
<ChevronDownIcon
aria-hidden
className={cn("size-3 shrink-0 transition-transform", !expanded && "-rotate-90")}
className={cn(
"size-3 shrink-0 transition-transform",
!expanded && "-rotate-90",
!showChevron && "invisible",
)}
/>
</button>
);
Expand Down
Loading