Skip to content

Commit a7af143

Browse files
authored
Remove duplicate devices query when switching device (commaai#435)
I would like to merge this instead of commaai/connect#430, but we need to decide how to handle public routes.
1 parent 637f69a commit a7af143

File tree

6 files changed

+14
-20
lines changed

6 files changed

+14
-20
lines changed

src/App.browser.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ describe('Demo mode', () => {
2121
beforeEach(() => setAccessToken(Demo.ACCESS_TOKEN))
2222

2323
test('View dashboard', async () => {
24-
const { findByText } = renderApp('/')
25-
expect(await findByText('demo 3X')).toBeTruthy()
24+
const { findAllByText } = renderApp('/')
25+
expect(await findAllByText('demo 3X')).toHaveLength(2)
2626
})
2727

2828
test('View demo route', async () => {

src/pages/dashboard/Dashboard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const Dashboard: Component<RouteSectionProps> = () => {
136136
<Match when={urlState().dongleId} keyed>
137137
{(dongleId) => (
138138
<DashboardLayout
139-
paneOne={<DeviceActivity dongleId={dongleId} shared={!currentDevice()} />}
139+
paneOne={<DeviceActivity dongleId={dongleId} device={currentDevice()} />}
140140
paneTwo={
141141
<Switch
142142
fallback={
@@ -147,7 +147,7 @@ const Dashboard: Component<RouteSectionProps> = () => {
147147
}
148148
>
149149
<Match when={urlState().dateStr === 'settings' || urlState().dateStr === 'prime'}>
150-
<SettingsActivity dongleId={dongleId} shared={!currentDevice()} />
150+
<SettingsActivity dongleId={dongleId} />
151151
</Match>
152152
<Match when={urlState().dateStr}>
153153
{(dateStr) => <RouteActivity dongleId={dongleId} dateStr={dateStr()} startTime={urlState().startTime} />}

src/pages/dashboard/activities/DeviceActivity.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import clsx from 'clsx'
2-
import { createMemo, createResource, createSignal, For, Show, Suspense } from 'solid-js'
2+
import { createMemo, createSignal, For, Show, Suspense } from 'solid-js'
33
import type { VoidComponent } from 'solid-js'
44

5-
import { getDevice } from '~/api/devices'
65
import { ATHENA_URL } from '~/api/config'
76
import { getAccessToken } from '~/api/auth/client'
7+
import type { Device } from '~/api/types'
88

99
import { DrawerToggleButton, useDrawerContext } from '~/components/material/Drawer'
1010
import Icon from '~/components/material/Icon'
@@ -19,7 +19,7 @@ import UploadQueue from '~/components/UploadQueue'
1919

2020
type DeviceActivityProps = {
2121
dongleId: string
22-
shared: boolean
22+
device: Device | undefined
2323
}
2424

2525
interface SnapshotResponse {
@@ -30,9 +30,7 @@ interface SnapshotResponse {
3030
}
3131

3232
const DeviceActivity: VoidComponent<DeviceActivityProps> = (props) => {
33-
// TODO: device should be passed in from DeviceList
34-
const [device] = createResource(() => props.dongleId, getDevice)
35-
const deviceName = createMemo(() => getDeviceName(device(), props.shared))
33+
const deviceName = createMemo(() => getDeviceName(props.device))
3634
const [queueVisible, setQueueVisible] = createSignal(false)
3735
const [snapshot, setSnapshot] = createSignal<{
3836
error: string | null
@@ -125,15 +123,13 @@ const DeviceActivity: VoidComponent<DeviceActivityProps> = (props) => {
125123
<DeviceLocation dongleId={props.dongleId} deviceName={deviceName()!} />
126124
</Suspense>
127125
<div class="flex items-center justify-between p-4">
128-
<Suspense fallback={<div class="h-[32px] skeleton-loader size-full" />}>
129-
{<div class="text-xl font-bold">{deviceName()}</div>}
130-
</Suspense>
126+
{<div class="text-xl font-bold">{deviceName()}</div>}
131127
<div class="flex gap-4">
132128
<IconButton name="camera" onClick={() => void takeSnapshot()} />
133129
<IconButton name="settings" href={`/${props.dongleId}/settings`} />
134130
</div>
135131
</div>
136-
<Show when={!props.shared}>
132+
<Show when={props.device}>
137133
<DeviceStatistics dongleId={props.dongleId} class="p-4" />
138134
<Show when={queueVisible()}>
139135
<UploadQueue dongleId={props.dongleId} />

src/pages/dashboard/activities/SettingsActivity.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const formatCurrency = (amount: number) => `$${(amount / 100).toFixed(amount % 1
3434

3535
type PrimeActivityProps = {
3636
dongleId: string
37-
shared: boolean
3837
}
3938

4039
type PrimePlan = 'nodata' | 'data'
@@ -403,7 +402,7 @@ const PrimeManage: VoidComponent<{ dongleId: string }> = (props) => {
403402

404403
const SettingsActivity: VoidComponent<PrimeActivityProps> = (props) => {
405404
const [device] = createResource(() => props.dongleId, getDevice)
406-
const deviceName = createMemo(() => getDeviceName(device(), props.shared))
405+
const deviceName = createMemo(() => getDeviceName(device()))
407406

408407
const [unpair, unpairData] = useAction(async () => {
409408
const { success } = await unpairDevice(props.dongleId)

src/pages/dashboard/components/DeviceList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const DeviceList: VoidComponent<DeviceListProps> = (props) => {
3737
activeClass="before:bg-primary"
3838
>
3939
<ListItemContent
40-
headline={getDeviceName(device, false)}
40+
headline={getDeviceName(device)}
4141
subhead={<span class="font-mono text-label-sm lowercase">{device.dongle_id}</span>}
4242
/>
4343
</ListItem>

src/utils/device.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import type { Device } from '~/api/types'
22

33
const SHARED_DEVICE = 'Shared Device'
44

5-
export function getDeviceName(device: Device | undefined, shared: boolean) {
6-
if (shared) return SHARED_DEVICE
7-
if (!device) return ''
5+
export function getDeviceName(device: Device | undefined) {
6+
if (!device) return SHARED_DEVICE
87
if (device.alias) return device.alias
98
return `comma ${device.device_type}`
109
}

0 commit comments

Comments
 (0)