forked from commaai/new-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboard.tsx
More file actions
186 lines (172 loc) · 6.75 KB
/
Dashboard.tsx
File metadata and controls
186 lines (172 loc) · 6.75 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 { createMemo, createResource, lazy, Match, Show, Suspense, Switch } from 'solid-js'
import type { Component, JSXElement, VoidComponent } from 'solid-js'
import { Navigate, type RouteSectionProps, useLocation } from '@solidjs/router'
import clsx from 'clsx'
import { USERADMIN_URL } from '~/api/config'
import { getDevices } from '~/api/devices'
import { getProfile } from '~/api/profile'
import storage from '~/utils/storage'
import type { Device } from '~/api/types'
import Button from '~/components/material/Button'
import ButtonBase from '~/components/material/ButtonBase'
import Drawer, { DrawerToggleButton, useDrawerContext } from '~/components/material/Drawer'
import Icon from '~/components/material/Icon'
import IconButton from '~/components/material/IconButton'
import TopAppBar from '~/components/material/TopAppBar'
import DeviceList from './components/DeviceList'
import DeviceActivity from './activities/DeviceActivity'
import RouteActivity from './activities/RouteActivity'
import SettingsActivity from './activities/SettingsActivity'
const PairActivity = lazy(() => import('./activities/PairActivity'))
const DashboardDrawer: VoidComponent<{ devices: Device[] }> = (props) => {
const { modal, setOpen } = useDrawerContext()
const onClose = () => setOpen(false)
const [profile] = createResource(getProfile)
return (
<>
<TopAppBar
component="h1"
leading={
<Show when={modal()}>
<IconButton name="arrow_back" onClick={onClose} />
</Show>
}
>
Devices
</TopAppBar>
<DeviceList class="overflow-y-auto p-2" devices={props.devices} />
<div class="grow" />
<Button class="m-4" leading={<Icon name="add" />} href="/pair" onClick={onClose}>
Add new device
</Button>
<div class="m-4 mt-0">
<ButtonBase href={USERADMIN_URL}>
<Suspense fallback={<div class="min-h-16 rounded-md skeleton-loader" />}>
<div class="flex max-w-full items-center px-3 rounded-md outline outline-1 outline-outline-variant min-h-16">
<div class="shrink-0 size-10 inline-flex items-center justify-center rounded-full bg-primary-container text-on-primary-container">
<Icon name={!profile.loading && !profile.latest ? 'person_off' : 'person'} filled />
</div>
<Show
when={profile()}
fallback={
<>
<div class="mx-3">Not signed in</div>
<div class="grow" />
<IconButton name="login" href="/login" />
</>
}
>
<div class="min-w-0 mx-3">
<div class="truncate text-body-md text-on-surface">{profile()?.email}</div>
<div class="truncate text-label-sm text-on-surface-variant">{profile()?.user_id}</div>
</div>
<div class="grow" />
<IconButton name="logout" href="/logout" />
</Show>
</div>
</Suspense>
</ButtonBase>
</div>
</>
)
}
const DashboardLayout: Component<{
paneOne: JSXElement
paneTwo: JSXElement
paneTwoContent: boolean
}> = (props) => {
const { modal } = useDrawerContext()
return (
<div class="relative size-full overflow-hidden">
<div
class={clsx(
'mx-auto size-full max-w-[1600px] md:grid md:grid-cols-2 lg:gap-2',
// Flex layout for mobile with horizontal transition
'flex transition-transform duration-300 ease-in-out',
props.paneTwoContent ? '-translate-x-full md:translate-x-0' : 'translate-x-0',
)}
>
<div class="min-w-full overflow-y-scroll">
<TopAppBar
class="font-bold"
leading={
<Show when={!modal()} fallback={<DrawerToggleButton />}>
<img alt="" src="/images/comma-white.png" class="h-8" />
</Show>
}
>
connect
</TopAppBar>
{props.paneOne}
</div>
<div class="min-w-full overflow-y-scroll">{props.paneTwo}</div>
</div>
</div>
)
}
const Dashboard: Component<RouteSectionProps> = () => {
const location = useLocation()
const urlState = createMemo(() => {
const parts = location.pathname.split('/').slice(1).filter(Boolean)
const startTime = parts[2] ? Math.max(Number(parts[2]), 0) : 0
const endTime = parts[3] ? Math.max(Number(parts[3]), startTime + 1) : undefined
return {
dongleId: parts[0] as string | undefined,
dateStr: parts[1] as string | undefined,
startTime: startTime,
endTime: endTime,
}
})
const [devices] = createResource(getDevices, { initialValue: [] })
const [profile] = createResource(getProfile)
const getDefaultDongleId = () => {
// Do not redirect if dongle ID already selected
if (urlState().dongleId) return undefined
const lastSelectedDongleId = storage.getItem('lastSelectedDongleId')
if (devices()?.some((device) => device.dongle_id === lastSelectedDongleId)) return lastSelectedDongleId
return devices()?.[0]?.dongle_id
}
return (
<Drawer drawer={<DashboardDrawer devices={devices()} />}>
<Switch>
<Match when={urlState().dongleId === 'pair' || !!location.query.pair}>
<PairActivity />
</Match>
<Match when={urlState().dongleId} keyed>
{(dongleId) => (
<DashboardLayout
paneOne={<DeviceActivity dongleId={dongleId} />}
paneTwo={
<Switch
fallback={
<div class="hidden size-full flex-col items-center justify-center gap-4 md:flex">
<Icon name="search" size="48" />
<span class="text-title-md">Select a route to view</span>
</div>
}
>
<Match when={urlState().dateStr === 'settings' || urlState().dateStr === 'prime'}>
<SettingsActivity dongleId={dongleId} />
</Match>
<Match when={urlState().dateStr} keyed>
{(dateStr) => (
<RouteActivity dongleId={dongleId} dateStr={dateStr} startTime={urlState().startTime} endTime={urlState().endTime} />
)}
</Match>
</Switch>
}
paneTwoContent={!!urlState().dateStr}
/>
)}
</Match>
<Match when={!profile.loading && !profile.latest}>
<Navigate href="/login" />
</Match>
<Match when={getDefaultDongleId()} keyed>
{(defaultDongleId) => <Navigate href={`/${defaultDongleId}`} />}
</Match>
</Switch>
</Drawer>
)
}
export default Dashboard