()
| 4 | import { useJsonColumnViewState } from "./useJsonColumnView"; |
| 5 | |
| 6 | export function useRelatedPaths(): string[] { |
| 7 | const cache = useRef(new Map<string, Array<string>>()); |
| 8 | const { selectedNodeId } = useJsonColumnViewState(); |
| 9 | const [json] = useJson(); |
| 10 | |
| 11 | return useMemo(() => { |
| 12 | if (!selectedNodeId) return []; |
| 13 | |
| 14 | //check cache |
| 15 | const cachedPaths = cache.current.get(selectedNodeId); |
| 16 | if (cachedPaths) { |
| 17 | return cachedPaths; |
| 18 | } |
| 19 | |
| 20 | //fetch result |
| 21 | let paths = getRelatedPathsAtPath(selectedNodeId, json); |
| 22 | |
| 23 | //cache |
| 24 | for (let index = 0; index < paths.length; index++) { |
| 25 | const path = paths[index]; |
| 26 | cache.current.set(path, paths); |
| 27 | } |
| 28 | |
| 29 | return paths; |
| 30 | }, [selectedNodeId, json]); |
| 31 | } |
no test coverage detected