MCPcopy Create free account
hub / github.com/triggerdotdev/jsonhero-web / JsonTreeView

Function JsonTreeView

app/components/JsonTreeView.tsx:14–114  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12import { Mono } from "./Primitives/Mono";
13
14export function JsonTreeView() {
15 const { selectedNodeId, selectedNodeSource } = useJsonColumnViewState();
16 const { goToNodeId } = useJsonColumnViewAPI();
17
18 const { tree, parentRef } = useJsonTreeViewContext();
19
20 // Scroll to the selected node when this component is first rendered.
21 const scrolledToNodeRef = useRef(false);
22
23 useEffect(() => {
24 if (!scrolledToNodeRef.current && selectedNodeId) {
25 tree.scrollToNode(selectedNodeId);
26 scrolledToNodeRef.current = true;
27 }
28 }, [selectedNodeId, scrolledToNodeRef]);
29
30 // Yup, this is hacky.
31 // This is to prevent the selection not changing the first time you try to move to a new node in the tree
32 const focusCount = useRef<number>(0);
33
34 // This focuses and scrolls to the selected node when the selectedNodeId
35 // is set from a source other than this tree (e.g. the search bar, path bar, related values).
36 useEffect(() => {
37 if (
38 tree.focusedNodeId &&
39 selectedNodeId &&
40 tree.focusedNodeId !== selectedNodeId
41 ) {
42 if (selectedNodeId === "$") {
43 return;
44 }
45
46 if (selectedNodeSource !== "tree" && focusCount.current > 0) {
47 focusCount.current = focusCount.current + 1;
48 tree.focusNode(selectedNodeId);
49 tree.scrollToNode(selectedNodeId);
50 }
51 }
52 }, [tree.focusedNodeId, goToNodeId, selectedNodeId, selectedNodeSource]);
53
54 // This is what syncs the tree view's focused node to the column view selected node
55 const previousFocusedNodeId = useRef<string | null>(null);
56
57 useEffect(() => {
58 let updated = false;
59
60 if (!previousFocusedNodeId.current) {
61 previousFocusedNodeId.current = tree.focusedNodeId;
62 updated = true;
63 }
64
65 if (
66 tree.focusedNodeId &&
67 (updated || previousFocusedNodeId.current !== tree.focusedNodeId)
68 ) {
69 previousFocusedNodeId.current = tree.focusedNodeId;
70 goToNodeId(tree.focusedNodeId, "tree");
71 }

Callers

nothing calls this directly

Calls 4

useJsonColumnViewStateFunction · 0.90
useJsonColumnViewAPIFunction · 0.90
useJsonTreeViewContextFunction · 0.90
useJsonDocFunction · 0.90

Tested by

no test coverage detected