-
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathApplicationDataBrowser.tsx
More file actions
209 lines (189 loc) · 6.32 KB
/
Copy pathApplicationDataBrowser.tsx
File metadata and controls
209 lines (189 loc) · 6.32 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
import React, { useState, useCallback } from 'react'
import { JSONTree } from 'react-json-tree'
import Button from 'react-bootstrap/Button'
import Card from 'react-bootstrap/Card'
import Col from 'react-bootstrap/Col'
import Form from 'react-bootstrap/Form'
import Row from 'react-bootstrap/Row'
import Spinner from 'react-bootstrap/Spinner'
import { useLoginStatus } from '../../store'
interface VersionData {
version: string
data: unknown
}
interface AppDataResult {
global: VersionData[]
user: VersionData[]
}
const expandAll = () => true
const expandRoot = (
_keyPath: readonly (string | number)[],
_data: unknown,
level: number
) => level < 1
const ApplicationDataBrowser: React.FC = () => {
const [appId, setAppId] = useState('')
const [expanded, setExpanded] = useState(false)
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const [result, setResult] = useState<AppDataResult | null>(null)
const [expandAllNodes, setExpandAllNodes] = useState(false)
const loginStatus = useLoginStatus()
const isLoggedIn = loginStatus.status === 'loggedIn'
const fetchData = useCallback(async () => {
const trimmed = appId.trim()
if (!trimmed) return
setLoading(true)
setError(null)
setResult(null)
try {
const fetched: AppDataResult = { global: [], user: [] }
// Fetch global versions
const globalVersionsRes = await fetch(
`/signalk/v1/applicationData/global/${encodeURIComponent(trimmed)}`,
{ credentials: 'include' }
)
if (globalVersionsRes.ok) {
const versions: string[] = await globalVersionsRes.json()
for (const version of versions) {
const dataRes = await fetch(
`/signalk/v1/applicationData/global/${encodeURIComponent(trimmed)}/${encodeURIComponent(version)}`,
{ credentials: 'include' }
)
if (dataRes.ok) {
fetched.global.push({ version, data: await dataRes.json() })
}
}
}
// Fetch user versions (only if logged in)
if (isLoggedIn) {
const userVersionsRes = await fetch(
`/signalk/v1/applicationData/user/${encodeURIComponent(trimmed)}`,
{ credentials: 'include' }
)
if (userVersionsRes.ok) {
const versions: string[] = await userVersionsRes.json()
for (const version of versions) {
const dataRes = await fetch(
`/signalk/v1/applicationData/user/${encodeURIComponent(trimmed)}/${encodeURIComponent(version)}`,
{ credentials: 'include' }
)
if (dataRes.ok) {
fetched.user.push({ version, data: await dataRes.json() })
}
}
}
}
setResult(fetched)
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to fetch')
} finally {
setLoading(false)
}
}, [appId, isLoggedIn])
const handleSubmit = useCallback(
(e: React.FormEvent) => {
e.preventDefault()
fetchData()
},
[fetchData]
)
const hasGlobal = result && result.global.length > 0
const hasUser = result && result.user.length > 0
const hasNoData = result && !hasGlobal && !hasUser
return (
<Card>
<Card.Header
style={{ cursor: 'pointer', userSelect: 'none' }}
onClick={() => setExpanded((prev) => !prev)}
>
Application Data {expanded ? '[-]' : '[+]'}
</Card.Header>
{expanded && (
<Card.Body>
<Form onSubmit={handleSubmit}>
<Form.Group as={Row} className="mb-3">
<Col xs="12" md="6">
<Form.Control
type="text"
placeholder="Enter application ID (e.g. unitpreferences)"
value={appId}
onChange={(e) => setAppId(e.target.value)}
autoComplete="off"
/>
</Col>
<Col xs="12" md="2">
<Button
variant="primary"
type="submit"
disabled={loading || !appId.trim()}
>
{loading ? <Spinner animation="border" size="sm" /> : 'Fetch'}
</Button>
</Col>
</Form.Group>
</Form>
{(hasGlobal || hasUser) && (
<Form.Check
type="checkbox"
id="appdata-expand-all"
label="Expand all"
checked={expandAllNodes}
onChange={(e) => setExpandAllNodes(e.target.checked)}
className="mb-2"
/>
)}
{error && <div className="text-danger mb-2">{error}</div>}
{hasNoData && (
<div className="text-muted">
No data found for "{appId.trim()}"
</div>
)}
{hasGlobal && (
<div className="mb-3">
<h6>Global</h6>
{result.global.map(({ version, data }) => (
<div key={version} className="mb-2">
<strong>{version}</strong>
<JSONTree
key={`global-${version}-${expandAllNodes}`}
data={data}
theme="default"
invertTheme={true}
sortObjectKeys
hideRoot
shouldExpandNodeInitially={
expandAllNodes ? expandAll : expandRoot
}
/>
</div>
))}
</div>
)}
{hasUser && (
<div>
<h6>User ({loginStatus.username})</h6>
{result.user.map(({ version, data }) => (
<div key={version} className="mb-2">
<strong>{version}</strong>
<JSONTree
key={`user-${version}-${expandAllNodes}`}
data={data}
theme="default"
invertTheme={true}
sortObjectKeys
hideRoot
shouldExpandNodeInitially={
expandAllNodes ? expandAll : expandRoot
}
/>
</div>
))}
</div>
)}
</Card.Body>
)}
</Card>
)
}
export default ApplicationDataBrowser