Skip to content

Commit 1826fb5

Browse files
argofowlmaria-rcks
andauthored
feat(web): sweep sidebar buttons to settle, un-settle, and wake threads (#14768)
Co-authored-by: maria-rcks <[email protected]>
1 parent 0c81120 commit 1826fb5

6 files changed

Lines changed: 225 additions & 28 deletions

File tree

‎apps/web/src/components/Sidebar.logic.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
reduceSidebarProjectScopeMenuState,
3131
resolveAdjacentThreadId,
3232
resolveProjectStatusIndicator,
33+
resolveSidebarSweepKeys,
3334
resolveSidebarStageBadgeLabel,
3435
resolveSidebarThreadSection,
3536
resolveSidebarRowAccessibility,
@@ -2014,6 +2015,22 @@ describe("sortPinnedThreadsForSidebar", () => {
20142015
});
20152016
});
20162017

2018+
describe("resolveSidebarSweepKeys", () => {
2019+
const ordered = ["a", "b", "c", "d", "blocked"];
2020+
const canSettle = (key: string) => key !== "blocked";
2021+
2022+
it("covers every row between the pressed row and the pointer, in either direction", () => {
2023+
expect(resolveSidebarSweepKeys(ordered, "b", "b", canSettle)).toEqual(["b"]);
2024+
expect(resolveSidebarSweepKeys(ordered, "b", "d", canSettle)).toEqual(["b", "c", "d"]);
2025+
expect(resolveSidebarSweepKeys(ordered, "d", "a", canSettle)).toEqual(["a", "b", "c", "d"]);
2026+
});
2027+
2028+
it("leaves out rows that cannot settle and rows that left the list", () => {
2029+
expect(resolveSidebarSweepKeys(ordered, "c", "blocked", canSettle)).toEqual(["c", "d"]);
2030+
expect(resolveSidebarSweepKeys(ordered, "gone", "a", canSettle)).toEqual([]);
2031+
});
2032+
});
2033+
20172034
describe("navigation after parking a thread", () => {
20182035
it.each([
20192036
["settle", "settled", null, "thread", true],

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,34 @@ export function resolveSidebarDropVerb(
274274
return "wake";
275275
}
276276

277+
/** Eligible rows between the pressed action and the pointer, in sidebar order. */
278+
export function resolveSidebarSweepKeys(
279+
orderedKeys: readonly string[],
280+
originKey: string,
281+
targetKey: string,
282+
canApply: (key: string) => boolean,
283+
): string[] {
284+
const origin = orderedKeys.indexOf(originKey);
285+
const target = orderedKeys.indexOf(targetKey);
286+
if (origin === -1 || target === -1) return [];
287+
return orderedKeys.slice(Math.min(origin, target), Math.max(origin, target) + 1).filter(canApply);
288+
}
289+
290+
/** The thread row at a pointer height, clamped to the rows visible in the
291+
sidebar's scroll viewport. A gap between rows resolves to the row above
292+
it. Rows carry their key in data-thread-item, which departing motion
293+
clones drop. */
294+
export function sidebarThreadKeyAtY(list: HTMLElement, y: number): string | null {
295+
const viewport = list.closest('[data-slot="scroll-area-viewport"]')?.getBoundingClientRect();
296+
const visibleY = viewport ? Math.min(Math.max(y, viewport.top), viewport.bottom - 1) : y;
297+
let key: string | null = null;
298+
for (const row of list.querySelectorAll<HTMLElement>("li[data-thread-item]")) {
299+
if (key !== null && row.getBoundingClientRect().top > visibleY) break;
300+
key = row.dataset.threadItem ?? null;
301+
}
302+
return key;
303+
}
304+
277305
export function planSidebarThreadDrop(input: {
278306
readonly activeKey: string;
279307
readonly activeSection: SidebarSection;

‎apps/web/src/components/Sidebar.pointer.test.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ describe("sidebar pointer lifecycle", () => {
8080
expect(drag.onFinish).toHaveBeenCalledOnce();
8181
});
8282

83+
it("moves to the pointer on the move that starts the drag", () => {
84+
const drag = gesture();
85+
document.dispatchEvent(pointer("pointermove", { clientY: 90 }));
86+
expect(drag.onStart).toHaveBeenCalledExactlyOnceWith({ x: 10, y: 10 });
87+
expect(drag.onMove).toHaveBeenCalledExactlyOnceWith({ x: 10, y: 90 });
88+
});
89+
8390
const interruptions = {
8491
blur: () => window.dispatchEvent(new Event("blur")),
8592
hidden: () => {
@@ -122,10 +129,11 @@ describe("sidebar pointer lifecycle", () => {
122129
const drag = gesture({ onMove, onDrop });
123130
document.dispatchEvent(pointer("pointermove", { clientY: 20 }));
124131
document.dispatchEvent(pointer("pointermove", { clientX: 30, clientY: 30 }));
125-
expect(drag.onMove).toHaveBeenCalledExactlyOnceWith({ x: 30, y: 30 });
132+
expect(drag.onMove).toHaveBeenNthCalledWith(1, { x: 10, y: 20 });
133+
expect(drag.onMove).toHaveBeenNthCalledWith(2, { x: 30, y: 30 });
126134
document.dispatchEvent(pointer("pointermove", { clientX: 90, clientY: 30 }));
127135
// Outside the list the sort no longer sees the pointer.
128-
expect(drag.onMove).toHaveBeenCalledOnce();
136+
expect(drag.onMove).toHaveBeenCalledTimes(2);
129137
document.dispatchEvent(pointer("pointerup", { buttons: 0, clientX: 90, clientY: 30 }));
130138
expect(onDrop).toHaveBeenCalledExactlyOnceWith({ x: 90, y: 30 });
131139
expect(drag.onCancel).toHaveBeenCalledOnce();
@@ -148,7 +156,8 @@ describe("sidebar pointer lifecycle", () => {
148156
document.dispatchEvent(pointer("pointermove", { clientX: 30, clientY: 30 }));
149157
document.dispatchEvent(pointer("pointermove", { clientX: 90, clientY: 100 }));
150158
document.dispatchEvent(pointer("pointermove", { clientX: 90, clientY: 200 }));
151-
expect(drag.onMove).toHaveBeenCalledExactlyOnceWith({ x: 30, y: 30 });
159+
expect(drag.onMove).toHaveBeenCalledTimes(2);
160+
expect(drag.onMove).toHaveBeenLastCalledWith({ x: 30, y: 30 });
152161

153162
document.dispatchEvent(pointer("pointermove", { clientX: 30, clientY: 60 }));
154163
expect(drag.onMove).toHaveBeenLastCalledWith({ x: 30, y: 60 });

‎apps/web/src/components/Sidebar.pointer.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export class SidebarPointerSensor {
3434
private readonly document: Document;
3535
private readonly window: Window;
3636

37-
constructor(private readonly props: SensorProps<Options>) {
37+
// The settle sweep constructs this sensor directly, outside dnd-kit, so it
38+
// takes only the props the gesture reads.
39+
constructor(private readonly props: Omit<SensorProps<Options>, "activeNode" | "context">) {
3840
this.pointer = props.event as PointerEvent;
3941
this.document = getOwnerDocument(this.pointer.target);
4042
this.window = getWindow(this.pointer.target);
@@ -89,8 +91,9 @@ export class SidebarPointerSensor {
8991
this.document.addEventListener("selectionchange", this.clearSelection);
9092
this.clearSelection();
9193
this.props.onStart(this.coordinates());
92-
return;
9394
}
95+
// The move that starts a drag also moves it, so a release before the
96+
// next pointermove still lands where the pointer is.
9497
if (this.phase === "dragging") {
9598
if (event.cancelable) event.preventDefault();
9699
if (this.props.options.onMove?.(coordinates) === true) return;

0 commit comments

Comments
 (0)