Skip to content

Commit ec1a5ff

Browse files
committed
Fix inspectCache atom
1 parent 5fe6188 commit ec1a5ff

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@emotion/styled": "^11.10.5",
6464
"@mui/material": "^5.11.4",
6565
"copy-to-clipboard": "^3.3.3",
66+
"fast-deep-equal": "^3.1.3",
6667
"group-items": "^2.2.0",
6768
"jotai": "^1.13.0"
6869
},

src/hooks/useInspect.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import {
1212
getInspectCacheAtom,
1313
setInspectCacheAtom
1414
} from '../state'
15+
import type { HostPath, JsonViewerProps } from '../type'
1516
import { useIsCycleReference } from './useIsCycleReference'
1617

17-
export function useInspect (path: (string | number)[], value: any, nestedIndex?: number) {
18+
export function useInspect (
19+
path: HostPath['path'],
20+
value: JsonViewerProps['value'],
21+
nestedIndex?: HostPath['nestedIndex']
22+
) {
1823
const depth = path.length
1924
const isTrap = useIsCycleReference(path, value)
2025
const defaultInspectDepth = useAtomValue(defaultInspectDepthAtom)
21-
const getInspectCache = useSetAtom(getInspectCacheAtom)
26+
const inspectCache = useAtomValue(getInspectCacheAtom({ path, nestedIndex }))
2227
const setInspectCache = useSetAtom(setInspectCacheAtom)
2328
useEffect(() => {
24-
const inspect = getInspectCache({ path, nestedIndex })
25-
if (inspect !== undefined) {
29+
if (inspectCache !== undefined) {
2630
return
2731
}
2832
if (nestedIndex !== undefined) {
@@ -34,11 +38,10 @@ export function useInspect (path: (string | number)[], value: any, nestedIndex?:
3438
: depth < defaultInspectDepth
3539
setInspectCache({ path, inspect })
3640
}
37-
}, [defaultInspectDepth, depth, isTrap, nestedIndex, path, getInspectCache, setInspectCache])
41+
}, [defaultInspectDepth, depth, isTrap, nestedIndex, path, inspectCache, setInspectCache])
3842
const [inspect, set] = useState<boolean>(() => {
39-
const shouldInspect = getInspectCache({ path, nestedIndex })
40-
if (shouldInspect !== undefined) {
41-
return shouldInspect
43+
if (inspectCache !== undefined) {
44+
return inspectCache
4245
}
4346
if (nestedIndex !== undefined) {
4447
return false

src/state.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import deepEqual from 'fast-deep-equal'
12
import { atom } from 'jotai'
3+
import { atomFamily } from 'jotai/utils'
24

35
import { lightColorspace } from './theme/base16'
46
import type { JsonViewerState, TypeRegistryState } from './type'
@@ -20,19 +22,20 @@ export const collapseStringsAfterLengthAtom = atom<JsonViewerState['collapseStri
2022
export const defaultInspectDepthAtom = atom<JsonViewerState['defaultInspectDepth'] | undefined>(undefined)
2123
export const objectSortKeysAtom = atom<JsonViewerState['objectSortKeys'] | undefined>(undefined)
2224
export const quotesOnKeysAtom = atom<JsonViewerState['quotesOnKeys'] | undefined>(undefined)
23-
export const inspectCacheAtom = atom<JsonViewerState['inspectCache'] | undefined>({})
25+
export const inspectCacheAtom = atom<JsonViewerState['inspectCache']>({})
2426
export const hoverPathAtom = atom<JsonViewerState['hoverPath'] | null>(null)
2527
export const registryAtom = atom<TypeRegistryState['registry']>([])
2628

27-
export const getInspectCacheAtom = atom(
28-
(get) => get(inspectCacheAtom),
29-
(get, _set, { path, nestedIndex }) => {
29+
// TODO check: if memory leaks, add to last line of useEffect:
30+
// return () => { atomFamily.remove ... // Anything in here is fired on component unmount }
31+
export const getInspectCacheAtom = atomFamily(({ path, nestedIndex }) => atom(
32+
(get) => {
3033
const target = nestedIndex === undefined
3134
? path.join('.')
3235
: `${path.join('.')}[${nestedIndex}]nt`
3336
return get(inspectCacheAtom)[target]
3437
}
35-
)
38+
), deepEqual)
3639
export const setInspectCacheAtom = atom(
3740
(get) => get(inspectCacheAtom),
3841
(get, set, { path, action, nestedIndex }) => {
@@ -50,10 +53,9 @@ export const setInspectCacheAtom = atom(
5053
)
5154
export const setHoverAtom = atom(
5255
(get) => get(hoverPathAtom),
53-
(_get, set, { path, nestedIndex }) => set(hoverPathAtom, path
54-
? { path, nestedIndex }
55-
: null
56-
)
56+
(_get, set, { path, nestedIndex }) => {
57+
set(hoverPathAtom, path ? { path, nestedIndex } : null)
58+
}
5759
)
5860

5961
export const registryTypesAtom = atom(

src/type.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,14 @@ export type JsonViewerProps<T = unknown> = {
164164
displayObjectSize?: boolean
165165
}
166166

167+
export type HostPath = {
168+
path: Path
169+
nestedIndex?: number
170+
}
171+
167172
export type JsonViewerState<T = unknown> = {
168173
inspectCache: Record<string, boolean>
169-
hoverPath: { path: Path; nestedIndex?: number } | null
174+
hoverPath: HostPath | null
170175
indentWidth: number
171176
groupArraysAfterLength: number
172177
enableClipboard: boolean

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,7 @@ __metadata:
18321832
eslint-plugin-simple-import-sort: ^8.0.0
18331833
eslint-plugin-unused-imports: ^2.0.0
18341834
expect-type: ^0.15.0
1835+
fast-deep-equal: ^3.1.3
18351836
group-items: ^2.2.0
18361837
husky: ^8.0.3
18371838
jotai: ^1.13.0

0 commit comments

Comments
 (0)