Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

Explorer.tsx

209 lines4.7 KB

1// @ts-nocheck
2​
3import React from 'react'
4​
5import { styled } from './utils'
6​
7export const Entry = styled('div', {
8 fontFamily: 'Menlo, monospace',
9 fontSize: '0.9em',
10 lineHeight: '1.7',
11 outline: 'none',
12 wordBreak: 'break-word',
13})
14​
15export const Label = styled('span', {
16 cursor: 'pointer',
17 color: 'white',
18})
19​
20export const Value = styled('span', (props, theme) => ({
21 color: theme.danger,
22}))
23​
24export const SubEntries = styled('div', {
25 marginLeft: '.1em',
26 paddingLeft: '1em',
27 borderLeft: '2px solid rgba(0,0,0,.15)',
28})
29​
30export const Info = styled('span', {
31 color: 'grey',
32 fontSize: '.7em',
33})
34​
35export const Expander = ({ expanded, style = {}, ...rest }) => (
36 <span
37 style={{
38 display: 'inline-block',
39 transition: 'all .1s ease',
40 transform: `rotate(${expanded ? 90 : 0}deg) ${style.transform || ''}`,
41 ...style,
42 }}
43 >
44 β–Ά
45 </span>
47​
48const DefaultRenderer = ({
49 handleEntry,
50 label,
51 value,
52 // path,
53 subEntries,
54 subEntryPages,
55 type,
56 // depth,
57 expanded,
58 toggle,
59 pageSize,
60}) => {
61 const [expandedPages, setExpandedPages] = React.useState([])
62​
63 return (
64 <Entry key={label}>
65 {subEntryPages?.length ? (
66 <>
67 <Label onClick={() => toggle()}>
68 <Expander expanded={expanded} /> {label}{' '}
69 <Info>
70 {String(type).toLowerCase() === 'iterable' ? '(Iterable) ' : ''}
71 {subEntries.length} {subEntries.length > 1 ? `items` : `item`}
72 </Info>
73 </Label>
74 {expanded ? (
75 subEntryPages.length === 1 ? (
76 <SubEntries>
77 {subEntries.map(entry => handleEntry(entry))}
78 </SubEntries>
79 ) : (
80 <SubEntries>
81 {subEntryPages.map((entries, index) => (
82 <div key={index}>
83 <Entry>
84 <Label
85 onClick={() =>
86 setExpandedPages(old =>
87 old.includes(index)
88 ? old.filter(d => d !== index)
89 : [...old, index]
90 )
91 }
92 >
93 <Expander expanded={expanded} /> [{index * pageSize} ...{' '}
94 {index * pageSize + pageSize - 1}]
95 </Label>
96 {expandedPages.includes(index) ? (
97 <SubEntries>
98 {entries.map(entry => handleEntry(entry))}
99 </SubEntries>
100 ) : null}
101 </Entry>
102 </div>
103 ))}
104 </SubEntries>
105 )
106 ) : null}
107 </>
108 ) : (
109 <>
110 <Label>{label}:</Label>{' '}
111 <Value>
112 {JSON.stringify(value, Object.getOwnPropertyNames(Object(value)))}
113 </Value>
114 </>
115 )}
116 </Entry>
117 )
119​
120export default function Explorer({
121 value,
122 defaultExpanded,
123 renderer = DefaultRenderer,
124 pageSize = 100,
125 depth = 0,
126 ...rest
127}) {
128 const [expanded, setExpanded] = React.useState(defaultExpanded)
129​
130 const toggle = set => {
131 setExpanded(old => (typeof set !== 'undefined' ? set : !old))
132 }
133​
134 const path = []
135​
136 let type = typeof value
137 let subEntries
138 const subEntryPages = []
139​
140 const makeProperty = sub => {
141 const newPath = path.concat(sub.label)
142 const subDefaultExpanded =
143 defaultExpanded === true
144 ? { [sub.label]: true }
145 : defaultExpanded?.[sub.label]
146 return {
147 ...sub,
148 path: newPath,
149 depth: depth + 1,
150 defaultExpanded: subDefaultExpanded,
151 }
152 }
153​
154 if (Array.isArray(value)) {
155 type = 'array'
156 subEntries = value.map((d, i) =>
157 makeProperty({
158 label: i,
159 value: d,
160 })
161 )
162 } else if (
163 value !== null &&
164 typeof value === 'object' &&
165 typeof value[Symbol.iterator] === 'function'
166 ) {
167 type = 'Iterable'
168 subEntries = Array.from(value, (val, i) =>
169 makeProperty({
170 label: i,
171 value: val,
172 })
173 )
174 } else if (typeof value === 'object' && value !== null) {
175 type = 'object'
176 // eslint-disable-next-line no-shadow
177 subEntries = Object.entries(value).map(([label, value]) =>
178 makeProperty({
179 label,
180 value,
181 })
182 )
183 }
184​
185 if (subEntries) {
186 let i = 0
187​
188 while (i < subEntries.length) {
189 subEntryPages.push(subEntries.slice(i, i + pageSize))
190 i = i + pageSize
191 }
192 }
193​
194 return renderer({
195 handleEntry: entry => (
196 <Explorer key={entry.label} renderer={renderer} {...rest} {...entry} />
197 ),
198 type,
199 subEntries,
200 subEntryPages,
201 depth,
202 value,
203 path,
204 expanded,
205 toggle,
206 pageSize,
207 ...rest,
208 })