fix(sidebar): keep context menu open during refresh
This commit is contained in:
4 files changed
+35
-9
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
import { createSidebarActionRequest, createSidebarActionTarget, findSidebarActionTarget, releaseRemovedSidebarActionTarget } from "@/lib/sidebar/sidebarActionTarget";
|
||||
import { createSidebarActionRequest, createSidebarActionTarget, findSidebarActionTarget, matchesSidebarActionTarget, releaseRemovedSidebarActionTarget } from "@/lib/sidebar/sidebarActionTarget";
|
||||
|
||||
function tableNode(): TreeNode {
|
||||
return {
|
||||
@@ -38,6 +38,19 @@ describe("sidebar action targets", () => {
|
||||
expect(Object.isFrozen(request)).toBe(true);
|
||||
});
|
||||
|
||||
it("matches a live node with the same action target identity", () => {
|
||||
const node = tableNode();
|
||||
|
||||
expect(matchesSidebarActionTarget(node, createSidebarActionTarget(node))).toBe(true);
|
||||
});
|
||||
|
||||
it("does not match the same id after identity fields change", () => {
|
||||
const target = createSidebarActionTarget(tableNode());
|
||||
const changedNode = { ...tableNode(), connectionId: "connection-2", label: "accounts" };
|
||||
|
||||
expect(matchesSidebarActionTarget(changedNode, target)).toBe(false);
|
||||
});
|
||||
|
||||
it("does not resolve a recycled row with the same local id", () => {
|
||||
const target = createSidebarActionTarget(tableNode());
|
||||
const recycled = tableNode();
|
||||
|
||||
@@ -12,13 +12,17 @@ function sameActionTarget(left: TreeNode, right: SidebarActionTarget): boolean {
|
||||
return left.id === right.id && left.type === right.type && left.connectionId === right.connectionId && left.database === right.database && left.schema === right.schema && left.catalog === right.catalog && left.label === right.label && left.signature === right.signature;
|
||||
}
|
||||
|
||||
export function matchesSidebarActionTarget(node: TreeNode, target: SidebarActionTarget): boolean {
|
||||
return sameActionTarget(node, target);
|
||||
}
|
||||
|
||||
export function findSidebarActionTarget(nodes: readonly TreeNode[], target: SidebarActionTarget): TreeNode | null {
|
||||
const visited = new WeakSet<TreeNode>();
|
||||
const find = (items: readonly TreeNode[]): TreeNode | null => {
|
||||
for (const node of items) {
|
||||
if (visited.has(node)) continue;
|
||||
visited.add(node);
|
||||
if (sameActionTarget(node, target)) return node;
|
||||
if (matchesSidebarActionTarget(node, target)) return node;
|
||||
const child = node.children ? find(node.children) : null;
|
||||
if (child) return child;
|
||||
const hiddenChild = node.hiddenChildren ? find(node.hiddenChildren) : null;
|
||||
|
||||
Reference in new issue
Block a user