1
1
import { Box } from '@mui/material'
2
+ import { useAtomValue , useSetAtom } from 'jotai'
3
+ import { useAtomCallback } from 'jotai/utils'
2
4
import type { ComponentProps , FC , MouseEvent } 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'
@@ -43,7 +55,7 @@ const IconBox: FC<IconBoxProps> = (props) => (
43
55
export const DataKeyPair : FC < DataKeyPairProps > = ( props ) => {
44
56
const { value, path, nestedIndex } = props
45
57
const propsEditable = props . editable ?? undefined
46
- const storeEditable = useJsonViewerStore ( store => store . editable )
58
+ const storeEditable = useAtomValue ( editableAtom )
47
59
const editable = useMemo ( ( ) => {
48
60
if ( storeEditable === false ) {
49
61
return false
@@ -60,26 +72,33 @@ export const DataKeyPair: FC<DataKeyPairProps> = (props) => {
60
72
const [ tempValue , setTempValue ] = useState ( typeof value === 'function' ? ( ) => value : value )
61
73
const depth = path . length
62
74
const key = path [ depth - 1 ]
63
- const hoverPath = useJsonViewerStore ( store => store . hoverPath )
75
+ const hoverPath = useAtomValue ( hoverPathAtom )
64
76
const isHover = useMemo ( ( ) => {
65
77
return hoverPath && path . every (
66
78
( value , index ) => value === hoverPath . path [ index ] && nestedIndex ===
67
79
hoverPath . nestedIndex )
68
80
} , [ hoverPath , path , nestedIndex ] )
69
- const setHover = useJsonViewerStore ( store => store . setHover )
70
- const root = useJsonViewerStore ( store => store . value )
81
+ const setHover = useAtomCallback (
82
+ useCallback ( ( get , set , arg ) => {
83
+ // eslint-disable-next-line react-hooks/rules-of-hooks
84
+ useSetAtom ( setHoverAtomFamily ( arg ) )
85
+ } , [ ] )
86
+ )
87
+ const root = useAtomValue ( valueAtom )
71
88
const [ inspect , setInspect ] = useInspect ( path , value , nestedIndex )
72
89
const [ editing , setEditing ] = useState ( false )
73
- const onChange = useJsonViewerStore ( store => store . onChange )
74
- const keyColor = useTextColor ( )
75
- const numberKeyColor = useJsonViewerStore ( store => store . colorspace . base0C )
90
+ const onChange = useAtomValue ( onChangeAtom )
91
+ const {
92
+ base07 : keyColor ,
93
+ base0C : numberKeyColor
94
+ } = useAtomValue ( colorspaceAtom )
76
95
const { Component, PreComponent, PostComponent, Editor } = useTypeComponents ( value , path )
77
- const quotesOnKeys = useJsonViewerStore ( store => store . quotesOnKeys )
78
- const rootName = useJsonViewerStore ( store => store . rootName )
96
+ const quotesOnKeys = useAtomValue ( quotesOnKeysAtom )
97
+ const rootName = useAtomValue ( rootNameAtom )
79
98
const isRoot = root === value
80
99
const isNumberKey = Number . isInteger ( Number ( key ) )
81
100
82
- const enableClipboard = useJsonViewerStore ( store => store . enableClipboard )
101
+ const enableClipboard = useAtomValue ( enableClipboardAtom )
83
102
const { copy, copied } = useClipboard ( )
84
103
85
104
const actionIcons = useMemo ( ( ) => {
@@ -161,7 +180,7 @@ export const DataKeyPair: FC<DataKeyPairProps> = (props) => {
161
180
162
181
const isEmptyValue = useMemo ( ( ) => getValueSize ( value ) === 0 , [ value ] )
163
182
const expandable = ! isEmptyValue && ! ! ( PreComponent && PostComponent )
164
- const KeyRenderer = useJsonViewerStore ( store => store . keyRenderer )
183
+ const KeyRenderer = useAtomValue ( keyRendererAtom )
165
184
const downstreamProps : DataItemProps = useMemo ( ( ) => ( {
166
185
path,
167
186
inspect,
@@ -173,10 +192,7 @@ export const DataKeyPair: FC<DataKeyPairProps> = (props) => {
173
192
className = 'data-key-pair'
174
193
data-testid = { 'data-key-pair' + path . join ( '.' ) }
175
194
sx = { { userSelect : 'text' } }
176
- onMouseEnter = {
177
- useCallback ( ( ) => setHover ( path , nestedIndex ) ,
178
- [ setHover , path , nestedIndex ] )
179
- }
195
+ onMouseEnter = { ( ) => setHover ( { path, nestedIndex } ) }
180
196
>
181
197
< DataBox
182
198
component = 'span'
0 commit comments