Release-cycle review found two move-path issues: - Remote moves were placed at index:0 (broadcastPageMoved hardcodes index:0), so every observer rendered the moved node at the TOP of its new siblings until refetch. Client moveTreeNode now places by fractional position (treeModel.placeByPosition, mirroring addTreeNode/insertByPosition) and applies the payload's pageData (title->name, icon, hasChildren) so receivers keep the node correct. - Moving a page under a restricted ancestor left a stale named node (title/ slugId/icon) in the trees of users who lost visibility. broadcastPageMoved now derives one FRESH hasRestrictedAncestor decision and drives both paths from it: when restricted, the move goes to authorized users only (emitToAuthorizedUsers, not the space-cache-gated emitTreeEvent) and a compensating deleteTreeNode goes to the unauthorized complement (same fresh getUserIdsWithPageAccess set) — disjoint, no stale-cache window. Non-restricted moves are unchanged (one moveTreeNode to the room). Follow-up (noted): invalidateSpaceRestrictionCache is still unwired at permission-mutation sites; the open-space fast path can lag up to the 30s TTL, but the move/delete consistency above no longer depends on it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
276 lines
9.5 KiB
TypeScript
276 lines
9.5 KiB
TypeScript
import type { TreeNode, SiblingsInfo } from './tree-model.types';
|
|
|
|
function findInternal<T extends object>(
|
|
nodes: TreeNode<T>[],
|
|
id: string,
|
|
): { parents: TreeNode<T>[]; node: TreeNode<T> } | null {
|
|
for (const node of nodes) {
|
|
if (node.id === id) return { parents: [], node };
|
|
if (node.children) {
|
|
const inner = findInternal(node.children, id);
|
|
if (inner) return { parents: [node, ...inner.parents], node: inner.node };
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export const treeModel = {
|
|
find<T extends object>(tree: TreeNode<T>[], id: string): TreeNode<T> | null {
|
|
return findInternal(tree, id)?.node ?? null;
|
|
},
|
|
|
|
path<T extends object>(tree: TreeNode<T>[], id: string): TreeNode<T>[] | null {
|
|
const found = findInternal(tree, id);
|
|
if (!found) return null;
|
|
return [...found.parents, found.node];
|
|
},
|
|
|
|
siblingsOf<T extends object>(
|
|
tree: TreeNode<T>[],
|
|
id: string,
|
|
): SiblingsInfo<T> | null {
|
|
const found = findInternal(tree, id);
|
|
if (!found) return null;
|
|
const parent = found.parents[found.parents.length - 1];
|
|
const siblings = parent ? parent.children! : tree;
|
|
return {
|
|
parentId: parent?.id ?? null,
|
|
siblings,
|
|
index: siblings.findIndex((n) => n.id === id),
|
|
};
|
|
},
|
|
|
|
isDescendant<T extends object>(
|
|
tree: TreeNode<T>[],
|
|
ancestorId: string,
|
|
descendantId: string,
|
|
): boolean {
|
|
if (ancestorId === descendantId) return false;
|
|
const ancestor = treeModel.find(tree, ancestorId);
|
|
if (!ancestor?.children) return false;
|
|
return findInternal(ancestor.children, descendantId) !== null;
|
|
},
|
|
|
|
visible<T extends object>(
|
|
tree: TreeNode<T>[],
|
|
openIds: ReadonlySet<string>,
|
|
): TreeNode<T>[] {
|
|
const out: TreeNode<T>[] = [];
|
|
const walk = (nodes: TreeNode<T>[]) => {
|
|
for (const node of nodes) {
|
|
out.push(node);
|
|
if (openIds.has(node.id) && node.children?.length) walk(node.children);
|
|
}
|
|
};
|
|
walk(tree);
|
|
return out;
|
|
},
|
|
|
|
insert<T extends object>(
|
|
tree: TreeNode<T>[],
|
|
parentId: string | null,
|
|
node: TreeNode<T>,
|
|
index?: number,
|
|
): TreeNode<T>[] {
|
|
if (parentId === null) {
|
|
const idx = index ?? tree.length;
|
|
return [...tree.slice(0, idx), node, ...tree.slice(idx)];
|
|
}
|
|
let touched = false;
|
|
const walk = (nodes: TreeNode<T>[]): TreeNode<T>[] =>
|
|
nodes.map((n) => {
|
|
if (n.id === parentId) {
|
|
touched = true;
|
|
const kids = n.children ?? [];
|
|
const idx = index ?? kids.length;
|
|
return {
|
|
...n,
|
|
children: [...kids.slice(0, idx), node, ...kids.slice(idx)],
|
|
};
|
|
}
|
|
if (n.children) {
|
|
const next = walk(n.children);
|
|
if (next !== n.children) return { ...n, children: next };
|
|
}
|
|
return n;
|
|
});
|
|
const out = walk(tree);
|
|
return touched ? out : tree;
|
|
},
|
|
|
|
// Position-aware insert for server-authoritative broadcasts. The server does
|
|
// not know each receiver's local index (clients have different loaded sets and
|
|
// the root list is paginated), so it sends the node's fractional `position`.
|
|
// We insert among the already-loaded siblings ordered by `position` so the
|
|
// order is consistent across clients regardless of which nodes they loaded.
|
|
// Falls back to appending when `position` is missing.
|
|
insertByPosition<T extends { position?: string }>(
|
|
tree: TreeNode<T>[],
|
|
parentId: string | null,
|
|
node: TreeNode<T>,
|
|
): TreeNode<T>[] {
|
|
const index = (siblings: TreeNode<T>[]): number => {
|
|
const pos = node.position;
|
|
if (pos == null) return siblings.length;
|
|
// First sibling whose position sorts after the new node's position.
|
|
const at = siblings.findIndex(
|
|
(s) => s.position != null && s.position > pos,
|
|
);
|
|
return at === -1 ? siblings.length : at;
|
|
};
|
|
|
|
if (parentId === null) {
|
|
return treeModel.insert(tree, null, node, index(tree));
|
|
}
|
|
const parent = treeModel.find(tree, parentId);
|
|
const kids = (parent?.children as TreeNode<T>[] | undefined) ?? [];
|
|
return treeModel.insert(tree, parentId, node, index(kids));
|
|
},
|
|
|
|
remove<T extends object>(tree: TreeNode<T>[], id: string): TreeNode<T>[] {
|
|
let touched = false;
|
|
const walk = (nodes: TreeNode<T>[]): TreeNode<T>[] => {
|
|
const filtered = nodes.filter((n) => {
|
|
if (n.id === id) {
|
|
touched = true;
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
return filtered.map((n) => {
|
|
if (n.children) {
|
|
const next = walk(n.children);
|
|
if (next !== n.children) return { ...n, children: next };
|
|
}
|
|
return n;
|
|
});
|
|
};
|
|
const out = walk(tree);
|
|
return touched ? out : tree;
|
|
},
|
|
|
|
// `patch` excludes `id` (immutable) and `children` (use insert / remove /
|
|
// appendChildren for structural changes — otherwise referential identity of
|
|
// unrelated subtrees gets blown away).
|
|
update<T extends object>(
|
|
tree: TreeNode<T>[],
|
|
id: string,
|
|
patch: Omit<Partial<T>, "id" | "children">,
|
|
): TreeNode<T>[] {
|
|
let touched = false;
|
|
const walk = (nodes: TreeNode<T>[]): TreeNode<T>[] =>
|
|
nodes.map((n) => {
|
|
if (n.id === id) {
|
|
touched = true;
|
|
return { ...n, ...patch };
|
|
}
|
|
if (n.children) {
|
|
const next = walk(n.children);
|
|
if (next !== n.children) return { ...n, children: next };
|
|
}
|
|
return n;
|
|
});
|
|
const out = walk(tree);
|
|
return touched ? out : tree;
|
|
},
|
|
|
|
appendChildren<T extends object>(
|
|
tree: TreeNode<T>[],
|
|
parentId: string,
|
|
children: TreeNode<T>[],
|
|
): TreeNode<T>[] {
|
|
let touched = false;
|
|
const walk = (nodes: TreeNode<T>[]): TreeNode<T>[] =>
|
|
nodes.map((n) => {
|
|
if (n.id === parentId) {
|
|
const existing = n.children ?? [];
|
|
// Dedup against existing ids — auto-expand + manual toggle can race
|
|
// and produce overlapping fetches; we don't want React to see two
|
|
// children with the same key.
|
|
const existingIds = new Set(existing.map((c) => c.id));
|
|
const fresh = children.filter((c) => !existingIds.has(c.id));
|
|
if (fresh.length === 0) return n;
|
|
touched = true;
|
|
return { ...n, children: [...existing, ...fresh] };
|
|
}
|
|
if (n.children) {
|
|
const next = walk(n.children);
|
|
if (next !== n.children) return { ...n, children: next };
|
|
}
|
|
return n;
|
|
});
|
|
const out = walk(tree);
|
|
return touched ? out : tree;
|
|
},
|
|
|
|
place<T extends object>(
|
|
tree: TreeNode<T>[],
|
|
sourceId: string,
|
|
to: { parentId: string | null; index: number },
|
|
): TreeNode<T>[] {
|
|
const source = treeModel.find(tree, sourceId);
|
|
if (!source) return tree;
|
|
if (to.parentId !== null && !treeModel.find(tree, to.parentId)) return tree;
|
|
const removed = treeModel.remove(tree, sourceId);
|
|
return treeModel.insert(removed, to.parentId, source, to.index);
|
|
},
|
|
|
|
// Position-aware move for server-authoritative `moveTreeNode` broadcasts. Like
|
|
// `place`, but instead of an absolute index (which the sender computed against
|
|
// its own loaded set), it inserts the moved node among the destination's
|
|
// already-loaded siblings ordered by the node's fractional `position`. This
|
|
// keeps the visible order correct for every receiver — `place(..., index: 0)`
|
|
// would wrongly drop the node at the TOP of its new sibling list.
|
|
// Returns the same array reference (like `place`) when the source is missing
|
|
// or the destination parent isn't loaded on this client, so callers can detect
|
|
// that and fall back to removing the node.
|
|
placeByPosition<T extends { position?: string }>(
|
|
tree: TreeNode<T>[],
|
|
sourceId: string,
|
|
to: { parentId: string | null; position?: string },
|
|
): TreeNode<T>[] {
|
|
const source = treeModel.find(tree, sourceId);
|
|
if (!source) return tree;
|
|
if (to.parentId !== null && !treeModel.find(tree, to.parentId)) return tree;
|
|
const removed = treeModel.remove(tree, sourceId);
|
|
// Reuse the same position-ordered insertion as `insertByPosition` by
|
|
// stamping the authoritative position onto the moved node first.
|
|
const positioned = { ...source, position: to.position } as TreeNode<T>;
|
|
return treeModel.insertByPosition(removed, to.parentId, positioned);
|
|
},
|
|
|
|
move<T extends object>(
|
|
tree: TreeNode<T>[],
|
|
sourceId: string,
|
|
op: import('./tree-model.types').DropOp,
|
|
): { tree: TreeNode<T>[]; result: import('./tree-model.types').DropResult } {
|
|
if (sourceId === op.targetId) return { tree, result: { parentId: null, index: 0 } };
|
|
if (!treeModel.find(tree, sourceId) || !treeModel.find(tree, op.targetId)) {
|
|
return { tree, result: { parentId: null, index: 0 } };
|
|
}
|
|
if (treeModel.isDescendant(tree, sourceId, op.targetId)) {
|
|
return { tree, result: { parentId: null, index: 0 } };
|
|
}
|
|
|
|
let parentId: string | null;
|
|
let index: number;
|
|
|
|
if (op.kind === 'make-child') {
|
|
parentId = op.targetId;
|
|
const target = treeModel.find(tree, op.targetId)!;
|
|
index = target.children?.length ?? 0;
|
|
} else {
|
|
const info = treeModel.siblingsOf(tree, op.targetId)!;
|
|
parentId = info.parentId;
|
|
const sourceInfo = treeModel.siblingsOf(tree, sourceId)!;
|
|
const sameParent = sourceInfo.parentId === parentId;
|
|
const adjust =
|
|
sameParent && sourceInfo.index < info.index ? -1 : 0;
|
|
index = info.index + adjust + (op.kind === 'reorder-after' ? 1 : 0);
|
|
}
|
|
|
|
const next = treeModel.place(tree, sourceId, { parentId, index });
|
|
return { tree: next, result: { parentId, index } };
|
|
},
|
|
};
|