()
| 12 | import { Mono } from "./Primitives/Mono"; |
| 13 | |
| 14 | export 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 | } |
nothing calls this directly
no test coverage detected