fix(sidebar): preserve nested pinned tree nodes

This commit is contained in:
t8y2
2026-05-15 11:26:19 +08:00
parent 6b31366b89
commit c56fe29e17
3 changed files with 38 additions and 8 deletions
+13
View File
@@ -1,3 +1,5 @@
import type { TreeNode } from "@/types/database";
export function orderPinnedFirst<T>(items: T[], isPinned: (item: T) => boolean): T[] {
const pinned: T[] = [];
const unpinned: T[] = [];
@@ -9,3 +11,14 @@ export function orderPinnedFirst<T>(items: T[], isPinned: (item: T) => boolean):
return [...pinned, ...unpinned];
}
export function applyPinnedTreeNodeState(nodes: TreeNode[], pinnedIds: Set<string>): TreeNode[] {
return orderPinnedFirst(
nodes.map((node) => ({
...node,
pinned: pinnedIds.has(node.id),
children: node.children ? applyPinnedTreeNodeState(node.children, pinnedIds) : node.children,
})),
(node) => !!node.pinned,
);
}
+2 -7
View File
@@ -2,7 +2,7 @@ import { defineStore } from "pinia";
import { uuid } from "@/lib/utils";
import { ref, computed, watch } from "vue";
import type { ColumnInfo, ConnectionConfig, SidebarLayout, TreeNode } from "@/types/database";
import { orderPinnedFirst } from "@/lib/pinnedItems";
import { applyPinnedTreeNodeState, orderPinnedFirst } from "@/lib/pinnedItems";
import {
reconcileLayout,
buildTreeNodesFromLayout,
@@ -232,13 +232,8 @@ export const useConnectionStore = defineStore("connection", () => {
return pinnedTreeNodeIds.value.has(id);
}
function pinTreeNode(node: TreeNode): TreeNode {
node.pinned = isTreeNodePinned(node.id);
return node;
}
function setChildren(parent: TreeNode, children: TreeNode[]) {
parent.children = orderPinnedFirst(children.map(pinTreeNode), (node) => !!node.pinned);
parent.children = applyPinnedTreeNodeState(children, pinnedTreeNodeIds.value);
loadedTreeNodeChildrenIds.value.add(parent.id);
}