fix(sidebar): preserve virtual table group ordering during refresh

Closes #9665
This commit is contained in:
t8y2 committed 2026-09-20 11:38:09 +08:00
1 parent a924285136
commit b040600e47
3 files changed
+205 -17

No files matched your search

+15 -17
View File
@@ -258,13 +258,15 @@ function rememberNaturalTreeNodeOrder(nodes: readonly TreeNode[]): void {
export function orderPinnedTreeNodes(nodes: TreeNode[], pinnedOrder: readonly string[] = [], isFixedPriority: FixedTreeNodePriority = () => false): TreeNode[] {
rememberNaturalTreeNodeOrder(nodes);
const virtualGroups: TreeNode[] = [];
const fixed: TreeNode[] = [];
const pinned: TreeNode[] = [];
const unpinned: TreeNode[] = [];
const orderByKey = new Map(normalizePinnedTreeNodeOrder(pinnedOrder).map((key, index) => [key, index] as const));
for (const node of nodes) {
if (isFixedPriority(node)) fixed.push(node);
if (node.type === "table-vgroup") virtualGroups.push(node);
else if (isFixedPriority(node)) fixed.push(node);
else if (node.pinned) pinned.push(node);
else unpinned.push(node);
}
@@ -280,7 +282,7 @@ export function orderPinnedTreeNodes(nodes: TreeNode[], pinnedOrder: readonly st
return naturalOrder(left, right);
});
unpinned.sort(naturalOrder);
return [...fixed, ...pinned, ...unpinned];
return [...virtualGroups, ...fixed, ...pinned, ...unpinned];
}
function findTreeNodeLocation(nodes: TreeNode[], target: TreeNode, parent: TreeNode | null = null): { node: TreeNode; parent: TreeNode | null } | null {
@@ -300,6 +302,7 @@ export function updatePinnedTreeNodeInPlace(nodes: TreeNode[], target: TreeNode,
if (!location) return "missing";
location.node.pinned = pinned;
if (location.parent?.type === "table-vgroup") return "siblings";
const siblings = location.parent?.children ?? nodes;
const ordered = orderPinnedTreeNodes(siblings);
@@ -321,39 +324,34 @@ function clonePinnedTreeNode(node: TreeNode, pinnedIds: Set<string>, pinnedOrder
};
clones.set(node, clone);
inheritNaturalTreeNodeOrder(node, clone);
if (node.children) clone.children = applyPinnedTreeNodeStateInternal(node.children, pinnedIds, pinnedOrder, isFixedPriority, clones);
if (node.hiddenChildren) clone.hiddenChildren = applyPinnedTreeNodeStateInternal(node.hiddenChildren, pinnedIds, pinnedOrder, isFixedPriority, clones);
if (node.children) clone.children = applyPinnedTreeNodeStateInternal(node.children, pinnedIds, pinnedOrder, isFixedPriority, clones, node.type === "table-vgroup");
if (node.hiddenChildren) clone.hiddenChildren = applyPinnedTreeNodeStateInternal(node.hiddenChildren, pinnedIds, pinnedOrder, isFixedPriority, clones, node.type === "table-vgroup");
return clone;
}
function applyPinnedTreeNodeStateInternal(nodes: TreeNode[], pinnedIds: Set<string>, pinnedOrder: readonly string[], isFixedPriority: FixedTreeNodePriority, clones: WeakMap<TreeNode, TreeNode>): TreeNode[] {
rememberNaturalTreeNodeOrder(nodes);
return orderPinnedTreeNodes(
nodes.map((node) => clonePinnedTreeNode(node, pinnedIds, pinnedOrder, isFixedPriority, clones)),
pinnedOrder,
isFixedPriority,
);
function applyPinnedTreeNodeStateInternal(nodes: TreeNode[], pinnedIds: Set<string>, pinnedOrder: readonly string[], isFixedPriority: FixedTreeNodePriority, clones: WeakMap<TreeNode, TreeNode>, preserveLayoutOrder = false): TreeNode[] {
if (!preserveLayoutOrder) rememberNaturalTreeNodeOrder(nodes);
const clonedNodes = nodes.map((node) => clonePinnedTreeNode(node, pinnedIds, pinnedOrder, isFixedPriority, clones));
return preserveLayoutOrder ? clonedNodes : orderPinnedTreeNodes(clonedNodes, pinnedOrder, isFixedPriority);
}
export function applyPinnedTreeNodeState(nodes: TreeNode[], pinnedIds: Set<string>, pinnedOrder: readonly string[] = [...pinnedIds], isFixedPriority: FixedTreeNodePriority = () => false): TreeNode[] {
return applyPinnedTreeNodeStateInternal(nodes, pinnedIds, pinnedOrder, isFixedPriority, new WeakMap());
}
function syncPinnedTreeNodeStateInPlaceInternal(nodes: TreeNode[], pinnedIds: Set<string>, pinnedOrder: readonly string[], isFixedPriority: FixedTreeNodePriority, visited: WeakSet<TreeNode>): void {
function syncPinnedTreeNodeStateInPlaceInternal(nodes: TreeNode[], pinnedIds: Set<string>, pinnedOrder: readonly string[], isFixedPriority: FixedTreeNodePriority, visited: WeakSet<TreeNode>, preserveLayoutOrder = false): void {
for (const node of nodes) {
if (visited.has(node)) continue;
visited.add(node);
node.pinned = pinnedIds.has(treeNodePinKey(node)) || pinnedIds.has(node.id);
if (node.children) {
syncPinnedTreeNodeStateInPlaceInternal(node.children, pinnedIds, pinnedOrder, isFixedPriority, visited);
node.children = orderPinnedTreeNodes(node.children, pinnedOrder, isFixedPriority);
syncPinnedTreeNodeStateInPlaceInternal(node.children, pinnedIds, pinnedOrder, isFixedPriority, visited, node.type === "table-vgroup");
}
if (node.hiddenChildren) {
syncPinnedTreeNodeStateInPlaceInternal(node.hiddenChildren, pinnedIds, pinnedOrder, isFixedPriority, visited);
node.hiddenChildren = orderPinnedTreeNodes(node.hiddenChildren, pinnedOrder, isFixedPriority);
syncPinnedTreeNodeStateInPlaceInternal(node.hiddenChildren, pinnedIds, pinnedOrder, isFixedPriority, visited, node.type === "table-vgroup");
}
}
nodes.splice(0, nodes.length, ...orderPinnedTreeNodes(nodes, pinnedOrder, isFixedPriority));
if (!preserveLayoutOrder) nodes.splice(0, nodes.length, ...orderPinnedTreeNodes(nodes, pinnedOrder, isFixedPriority));
}
export function syncPinnedTreeNodeStateInPlace(nodes: TreeNode[], pinnedIds: Set<string>, pinnedOrder: readonly string[] = [...pinnedIds], isFixedPriority: FixedTreeNodePriority = () => false): void {
+107
View File
@@ -1,4 +1,6 @@
import { describe, expect, it } from "vitest";
import { reactive } from "vue";
import { applyPinnedTreeNodeState, syncPinnedTreeNodeStateInPlace, treeNodePinKey, updatePinnedTreeNodeInPlace } from "@/lib/app/pinnedItems";
import type { TreeNode } from "@/types/database";
import {
applyTableVGroupsToChildren,
@@ -18,6 +20,7 @@ import {
tableVGroupPathForTable,
tableVGroupScopeKey,
toggleTableVGroupCollapsed,
type TableVGroupLayout,
} from "@/lib/table/tableVGroup";
const SCOPE = { connectionId: "conn-1", database: "db1" };
@@ -175,6 +178,110 @@ describe("applyTableVGroupsToChildren", () => {
});
});
describe("table virtual groups with pinned tree ordering", () => {
function groupedTree() {
const tables = ["t_a", "t_b", "t_c", "t_d", "t_e", "t_f"].map(tableNode);
const loadMore = otherNode("load-more");
const container: TreeNode = { id: "database", label: "db1", type: "database", ...SCOPE, children: [...tables, loadMore] };
const tree = [container];
const pinOrder = [treeNodePinKey(tables[5]!), treeNodePinKey(tables[0]!)];
const pinnedIds = new Set(pinOrder);
const layout: TableVGroupLayout = {
version: 1,
groups: [
{ id: "business", name: "Business", collapsed: false },
{ id: "nested", name: "Nested", collapsed: true },
{ id: "audit", name: "Audit", collapsed: false },
],
order: [
{
type: "group",
id: "business",
children: [
{ type: "table", name: "t_d" },
{ type: "group", id: "nested", children: [{ type: "table", name: "t_b" }] },
{ type: "table", name: "t_a" },
],
},
{ type: "group", id: "audit", children: [{ type: "table", name: "t_c" }] },
],
};
syncPinnedTreeNodeStateInPlace(tree, pinnedIds, pinOrder);
container.children = applyTableVGroupsToChildren(container.children!, layout, SCOPE);
return { tree, container, tables, loadMore, layout, pinnedIds, pinOrder };
}
it.each(["in-place", "reactive", "clone"])("preserves root and nested layout order during repeated %s pin synchronization", (mode) => {
const fixture = groupedTree();
let tree = mode === "reactive" ? reactive(fixture.tree) : fixture.tree;
const snapshot = () => JSON.stringify(tree, ["id", "children", "isExpanded"]);
const expected = snapshot();
const savedLayout = JSON.stringify(fixture.layout);
for (let iteration = 0; iteration < 3; iteration++) {
if (mode === "clone") tree = applyPinnedTreeNodeState(tree, fixture.pinnedIds, fixture.pinOrder);
else syncPinnedTreeNodeStateInPlace(tree, fixture.pinnedIds, fixture.pinOrder);
expect(snapshot()).toBe(expected);
}
expect(tree[0]!.children?.map((node) => node.label)).toEqual(["Business", "Audit", "t_f", "t_e", "load-more"]);
expect(tree[0]!.children?.[0]?.children?.map((node) => node.label)).toEqual(["t_d", "Nested", "t_a"]);
expect(tree[0]!.children?.[0]?.children?.[2]?.pinned).toBe(true);
expect(JSON.stringify(fixture.layout)).toBe(savedLayout);
if (mode === "in-place") {
expect(tree[0]!.children?.[0]?.children?.[0]).toBe(fixture.tables[3]);
expect(tree[0]!.children?.at(-1)).toBe(fixture.loadMore);
}
});
it("keeps explicit group member order when a member is pinned or unpinned directly", () => {
const { tree, container, tables } = groupedTree();
const members = container.children![0]!.children!;
const expected = members.map((node) => node.id);
expect(updatePinnedTreeNodeInPlace(tree, tables[0]!, false)).toBe("siblings");
expect(container.children![0]!.children!.map((node) => node.id)).toEqual(expected);
expect(updatePinnedTreeNodeInPlace(tree, tables[0]!, true)).toBe("siblings");
expect(container.children![0]!.children!.map((node) => node.id)).toEqual(expected);
});
it("uses the latest group layout after drag reordering", () => {
const { tree, container, layout, pinnedIds, pinOrder } = groupedTree();
const reordered = reorderTableVGroupEntry(layout, "audit", "business", "before");
container.children = applyTableVGroupsToChildren(container.children!, reordered, SCOPE);
syncPinnedTreeNodeStateInPlace(tree, pinnedIds, pinOrder);
expect(groupIds(container.children!)).toEqual(["table-vgroup:audit", "table-vgroup:business"]);
});
it("restores ordinary pinned and natural ordering when groups are disabled", () => {
const { tree, container, tables, layout, pinnedIds, pinOrder } = groupedTree();
syncPinnedTreeNodeStateInPlace(tree, pinnedIds, pinOrder);
container.children = applyTableVGroupsToChildren(stripTableVGroupsFromChildren(container.children!), { ...layout, enabled: false }, SCOPE);
syncPinnedTreeNodeStateInPlace(tree, pinnedIds, pinOrder);
expect(container.children?.map((node) => node.label)).toEqual(["t_f", "t_a", "t_b", "t_c", "t_d", "t_e", "load-more"]);
syncPinnedTreeNodeStateInPlace(tree, new Set());
expect(container.children).toEqual([...tables, container.children!.at(-1)]);
});
it("still orders metadata below grouped tables by pin state", () => {
const { tree, tables, pinnedIds, pinOrder } = groupedTree();
const firstColumn: TreeNode = { id: "column-a", label: "a", type: "column", ...SCOPE };
const lastColumn: TreeNode = { id: "column-z", label: "z", type: "column", ...SCOPE };
tables[3]!.children = [firstColumn, lastColumn];
const columnKey = treeNodePinKey(lastColumn);
syncPinnedTreeNodeStateInPlace(tree, new Set([...pinnedIds, columnKey]), [...pinOrder, columnKey]);
expect(tables[3]!.children).toEqual([lastColumn, firstColumn]);
syncPinnedTreeNodeStateInPlace(tree, pinnedIds, pinOrder);
expect(tables[3]!.children).toEqual([firstColumn, lastColumn]);
});
});
describe("resolveTableVGroupScopeFromNode", () => {
const databaseNode: TreeNode = { id: "conn-1:main", label: "main", type: "database", connectionId: "conn-1", database: "main" };
const tablesGroup: TreeNode = { id: "conn-1:main:__tables", label: "Tables", type: "group-tables", connectionId: "conn-1", database: "main", schema: "main", children: [] };