Skip to content

Commit c9f6d32

Browse files
committed
feat: finish basic view of next component
1 parent dfafd4b commit c9f6d32

File tree

1 file changed

+113
-32
lines changed

1 file changed

+113
-32
lines changed

src/next.tsx

Lines changed: 113 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ThemeProvider
99
} from '@mui/material'
1010
import type { OverridesStyleRules } from '@mui/material/styles/overrides'
11+
import { DevelopmentError } from '@textea/dev-kit/utils'
1112
import type React from 'react'
1213
import { useCallback, useDebugValue, useEffect, useMemo } from 'react'
1314

@@ -52,6 +53,68 @@ function getType (value: unknown) {
5253
return type
5354
}
5455

56+
const needExpand = (value: unknown): boolean => {
57+
const type = getType(value)
58+
switch (type) {
59+
case 'object':
60+
case 'function':
61+
case 'array':
62+
return true
63+
default:
64+
return false
65+
}
66+
}
67+
68+
const getEndingClosure = (value: unknown): string => {
69+
const type = getType(value)
70+
switch (type) {
71+
case 'object':
72+
case 'function':
73+
return '}'
74+
case 'array':
75+
return ']'
76+
default:
77+
throw new DevelopmentError()
78+
}
79+
}
80+
81+
function shortPreviewValue (value: unknown): string {
82+
const type = getType(value)
83+
if (type === 'function') {
84+
return (value as Function).toString().slice(9, -1).replace(/\{[\s\S]+/, '')
85+
} else if (type === 'array') {
86+
return '[...]'
87+
} else if (type === 'object') {
88+
return '{...}'
89+
} else {
90+
return `${value}`
91+
}
92+
}
93+
94+
function longPreviewValue (value: unknown): string {
95+
const type = getType(value)
96+
if (type === 'function') {
97+
const functionHead = (value as Function).toString()
98+
.slice(9, -1)
99+
.replace(/\{[\s\S]+/, '')
100+
return `${functionHead} {`
101+
} else if (type === 'array') {
102+
return ' ['
103+
} else if (type === 'object') {
104+
return '{'
105+
} else {
106+
return ''
107+
}
108+
}
109+
110+
function shortPreviewKeyValuePair (key: string, value: unknown): string {
111+
return `${key}: ${shortPreviewValue(value)}`
112+
}
113+
114+
function longPreviewKeyValuePair (key: string, value: unknown): string {
115+
return `${key}: ${longPreviewValue(value)}`
116+
}
117+
55118
const ObjectJson: React.FC<DataProps> = ({
56119
value,
57120
isRoot,
@@ -76,51 +139,69 @@ const ObjectJson: React.FC<DataProps> = ({
76139
onNodeToggle={handleToggle}
77140
>
78141
<TreeItem nodeId='data-viewer-root' label='root'>
79-
{
80-
Object.entries(value as object).map(([key, value]) => {
81-
const path = `${father}${father ? '.' : ''}${key}`
82-
const isExpend = expanded.includes(path)
83-
return (
84-
<TreeItem
85-
nodeId={path}
86-
key={key}
87-
label={isExpend ? path : `${key} : ${value}`}
88-
>
89-
<ObjectJson
90-
father={key}
91-
value={value}
92-
isRoot={false}
93-
/>
94-
</TreeItem>
95-
)
96-
})
97-
}
142+
<ObjectJson
143+
father='root'
144+
value={value}
145+
isRoot={false}
146+
/>
98147
</TreeItem>
99148
</TreeView>
100149
)
101150
} else {
102151
return (
103152
Object.entries(value as object).map(([key, value]) => {
104153
const path = `${father}${father ? '.' : ''}${key}`
105-
return (
106-
<TreeItem
107-
nodeId={path}
108-
key={key}
109-
label={`${value}`}
110-
>
111-
<ObjectJson
112-
father={key}
113-
value={value}
114-
isRoot={false}
154+
const isExpend = expanded.includes(path)
155+
const shouldExpand = needExpand(value)
156+
if (shouldExpand) {
157+
return (
158+
<TreeItem
159+
nodeId={path}
160+
key={key}
161+
label={isExpend
162+
? longPreviewKeyValuePair(key, value)
163+
: shortPreviewKeyValuePair(key, value)}
164+
>
165+
{
166+
shouldExpand
167+
? (
168+
<ObjectJson
169+
father={key}
170+
value={value}
171+
isRoot={false}
172+
/>
173+
)
174+
: null
175+
}
176+
{shouldExpand && isExpend && (
177+
<TreeItem nodeId={`${path}-ending`}
178+
label={getEndingClosure(value)}/>
179+
)}
180+
</TreeItem>
181+
)
182+
} else {
183+
return (
184+
<TreeItem
185+
nodeId={path}
186+
key={key}
187+
label={shortPreviewKeyValuePair(key, value)}
115188
/>
116-
</TreeItem>
117-
)
189+
)
190+
}
118191
})
119192
)
120193
}
121194
} else {
122195
const path = `${father}${father ? '.' : ''}${value}`
123-
return <TreeItem nodeId={path} label={`${value}`}/>
196+
const type = getType(value)
197+
if (type === 'function') {
198+
const entire = (value as Function).toString()
199+
const label = entire.slice(entire.indexOf('{') + 1,
200+
entire.lastIndexOf('}'))
201+
return <TreeItem nodeId={path} label={label}/>
202+
} else {
203+
return <TreeItem nodeId={path} label={`${value}`}/>
204+
}
124205
}
125206
}, [expanded, father, handleToggle, isRoot, type, value])
126207
return <>{elements}</>

0 commit comments

Comments
 (0)