-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathDocsPane.js
More file actions
334 lines (308 loc) · 9.54 KB
/
Copy pathDocsPane.js
File metadata and controls
334 lines (308 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { css } from '@firebolt-dev/css'
import { useEffect, useMemo, useRef, useState } from 'react'
import {
BookIcon,
SearchIcon,
LoaderIcon,
ArrowRightIcon,
XIcon,
RotateCcwIcon,
} from 'lucide-react'
import documentationData from '../public/data/docs.json'
import { usePane } from './usePane'
export function Docspane({ world, close }) {
const paneRef = useRef()
const headRef = useRef()
usePane('docs', paneRef, headRef)
const [query, setQuery] = useState('')
const [refresh, setRefresh] = useState(0)
return (
<div
ref={paneRef}
className='docspane'
css={css`
position: absolute;
top: 20px;
left: 20px;
width: 540px;
height: 600px;
background: rgba(22, 22, 28, 1);
border: 1px solid rgba(255, 255, 255, 0.03);
border-radius: 10px;
box-shadow: rgba(0, 0, 0, 0.5) 0px 10px 30px;
pointer-events: auto;
display: flex;
flex-direction: column;
.docspane-head {
height: 50px;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
display: flex;
align-items: center;
padding: 0 13px 0 20px;
&-title {
padding-left: 7px;
font-weight: 500;
flex: 1;
}
&-search {
width: 150px;
display: flex;
align-items: center;
svg {
margin-right: 5px;
}
input {
flex: 1;
font-size: 14px;
background: transparent;
border: none;
color: white;
outline: none;
&::placeholder {
color: rgba(255, 255, 255, 0.5);
}
}
}
&-btn {
width: 30px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
color: rgba(255, 255, 255, 0.5);
&:hover {
cursor: pointer;
color: white;
}
}
}
`}
>
<div className='docspane-head' ref={headRef}>
<BookIcon size={16} />
<div className='docspane-head-title'>Documentation</div>
<div className='docspane-head-search'>
<SearchIcon size={16} />
<input type='text' placeholder='Search' value={query} onChange={e => setQuery(e.target.value)} />
</div>
<div className='docspane-head-btn' onClick={() => setRefresh(n => n + 1)}>
<RotateCcwIcon size={16} />
</div>
<div className='docspane-head-btn' onClick={() => world.emit('docs', null)}>
<XIcon size={20} />
</div>
</div>
<DocspaneContent world={world} query={query} refresh={refresh} />
</div>
)
}
function DocspaneContent({ query, refresh }) {
const [expandedSections, setExpandedSections] = useState({})
const [data, setData] = useState(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
useEffect(() => {
const loadData = async () => {
try {
setLoading(true)
setData(documentationData)
} catch (err) {
setError(err)
} finally {
setLoading(false)
}
}
loadData()
}, [refresh])
const filteredData = useMemo(() => {
if (!data || !query) return data
const lowerCaseQuery = query.toLowerCase()
return Object.keys(data).reduce((acc, key) => {
const item = data[key]
const matchesTitle = item.title.toLowerCase().includes(lowerCaseQuery)
const matchesDescription = item.description.toLowerCase().includes(lowerCaseQuery)
const matchesProperties = Object.values(item.properties || {}).some(prop =>
prop.description.toLowerCase().includes(lowerCaseQuery)
)
const matchesMethods = Object.values(item.methods || {}).some(method =>
method.description.toLowerCase().includes(lowerCaseQuery)
)
if (matchesTitle || matchesDescription || matchesProperties || matchesMethods) {
acc[key] = item
}
return acc
}, {})
}, [data, query])
const toggleSection = (key) => {
setExpandedSections(prevState => {
// Check if the section is already expanded
if (prevState[key]) {
return {
...prevState,
[key]: false
}
}
// Close all other sections and expand the clicked one
const newExpanded = Object.keys(prevState).reduce((acc, k) => {
acc[k] = false
return acc
}, {})
newExpanded[key] = true
return newExpanded
})
}
if (loading) {
return (
<div className='docspane-loader' css={css`
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
color: rgba(255, 255, 255, 0.5);
`}>
<LoaderIcon size={16} />
<span style={{ marginLeft: '10px' }}>Loading...</span>
</div>
)
}
if (error) {
return (
<div className='docspane-error' css={css`
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
color: rgba(255, 0, 0, 0.5);
`}>
<XIcon size={16} />
<span style={{ marginLeft: '10px' }}>Error loading documentation</span>
</div>
)
}
return (
<div
className='docspane-content'
css={css`
flex: 1;
padding: 20px 20px 0;
overflow-y: auto;
maxHeight: 450px;
scrollbar-width: none; /* For Firefox */
&::-webkit-scrollbar {
display: none; /* For Chrome, Safari and Opera */
}
.docspane-section {
margin-bottom: 10px;
max-height: 500px;
border: 1px solid rgba(255, 255, 255, 0.05);
border-radius: 10px;
background: rgba(35, 35, 45, 1);
overflow-y: auto;
scrollbar-width: none; /* For Firefox */
&::-webkit-scrollbar {
display: none; /* For Chrome, Safari and Opera */
}
&-header {
display: flex;
align-items: center;
cursor: pointer;
padding: 10px;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
&:hover {
background: rgba(45, 45, 55, 1);
}
}
&-title {
font-size: 16px;
font-weight: 500;
margin-left: 10px;
}
&-description {
padding: 10px;
color: rgba(255, 255, 255, 0.7);
font-size: 14px;
}
&-content {
padding: 0 10px;
background: rgba(45, 45, 55, 1);
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out, padding 0.2s ease-out;
&.expanded {
max-height: 500px;
padding: 10px;
overflow-y: auto;
scrollbar-width: none; /* For Firefox */
&::-webkit-scrollbar {
display: none; /* For Chrome, Safari and Opera */
}
}
}
}
.docspane-item {
margin-bottom: 10px;
padding: 10px;
border-radius: 6px;
background: rgba(35, 35, 45, 1);
&-name {
font-weight: 500;
margin-bottom: 4px;
}
&-type {
font-size: 12px;
color: rgba(255, 255, 255, 0.5);
margin-bottom: 4px;
}
&-description {
font-size: 14px;
color: rgba(255, 255, 255, 0.7);
}
}
`}
>
{!filteredData || Object.keys(filteredData).length === 0 ? (
<div className='docspane-empty' css={css`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
color: rgba(255, 255, 255, 0.5);
`}>
<span>No results found</span>
</div>
) : (
Object.entries(filteredData).map(([key, item]) => (
<div key={key} className='docspane-section'>
<div className='docspane-section-header' onClick={() => toggleSection(key)}>
<ArrowRightIcon
size={16}
css={css`
transform: ${expandedSections[key] ? 'rotate(90deg)' : 'rotate(0deg)'};
transition: transform 0.2s;
`}
/>
<div className='docspane-section-title'>{item.title}</div>
</div>
<div className='docspane-section-description'>{item.description}</div>
<div className={`docspane-section-content ${expandedSections[key] ? 'expanded' : ''}`}>
{item.properties && Object.entries(item.properties).map(([propKey, prop]) => (
<div key={propKey} className='docspane-item'>
<div className='docspane-item-name'>{propKey}</div>
<div className='docspane-item-type'>{prop.type}</div>
<div className='docspane-item-description'>{prop.description}</div>
</div>
))}
{item.methods && Object.entries(item.methods).map(([methodKey, method]) => (
<div key={methodKey} className='docspane-item'>
<div className='docspane-item-name'>{methodKey}</div>
<div className='docspane-item-description'>{method.description}</div>
</div>
))}
</div>
</div>
))
)}
</div>
)
}