forked from commaai/new-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceActivity.tsx
More file actions
186 lines (167 loc) · 6.69 KB
/
DeviceActivity.tsx
File metadata and controls
186 lines (167 loc) · 6.69 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
import clsx from 'clsx'
import { createResource, createSignal, For, Show, Suspense } from 'solid-js'
import type { VoidComponent } from 'solid-js'
import { getDevice, SHARED_DEVICE } from '~/api/devices'
import { ATHENA_URL } from '~/api/config'
import { getAccessToken } from '~/api/auth/client'
import Icon from '~/components/material/Icon'
import IconButton from '~/components/material/IconButton'
import DeviceLocation from '~/components/DeviceLocation'
import DeviceStatistics from '~/components/DeviceStatistics'
import { deviceIsOnline, getDeviceName } from '~/utils/device'
import RouteList from '../components/RouteList'
import UploadQueue from '~/components/UploadQueue'
type DeviceActivityProps = {
dongleId: string
}
interface SnapshotResponse {
result?: {
jpegFront?: string
jpegBack?: string
}
}
const DeviceActivity: VoidComponent<DeviceActivityProps> = (props) => {
// TODO: device should be passed in from DeviceList
const [device] = createResource(() => props.dongleId, getDevice)
// Resource as source of another resource blocks component initialization
const deviceName = () => (device.latest ? getDeviceName(device.latest) : '')
// TODO: remove this. if we're listing the routes for a device you should always be a user, this is for viewing public routes which are being removed
const isDeviceUser = () => (device.loading ? true : device.latest?.is_owner || device.latest?.alias !== SHARED_DEVICE)
const [queueVisible, setQueueVisible] = createSignal(false)
const [snapshot, setSnapshot] = createSignal<{
error: string | null
fetching: boolean
images: string[]
}>({
error: null,
fetching: false,
images: [],
})
const takeSnapshot = async () => {
setSnapshot({ error: null, fetching: true, images: [] })
try {
const payload = {
method: 'takeSnapshot',
jsonrpc: '2.0',
id: 0,
}
const response = await fetch(`${ATHENA_URL}/${props.dongleId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `JWT ${getAccessToken()}`,
},
body: JSON.stringify(payload),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const resp: SnapshotResponse = (await response.json()) as SnapshotResponse
const images = []
if (resp.result?.jpegFront) images.push(resp.result.jpegFront)
if (resp.result?.jpegBack) images.push(resp.result.jpegBack)
if (images.length > 0) {
setSnapshot({ error: null, fetching: false, images })
} else {
throw new Error('No images found.')
}
} catch (err) {
let error = (err as Error).message
if (error.includes('Device not registered')) {
error = 'Device offline'
}
setSnapshot({ error, fetching: false, images: [] })
}
}
const downloadSnapshot = (image: string, index: number) => {
const link = document.createElement('a')
link.href = `data:image/jpeg;base64,${image}`
link.download = `snapshot${index + 1}.jpg`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
const clearImage = (index: number) => {
const newImages = snapshot().images.filter((_, i) => i !== index)
setSnapshot({ ...snapshot(), images: newImages })
}
const clearError = () => {
setSnapshot({ ...snapshot(), error: null })
}
return (
<>
<div class="flex flex-col gap-4 px-4 pb-4">
<div class="h-min overflow-hidden rounded-lg bg-surface-container-low">
<Suspense fallback={<div class="h-[240px] skeleton-loader size-full" />}>
<DeviceLocation dongleId={props.dongleId} deviceName={deviceName()!} />
</Suspense>
<div class="flex items-center justify-between p-4">
<Suspense fallback={<div class="h-[32px] skeleton-loader size-full rounded-xs" />}>
<div class="inline-flex items-center gap-2">
<div
class={clsx(
'm-2 size-2 shrink-0 rounded-full',
device.latest && deviceIsOnline(device.latest) ? 'bg-green-400' : 'bg-gray-400',
)}
/>
{<div class="text-xl font-bold">{deviceName()}</div>}
</div>
</Suspense>
<div class="flex gap-4">
<IconButton name="camera" onClick={() => void takeSnapshot()} />
<IconButton name="settings" href={`/${props.dongleId}/settings`} />
</div>
</div>
<Show when={isDeviceUser()}>
<DeviceStatistics dongleId={props.dongleId} class="p-4" />
<Show when={queueVisible()}>
<UploadQueue dongleId={props.dongleId} />
</Show>
<button
class={clsx(
'flex w-full cursor-pointer justify-center rounded-b-lg bg-surface-container-lowest p-2',
queueVisible() && 'border-t-2 border-t-surface-container-low',
)}
onClick={() => setQueueVisible(!queueVisible())}
>
<p class="mr-2">Upload Queue</p>
<Icon class="text-zinc-500" name={queueVisible() ? 'keyboard_arrow_up' : 'keyboard_arrow_down'} />
</button>
</Show>
</div>
<div class="flex flex-col gap-2">
<For each={snapshot().images}>
{(image, index) => (
<div class="flex-1 overflow-hidden rounded-lg bg-surface-container-low">
<div class="relative p-4">
<img src={`data:image/jpeg;base64,${image}`} alt={`Device Snapshot ${index() + 1}`} />
<div class="absolute right-4 top-4 p-4">
<IconButton class="text-white" name="download" onClick={() => downloadSnapshot(image, index())} />
<IconButton class="text-white" name="clear" onClick={() => clearImage(index())} />
</div>
</div>
</div>
)}
</For>
{snapshot().fetching && (
<div class="flex-1 overflow-hidden rounded-lg bg-surface-container-low">
<div class="p-4">
<div>Loading snapshots...</div>
</div>
</div>
)}
{snapshot().error && (
<div class="flex-1 overflow-hidden rounded-lg bg-surface-container-low">
<div class="flex items-center p-4">
<IconButton class="text-white" name="clear" onClick={clearError} />
<span>Error: {snapshot().error}</span>
</div>
</div>
)}
</div>
<RouteList dongleId={props.dongleId} />
</div>
</>
)
}
export default DeviceActivity