fix(sidebar): clear selection after connection moves

This commit is contained in:
William Yang authored and GitHub committed 2026-08-11 21:37:31 +08:00
1 parent 8d084cc655
commit c6f9ddb5a4
8 files changed
+576 -23

No files matched your search

@@ -0,0 +1,89 @@
import { describe, expect, it } from "vitest";
import { applyConnectionMultiSelection, connectionMultiSelectionAfterToggle, emptyConnectionMultiSelection, releaseConnectionFromMultiSelection } from "@/lib/sidebar/sidebarConnectionMultiSelect";
import type { ConnectionMultiSelection, ConnectionMultiSelectionTarget } from "@/lib/sidebar/sidebarConnectionMultiSelect";
function tick(selection: ConnectionMultiSelection, ...connectionIds: string[]): ConnectionMultiSelection {
return connectionIds.reduce(connectionMultiSelectionAfterToggle, selection);
}
function target(): ConnectionMultiSelectionTarget {
return { selectedTreeNodeIds: [], selectedTreeNodeId: null, treeSelectionAnchorId: null, connectionMultiSelectActive: false };
}
describe("connection multi-selection", () => {
it("adds each ticked connection to the running selection", () => {
const selection = tick(emptyConnectionMultiSelection(), "conn-1", "conn-2", "conn-3");
expect(selection.connectionIds).toEqual(["conn-1", "conn-2", "conn-3"]);
expect(selection.activeConnectionId).toBe("conn-3");
expect(selection.anchorConnectionId).toBe("conn-3");
expect(selection.active).toBe(true);
});
it("drops a connection that is ticked twice", () => {
const selection = tick(emptyConnectionMultiSelection(), "conn-1", "conn-2", "conn-2");
expect(selection.connectionIds).toEqual(["conn-1"]);
expect(selection.activeConnectionId).toBe("conn-1");
expect(selection.active).toBe(true);
});
it("leaves multi-select off once the last connection is unticked", () => {
const selection = tick(emptyConnectionMultiSelection(), "conn-1", "conn-1");
expect(selection.connectionIds).toEqual([]);
expect(selection.activeConnectionId).toBeNull();
expect(selection.active).toBe(false);
});
it("writes the selection back to the tree selection fields", () => {
const store = target();
applyConnectionMultiSelection(store, tick(emptyConnectionMultiSelection(), "conn-1", "conn-2"));
expect(store).toEqual({
selectedTreeNodeIds: ["conn-1", "conn-2"],
selectedTreeNodeId: "conn-2",
treeSelectionAnchorId: "conn-2",
connectionMultiSelectActive: true,
});
});
it("clears every tree selection field when the selection is released", () => {
const store = target();
applyConnectionMultiSelection(store, tick(emptyConnectionMultiSelection(), "conn-1"));
applyConnectionMultiSelection(store, emptyConnectionMultiSelection());
expect(store).toEqual({
selectedTreeNodeIds: [],
selectedTreeNodeId: null,
treeSelectionAnchorId: null,
connectionMultiSelectActive: false,
});
});
it("releases only the connection that was moved from a multi-selection", () => {
const store = target();
applyConnectionMultiSelection(store, tick(emptyConnectionMultiSelection(), "conn-1", "conn-2", "conn-3"));
releaseConnectionFromMultiSelection(store, "conn-3");
expect(store).toEqual({
selectedTreeNodeIds: ["conn-1", "conn-2"],
selectedTreeNodeId: "conn-1",
treeSelectionAnchorId: "conn-1",
connectionMultiSelectActive: true,
});
});
it("leaves an unrelated multi-selection unchanged", () => {
const store = target();
applyConnectionMultiSelection(store, tick(emptyConnectionMultiSelection(), "conn-1", "conn-2"));
releaseConnectionFromMultiSelection(store, "conn-3");
expect(store.selectedTreeNodeIds).toEqual(["conn-1", "conn-2"]);
expect(store.connectionMultiSelectActive).toBe(true);
});
});
@@ -0,0 +1,66 @@
/**
* Connection multi-selection state for the sidebar tree.
*
* The transitions live here rather than in the components because the checkbox
* that grows the selection and the bulk actions that release it sit in two
* different files, and only their combination decides what the next bulk action
* operates on.
*/
export type ConnectionMultiSelection = {
connectionIds: string[];
activeConnectionId: string | null;
anchorConnectionId: string | null;
active: boolean;
};
/** The tree selection fields a multi-selection is written back to. */
export type ConnectionMultiSelectionTarget = {
selectedTreeNodeIds: string[];
selectedTreeNodeId: string | null;
treeSelectionAnchorId: string | null;
connectionMultiSelectActive: boolean;
};
export function emptyConnectionMultiSelection(): ConnectionMultiSelection {
return { connectionIds: [], activeConnectionId: null, anchorConnectionId: null, active: false };
}
/**
* Ticking a checkbox adds to the current selection while multi-select is on, and
* starts a new one when it is off. A bulk action that forgets to release the
* selection therefore hands its connections to the next one (issue #5758).
*/
export function connectionMultiSelectionAfterToggle(selection: Pick<ConnectionMultiSelection, "connectionIds" | "active">, connectionId: string): ConnectionMultiSelection {
const next = new Set(selection.active ? selection.connectionIds : []);
if (next.has(connectionId)) next.delete(connectionId);
else next.add(connectionId);
const connectionIds = [...next];
return {
connectionIds,
activeConnectionId: next.has(connectionId) ? connectionId : (connectionIds[0] ?? null),
anchorConnectionId: connectionId,
active: connectionIds.length > 0,
};
}
export function applyConnectionMultiSelection(target: ConnectionMultiSelectionTarget, selection: ConnectionMultiSelection): void {
target.selectedTreeNodeIds = selection.connectionIds;
target.selectedTreeNodeId = selection.activeConnectionId;
target.treeSelectionAnchorId = selection.anchorConnectionId;
target.connectionMultiSelectActive = selection.active;
}
export function releaseConnectionFromMultiSelection(target: ConnectionMultiSelectionTarget, connectionId: string): void {
if (!target.connectionMultiSelectActive || !target.selectedTreeNodeIds.includes(connectionId)) return;
const connectionIds = target.selectedTreeNodeIds.filter((id) => id !== connectionId);
const activeConnectionId = target.selectedTreeNodeId && connectionIds.includes(target.selectedTreeNodeId) ? target.selectedTreeNodeId : (connectionIds[0] ?? null);
const anchorConnectionId = target.treeSelectionAnchorId && connectionIds.includes(target.treeSelectionAnchorId) ? target.treeSelectionAnchorId : activeConnectionId;
applyConnectionMultiSelection(target, {
connectionIds,
activeConnectionId,
anchorConnectionId,
active: connectionIds.length > 0,
});
}