mirror of
https://github.com/t8y2/dbx.git
synced 2026-10-31 00:22:30 +00:00
25 lines
689 B
TypeScript
25 lines
689 B
TypeScript
import type { TreeNode } from "@/types/database";
|
|
|
|
export function orderPinnedFirst<T>(items: T[], isPinned: (item: T) => boolean): T[] {
|
|
const pinned: T[] = [];
|
|
const unpinned: T[] = [];
|
|
|
|
for (const item of items) {
|
|
if (isPinned(item)) pinned.push(item);
|
|
else unpinned.push(item);
|
|
}
|
|
|
|
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,
|
|
);
|
|
}
|