@@ -6,6 +6,7 @@ import { useTextColor } from '../../hooks/useColor'
6
6
import { useIsCycleReference } from '../../hooks/useIsCycleReference'
7
7
import { useJsonViewerStore } from '../../stores/JsonViewerStore'
8
8
import type { DataItemProps } from '../../type'
9
+ import { getValueSize } from '../../utils'
9
10
import { DataKeyPair } from '../DataKeyPair'
10
11
import { CircularArrowsIcon } from '../icons/CircularArrowsIcon'
11
12
import { DataBox } from '../mui/DataBox'
@@ -16,15 +17,11 @@ const objectRb = '}'
16
17
const arrayRb = ']'
17
18
18
19
function inspectMetadata ( value : object ) {
19
- let length
20
+ const length = getValueSize ( value )
21
+
20
22
let name = ''
21
- if ( Array . isArray ( value ) ) {
22
- length = value . length
23
- } else if ( value instanceof Map || value instanceof Set ) {
23
+ if ( value instanceof Map || value instanceof Set ) {
24
24
name = value [ Symbol . toStringTag ]
25
- length = value . size
26
- } else {
27
- length = Object . keys ( value ) . length
28
25
}
29
26
if ( Object . prototype . hasOwnProperty . call ( value , Symbol . toStringTag ) ) {
30
27
name = ( value as any ) [ Symbol . toStringTag ]
@@ -36,9 +33,8 @@ export const PreObjectType: React.FC<DataItemProps<object>> = (props) => {
36
33
const metadataColor = useJsonViewerStore ( store => store . colorspace . base04 )
37
34
const textColor = useTextColor ( )
38
35
const isArray = useMemo ( ( ) => Array . isArray ( props . value ) , [ props . value ] )
39
- const sizeOfValue = useMemo (
40
- ( ) => props . inspect ? inspectMetadata ( props . value ) : '' ,
41
- [ props . inspect , props . value ]
36
+ const isEmptyValue = useMemo ( ( ) => getValueSize ( props . value ) === 0 , [ props . value ] )
37
+ const sizeOfValue = useMemo ( ( ) => inspectMetadata ( props . value ) , [ props . inspect , props . value ]
42
38
)
43
39
const displayObjectSize = useJsonViewerStore ( store => store . displayObjectSize )
44
40
const isTrap = useIsCycleReference ( props . path , props . value )
@@ -50,20 +46,18 @@ export const PreObjectType: React.FC<DataItemProps<object>> = (props) => {
50
46
} }
51
47
>
52
48
{ isArray ? arrayLb : objectLb }
53
- { displayObjectSize
54
- ? (
55
- < Box
56
- component = 'span'
57
- sx = { {
58
- pl : 0.5 ,
59
- fontStyle : 'italic' ,
60
- color : metadataColor
61
- } }
62
- >
63
- { sizeOfValue }
64
- </ Box >
65
- )
66
- : null }
49
+ { displayObjectSize && props . inspect && ! isEmptyValue && (
50
+ < Box
51
+ component = 'span'
52
+ sx = { {
53
+ pl : 0.5 ,
54
+ fontStyle : 'italic' ,
55
+ color : metadataColor
56
+ } }
57
+ >
58
+ { sizeOfValue }
59
+ </ Box >
60
+ ) }
67
61
68
62
{ isTrap && ! props . inspect
69
63
? (
@@ -85,14 +79,13 @@ export const PostObjectType: React.FC<DataItemProps<object>> = (props) => {
85
79
const metadataColor = useJsonViewerStore ( store => store . colorspace . base04 )
86
80
const isArray = useMemo ( ( ) => Array . isArray ( props . value ) , [ props . value ] )
87
81
const displayObjectSize = useJsonViewerStore ( store => store . displayObjectSize )
88
- const sizeOfValue = useMemo (
89
- ( ) => ! props . inspect ? inspectMetadata ( props . value ) : '' ,
90
- [ props . inspect , props . value ]
91
- )
82
+ const isEmptyValue = useMemo ( ( ) => getValueSize ( props . value ) === 0 , [ props . value ] )
83
+ const sizeOfValue = useMemo ( ( ) => inspectMetadata ( props . value ) , [ props . inspect , props . value ] )
84
+
92
85
return (
93
86
< Box component = 'span' className = 'data-object-end' >
94
87
{ isArray ? arrayRb : objectRb }
95
- { displayObjectSize
88
+ { displayObjectSize && ( isEmptyValue || ! props . inspect )
96
89
? (
97
90
< Box
98
91
component = 'span'
@@ -118,12 +111,9 @@ function getIterator (value: any): value is Iterable<unknown> {
118
111
export const ObjectType : React . FC < DataItemProps < object > > = ( props ) => {
119
112
const keyColor = useTextColor ( )
120
113
const borderColor = useJsonViewerStore ( store => store . colorspace . base02 )
121
- const groupArraysAfterLength = useJsonViewerStore (
122
- store => store . groupArraysAfterLength )
114
+ const groupArraysAfterLength = useJsonViewerStore ( store => store . groupArraysAfterLength )
123
115
const isTrap = useIsCycleReference ( props . path , props . value )
124
- const [ displayLength , setDisplayLength ] = useState (
125
- useJsonViewerStore ( store => store . maxDisplayLength )
126
- )
116
+ const [ displayLength , setDisplayLength ] = useState ( useJsonViewerStore ( store => store . maxDisplayLength ) )
127
117
const objectSortKeys = useJsonViewerStore ( store => store . objectSortKeys )
128
118
const elements = useMemo ( ( ) => {
129
119
if ( ! props . inspect ) {
@@ -201,10 +191,9 @@ export const ObjectType: React.FC<DataItemProps<object>> = (props) => {
201
191
// object
202
192
let entries : [ key : string , value : unknown ] [ ] = Object . entries ( value )
203
193
if ( objectSortKeys ) {
204
- entries = entries . sort ( ( [ a ] , [ b ] ) => objectSortKeys === true
205
- ? a . localeCompare ( b )
206
- : objectSortKeys ( a , b )
207
- )
194
+ entries = objectSortKeys === true
195
+ ? entries . sort ( ( [ a ] , [ b ] ) => a . localeCompare ( b ) )
196
+ : entries . sort ( ( [ a ] , [ b ] ) => objectSortKeys ( a , b ) )
208
197
}
209
198
const elements = entries . slice ( 0 , displayLength ) . map ( ( [ key , value ] ) => {
210
199
const path = [ ...props . path , key ]
@@ -243,6 +232,10 @@ export const ObjectType: React.FC<DataItemProps<object>> = (props) => {
243
232
const marginLeft = props . inspect ? 0.6 : 0
244
233
const width = useJsonViewerStore ( store => store . indentWidth )
245
234
const indentWidth = props . inspect ? width - marginLeft : width
235
+ const isEmptyValue = useMemo ( ( ) => getValueSize ( props . value ) === 0 , [ props . value ] )
236
+ if ( isEmptyValue ) {
237
+ return null
238
+ }
246
239
return (
247
240
< Box
248
241
className = 'data-object'
0 commit comments