| 1 | import { useCallback, useEffect, useRef, type Dispatch, type SetStateAction } from "react"; |
| 2 | import { app } from "./bridge"; |
| 3 | import type { ProjectNode } from "./types"; |
| 4 | |
| 5 | type RuntimeProjection = { |
| 6 | apply(tree: ProjectNode[]): ProjectNode[]; |
| 7 | dispose(): void; |
| 8 | }; |
| 9 | |
| 10 | export function useProjectTreeRuntimeProjection( |
| 11 | setTree: Dispatch<SetStateAction<ProjectNode[]>>, |
| 12 | excludedTopicIds: () => ReadonlySet<string>, |
| 13 | ) { |
| 14 | const projectionRef = useRef<RuntimeProjection | null>(null); |
| 15 | useEffect(() => { |
| 16 | let active = true; |
| 17 | void import("./projectTreeRuntime").then((runtime) => { |
| 18 | if (active) projectionRef.current = runtime.bindProjectTreeRuntime( |
| 19 | setTree, |
| 20 | () => app.GetProjectTreeRuntimeSnapshot?.(), |
| 21 | excludedTopicIds, |
| 22 | ); |
| 23 | }).catch(() => {}); |
| 24 | return () => { |
| 25 | active = false; |
| 26 | projectionRef.current?.dispose(); |
| 27 | }; |
| 28 | }, [excludedTopicIds, setTree]); |
| 29 | return useCallback((tree: ProjectNode[]) => projectionRef.current?.apply(tree) ?? tree, []); |
| 30 | } |
| 31 |