Skip to content

Commit 2ae8628

Browse files
committed
Remove AtomFamily
1 parent 5098030 commit 2ae8628

File tree

4 files changed

+74
-80
lines changed

4 files changed

+74
-80
lines changed

src/components/DataKeyPair.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Box, styled } from '@mui/material'
22
import { useAtomValue, useSetAtom } from 'jotai'
3-
import { useAtomCallback } from 'jotai/utils'
43
import type React from 'react'
54
import { useCallback, useMemo, useState } from 'react'
65

@@ -15,7 +14,7 @@ import {
1514
onChangeAtom,
1615
quotesOnKeysAtom,
1716
rootNameAtom,
18-
setHoverAtomFamily,
17+
setHoverAtom,
1918
valueAtom
2019
} from '../state'
2120
import { useTypeComponents } from '../stores/typeRegistry'
@@ -69,12 +68,7 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
6968
(value, index) => value === hoverPath.path[index] && nestedIndex ===
7069
hoverPath.nestedIndex)
7170
}, [hoverPath, path, nestedIndex])
72-
const setHover = useAtomCallback(
73-
useCallback((get, set, arg) => {
74-
// eslint-disable-next-line react-hooks/rules-of-hooks
75-
useSetAtom(setHoverAtomFamily(arg))
76-
}, [])
77-
)
71+
const setHover = useSetAtom(setHoverAtom)
7872
const root = useAtomValue(valueAtom)
7973
const [inspect, setInspect] = useInspect(path, value, nestedIndex)
8074
const [editing, setEditing] = useState(false)
@@ -206,7 +200,10 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
206200
return (
207201
<Box className='data-key-pair'
208202
data-testid={'data-key-pair' + path.join('.')}
209-
onMouseEnter={() => setHover({ path, nestedIndex })}
203+
onMouseEnter={
204+
useCallback(() => setHover({ path, nestedIndex }),
205+
[setHover, path, nestedIndex])
206+
}
210207
>
211208
<DataBox
212209
component='span'

src/hooks/useInspect.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
import { useAtomValue, useSetAtom } from 'jotai'
2-
import { useAtomCallback } from 'jotai/utils'
3-
import { useCallback, useEffect, useState } from 'react'
2+
import {
3+
Dispatch,
4+
SetStateAction,
5+
useCallback,
6+
useEffect,
7+
useState
8+
} from 'react'
49

510
import {
611
defaultInspectDepthAtom,
7-
getInspectCacheAtomFamily,
8-
setInspectCacheAtomFamily
12+
getInspectCacheAtom,
13+
setInspectCacheAtom
914
} from '../state'
1015
import { useIsCycleReference } from './useIsCycleReference'
1116

1217
export function useInspect (path: (string | number)[], value: any, nestedIndex?: number) {
1318
const depth = path.length
1419
const isTrap = useIsCycleReference(path, value)
1520
const defaultInspectDepth = useAtomValue(defaultInspectDepthAtom)
16-
17-
const getInspectCache = useAtomCallback(
18-
useCallback((get, set, arg) => {
19-
// eslint-disable-next-line react-hooks/rules-of-hooks
20-
useAtomValue(getInspectCacheAtomFamily(arg))
21-
}, [])
22-
)
23-
const setInspectCache = useAtomCallback(
24-
useCallback((get, set, arg) => {
25-
// eslint-disable-next-line react-hooks/rules-of-hooks
26-
useSetAtom(setInspectCacheAtomFamily(arg))
27-
}, [])
28-
)
21+
const getInspectCache = useSetAtom(getInspectCacheAtom)
22+
const setInspectCache = useSetAtom(setInspectCacheAtom)
2923
useEffect(() => {
3024
const inspect = getInspectCache({ path, nestedIndex })
3125
if (inspect !== undefined) {
@@ -41,8 +35,8 @@ export function useInspect (path: (string | number)[], value: any, nestedIndex?:
4135
setInspectCache({ path, inspect })
4236
}
4337
}, [defaultInspectDepth, depth, isTrap, nestedIndex, path, getInspectCache, setInspectCache])
44-
const shouldInspect = useAtomValue(getInspectCacheAtomFamily({ path, nestedIndex }))
45-
const [inspect, setOriginal] = useState<boolean>(() => {
38+
const [inspect, set] = useState<boolean>(() => {
39+
const shouldInspect = getInspectCache({ path, nestedIndex })
4640
if (shouldInspect !== undefined) {
4741
return shouldInspect
4842
}
@@ -53,15 +47,12 @@ export function useInspect (path: (string | number)[], value: any, nestedIndex?:
5347
? false
5448
: depth < defaultInspectDepth
5549
})
56-
const setInspect = useAtomCallback(
57-
useCallback((get, set, apply) => {
58-
setOriginal((oldState) => {
59-
const newState = typeof apply === 'boolean' ? apply : apply(oldState)
60-
// eslint-disable-next-line react-hooks/rules-of-hooks
61-
useSetAtom(setInspectCacheAtomFamily(apply))
62-
return newState
63-
})
64-
}, [])
65-
)
50+
const setInspect = useCallback<Dispatch<SetStateAction<boolean>>>((apply) => {
51+
set((oldState) => {
52+
const newState = typeof apply === 'boolean' ? apply : apply(oldState)
53+
setInspectCache({ path, newState, nestedIndex })
54+
return newState
55+
})
56+
}, [nestedIndex, path, setInspectCache])
6657
return [inspect, setInspect] as const
6758
}

src/index.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createTheme, Paper, ThemeProvider } from '@mui/material'
22
import type { Atom } from 'jotai'
33
import { useAtom, useSetAtom } from 'jotai'
4-
import { useAtomCallback } from 'jotai/utils'
54
import type React from 'react'
65
import { useCallback, useEffect, useMemo, useRef } from 'react'
76

@@ -19,9 +18,9 @@ import {
1918
maxDisplayLengthAtom,
2019
onChangeAtom,
2120
onCopyAtom,
22-
registryTypesAtomFamily,
21+
registryTypesAtom,
2322
rootNameAtom,
24-
setHoverAtomFamily,
23+
setHoverAtom,
2524
valueAtom
2625
} from './state'
2726
import {
@@ -75,32 +74,28 @@ const JsonViewerInner: React.FC<JsonViewerProps> = (props) => {
7574
} else if (typeof props.theme === 'object') {
7675
setColorspace(props.theme)
7776
}
78-
}, [props.theme])
77+
}, [props.theme, setColorspace])
7978
const onceRef = useRef(true)
8079
const predefinedTypes = useMemo(() => predefined(), [])
80+
const registerTypes = useSetAtom(registryTypesAtom)
8181
if (onceRef.current) {
8282
const allTypes = [...predefinedTypes]
8383
props.valueTypes?.forEach(type => {
8484
allTypes.push(type)
8585
})
86-
useSetAtom(registryTypesAtomFamily(allTypes))
86+
registerTypes(allTypes)
8787
onceRef.current = false
8888
}
8989
useEffect(() => {
9090
const allTypes = [...predefinedTypes]
9191
props.valueTypes?.forEach(type => {
9292
allTypes.push(type)
9393
})
94-
useSetAtom(registryTypesAtomFamily(allTypes))
95-
}, [predefinedTypes, props.valueTypes])
94+
registerTypes(allTypes)
95+
}, [predefinedTypes, props.valueTypes, registerTypes])
9696

9797
const value = useAtom(valueAtom)
98-
const setHover = useAtomCallback(
99-
useCallback((get, set, arg) => {
100-
// eslint-disable-next-line react-hooks/rules-of-hooks
101-
useSetAtom(setHoverAtomFamily(arg))
102-
}, [])
103-
)
98+
const setHover = useSetAtom(setHoverAtom)
10499
return (
105100
<Paper
106101
elevation={0}
@@ -111,7 +106,11 @@ const JsonViewerInner: React.FC<JsonViewerProps> = (props) => {
111106
userSelect: 'none',
112107
contentVisibility: 'auto'
113108
}}
114-
onMouseLeave={() => setHover(null)}
109+
onMouseLeave={
110+
useCallback(() => {
111+
setHover(null)
112+
}, [setHover])
113+
}
115114
>
116115
<DataKeyPair
117116
value={value}

src/state.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { atom } from 'jotai'
2-
import { atomFamily } from 'jotai/utils'
32

43
import type { JsonViewerState, TypeRegistryState } from './type'
54

@@ -24,36 +23,44 @@ export const inspectCacheAtom = atom<JsonViewerState['inspectCache'] | undefined
2423
export const hoverPathAtom = atom<JsonViewerState['hoverPath'] | undefined>(undefined)
2524
export const registryAtom = atom<TypeRegistryState['registry'] | undefined>(undefined)
2625

27-
export const getInspectCacheAtomFamily = atomFamily(({ path, nestedIndex }) => {
28-
const target = nestedIndex === undefined
29-
? path.join('.')
30-
: `${path.join('.')}[${nestedIndex}]nt`
31-
return atom((get) => get(inspectCacheAtom)[target])
32-
})
33-
export const setInspectCacheAtomFamily = atomFamily(({ path, action, nestedIndex }) => atom((get, set) => {
34-
const target = nestedIndex === undefined
35-
? path.join('.')
36-
: `${path.join('.')}[${nestedIndex}]nt`
37-
const inspectCache = get(inspectCacheAtom)
38-
return set(inspectCacheAtom, {
39-
...inspectCache,
40-
[target]: typeof action === 'function'
41-
? action(inspectCache[target])
42-
: action
43-
})
44-
}))
45-
export const setHoverAtomFamily = atomFamily(({ path, nestedIndex }) => atom(
46-
(get, set) => set(hoverPathAtom, path
26+
export const getInspectCacheAtom = atom(
27+
(get) => get(inspectCacheAtom),
28+
(get, _set, { path, nestedIndex }) => {
29+
const target = nestedIndex === undefined
30+
? path.join('.')
31+
: `${path.join('.')}[${nestedIndex}]nt`
32+
return get(inspectCacheAtom)[target]
33+
}
34+
)
35+
export const setInspectCacheAtom = atom(
36+
(get) => get(inspectCacheAtom),
37+
(get, set, { path, action, nestedIndex }) => {
38+
const target = nestedIndex === undefined
39+
? path.join('.')
40+
: `${path.join('.')}[${nestedIndex}]nt`
41+
const inspectCache = get(inspectCacheAtom)
42+
return set(inspectCacheAtom, {
43+
...inspectCache,
44+
[target]: typeof action === 'function'
45+
? action(inspectCache[target])
46+
: action
47+
})
48+
}
49+
)
50+
export const setHoverAtom = atom(
51+
(get) => get(hoverPathAtom),
52+
(_get, set, { path, nestedIndex }) => set(hoverPathAtom, path
4753
? { path, nestedIndex }
4854
: null
4955
)
50-
))
51-
export const registryTypesAtomFamily = atomFamily((setState) => atom(
52-
(get, set) => {
56+
)
57+
58+
export const registryTypesAtom = atom(
59+
(get) => get(registryAtom),
60+
(get, set, setState) => {
5361
if (typeof setState === 'function') {
54-
set(registryAtom, setState)
55-
return
62+
return setState(get(registryAtom))
5663
}
57-
return setState
64+
return set(registryAtom, setState)
5865
}
59-
))
66+
)

0 commit comments

Comments
 (0)