1
1
import { Box , styled } from '@mui/material'
2
+ import { useAtomValue , useSetAtom } from 'jotai'
3
+ import { useAtomCallback } from 'jotai/utils'
2
4
import type React from 'react'
3
5
import { useCallback , useMemo , useState } from 'react'
4
6
5
- import { useTextColor } from '../hooks/useColor'
6
7
import { useClipboard } from '../hooks/useCopyToClipboard'
7
8
import { useInspect } from '../hooks/useInspect'
8
- import { useJsonViewerStore } from '../stores/JsonViewerStore'
9
+ import {
10
+ colorspaceAtom ,
11
+ editableAtom ,
12
+ enableClipboardAtom ,
13
+ hoverPathAtom ,
14
+ keyRendererAtom ,
15
+ onChangeAtom ,
16
+ quotesOnKeysAtom ,
17
+ rootNameAtom ,
18
+ setHoverAtomFamily ,
19
+ valueAtom
20
+ } from '../state'
9
21
import { useTypeComponents } from '../stores/typeRegistry'
10
22
import type { DataItemProps } from '../type'
11
23
import { getValueSize } from '../utils'
@@ -34,7 +46,7 @@ const IconBox = styled(props => <Box {...props} component='span'/>)`
34
46
export const DataKeyPair : React . FC < DataKeyPairProps > = ( props ) => {
35
47
const { value, path, nestedIndex } = props
36
48
const propsEditable = props . editable ?? undefined
37
- const storeEditable = useJsonViewerStore ( store => store . editable )
49
+ const storeEditable = useAtomValue ( editableAtom )
38
50
const editable = useMemo ( ( ) => {
39
51
if ( storeEditable === false ) {
40
52
return false
@@ -51,26 +63,33 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
51
63
const [ tempValue , setTempValue ] = useState ( typeof value === 'function' ? ( ) => value : value )
52
64
const depth = path . length
53
65
const key = path [ depth - 1 ]
54
- const hoverPath = useJsonViewerStore ( store => store . hoverPath )
66
+ const hoverPath = useAtomValue ( hoverPathAtom )
55
67
const isHover = useMemo ( ( ) => {
56
68
return hoverPath && path . every (
57
69
( value , index ) => value === hoverPath . path [ index ] && nestedIndex ===
58
70
hoverPath . nestedIndex )
59
71
} , [ hoverPath , path , nestedIndex ] )
60
- const setHover = useJsonViewerStore ( store => store . setHover )
61
- const root = useJsonViewerStore ( store => store . value )
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
+ )
78
+ const root = useAtomValue ( valueAtom )
62
79
const [ inspect , setInspect ] = useInspect ( path , value , nestedIndex )
63
80
const [ editing , setEditing ] = useState ( false )
64
- const onChange = useJsonViewerStore ( store => store . onChange )
65
- const keyColor = useTextColor ( )
66
- const numberKeyColor = useJsonViewerStore ( store => store . colorspace . base0C )
81
+ const onChange = useAtomValue ( onChangeAtom )
82
+ const {
83
+ base07 : keyColor ,
84
+ base0C : numberKeyColor
85
+ } = useAtomValue ( colorspaceAtom )
67
86
const { Component, PreComponent, PostComponent, Editor } = useTypeComponents ( value , path )
68
- const quotesOnKeys = useJsonViewerStore ( store => store . quotesOnKeys )
69
- const rootName = useJsonViewerStore ( store => store . rootName )
87
+ const quotesOnKeys = useAtomValue ( quotesOnKeysAtom )
88
+ const rootName = useAtomValue ( rootNameAtom )
70
89
const isRoot = root === value
71
90
const isNumberKey = Number . isInteger ( Number ( key ) )
72
91
73
- const enableClipboard = useJsonViewerStore ( store => store . enableClipboard )
92
+ const enableClipboard = useAtomValue ( enableClipboardAtom )
74
93
const { copy, copied } = useClipboard ( )
75
94
76
95
const actionIcons = useMemo ( ( ) => {
@@ -177,7 +196,7 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
177
196
178
197
const isEmptyValue = useMemo ( ( ) => getValueSize ( value ) === 0 , [ value ] )
179
198
const expandable = ! isEmptyValue && ! ! ( PreComponent && PostComponent )
180
- const KeyRenderer = useJsonViewerStore ( store => store . keyRenderer )
199
+ const KeyRenderer = useAtomValue ( keyRendererAtom )
181
200
const downstreamProps : DataItemProps = useMemo ( ( ) => ( {
182
201
path,
183
202
inspect,
@@ -187,10 +206,7 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
187
206
return (
188
207
< Box className = 'data-key-pair'
189
208
data-testid = { 'data-key-pair' + path . join ( '.' ) }
190
- onMouseEnter = {
191
- useCallback ( ( ) => setHover ( path , nestedIndex ) ,
192
- [ setHover , path , nestedIndex ] )
193
- }
209
+ onMouseEnter = { ( ) => setHover ( { path, nestedIndex } ) }
194
210
>
195
211
< DataBox
196
212
component = 'span'
@@ -209,7 +225,7 @@ export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
209
225
if ( ! isEmptyValue ) {
210
226
setInspect ( state => ! state )
211
227
}
212
- } , [ setInspect ] )
228
+ } , [ isEmptyValue , setInspect ] )
213
229
}
214
230
>
215
231
{
0 commit comments