mono/packages/ui/src/components/tree/hooks/use-fresh-node.ts
2026-03-26 23:01:41 +01:00

17 lines
564 B
TypeScript

import { useMemo } from "react";
import { useTreeApi } from "../context";
import { IdObj } from "../types/utils";
export function useFreshNode<T>(index: number) {
const tree = useTreeApi<T>();
const original = tree.at(index);
if (!original) throw new Error(`Could not find node for index: ${index}`);
return useMemo(() => {
const fresh = original.clone();
tree.visibleNodes[index] = fresh; // sneaky
return fresh;
// Return a fresh instance if the state values change
}, [...Object.values(original.state), original]);
}