Skip to content

Commit 53b486e

Browse files
authored
cleanup: use clearer types in RouteUploadButtons (commaai#339)
1 parent 84e56c0 commit 53b486e

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

src/api/upload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const FileTypes = {
1010
ecameras: ['ecamera.hevc'],
1111
}
1212

13-
type FileType = keyof typeof FileTypes
13+
export type FileType = keyof typeof FileTypes
1414

1515
const getFiles = async (routeName: string, types?: FileType[]) => {
1616
const files = await getAlreadyUploadedFiles(routeName)

src/components/RouteUploadButtons.tsx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ import clsx from 'clsx'
44

55
import Icon, { type IconName } from '~/components/material/Icon'
66
import Button from './material/Button'
7-
import { FileTypes, uploadAllSegments } from '~/api/upload'
7+
import { uploadAllSegments, type FileType } from '~/api/upload'
88
import type { Route } from '~/types'
99

10+
type ButtonType = 'road' | 'driver' | 'logs' | 'route'
11+
type ButtonState = 'idle' | 'loading' | 'success' | 'error'
12+
13+
const BUTTON_TO_FILE_TYPES: Record<ButtonType, FileType[] | undefined> = {
14+
road: ['cameras', 'ecameras'],
15+
driver: ['dcameras'],
16+
logs: ['logs'],
17+
route: undefined,
18+
}
19+
1020
interface UploadButtonProps {
11-
state: 'idle' | 'loading' | 'success' | 'error'
21+
state: ButtonState
1222
onClick?: () => void
1323
icon: IconName
1424
text: string
@@ -21,10 +31,7 @@ const UploadButton: VoidComponent<UploadButtonProps> = (props) => {
2131

2232
const handleUpload = () => {
2333
if (disabled()) return
24-
25-
if (props.onClick) {
26-
props.onClick()
27-
}
34+
props.onClick?.()
2835
}
2936

3037
const stateToIcon: Record<Exclude<UploadButtonProps['state'], null | undefined>, IconName> = {
@@ -47,37 +54,28 @@ const UploadButton: VoidComponent<UploadButtonProps> = (props) => {
4754
)
4855
}
4956

50-
type ButtonType = 'cameras' | 'driver' | 'logs' | 'route'
51-
5257
interface RouteUploadButtonsProps {
5358
route?: Route
5459
}
5560

5661
const RouteUploadButtons: VoidComponent<RouteUploadButtonsProps> = (props) => {
5762
const [uploadStore, setUploadStore] = createStore({
5863
states: {
59-
cameras: 'idle',
64+
road: 'idle',
6065
driver: 'idle',
6166
logs: 'idle',
6267
route: 'idle',
63-
} as Record<ButtonType, 'idle' | 'loading' | 'success' | 'error'>,
68+
} as Record<ButtonType, ButtonState>,
6469
})
6570

66-
const updateButtonStates = (types: ButtonType[], state: 'loading' | 'success' | 'error') => {
71+
const updateButtonStates = (types: ButtonType[], state: ButtonState) => {
6772
batch(() => {
6873
for (const type of types) {
6974
setUploadStore('states', type, state)
7075
}
7176
})
7277
}
7378

74-
const buttonToFileTypeMap: Record<ButtonType, (keyof typeof FileTypes)[] | undefined> = {
75-
cameras: ['cameras', 'ecameras'],
76-
driver: ['dcameras'],
77-
logs: ['logs'],
78-
route: undefined,
79-
}
80-
8179
const handleUpload = async (type: ButtonType) => {
8280
if (!props.route) return
8381

@@ -87,7 +85,7 @@ const RouteUploadButtons: VoidComponent<RouteUploadButtonsProps> = (props) => {
8785
.map(([type]) => type as ButtonType)
8886
.filter((type) => type !== undefined)
8987

90-
const typesToUpload = typesNotUploadedYet.flatMap((type) => buttonToFileTypeMap[type]).filter((type) => type !== undefined)
88+
const typesToUpload = typesNotUploadedYet.flatMap((type) => BUTTON_TO_FILE_TYPES[type]).filter((type) => type !== undefined)
9189

9290
updateButtonStates(typesNotUploadedYet, 'loading')
9391

@@ -103,7 +101,7 @@ const RouteUploadButtons: VoidComponent<RouteUploadButtonsProps> = (props) => {
103101

104102
setUploadStore('states', type, 'loading')
105103

106-
const fileTypesToUpload = buttonToFileTypeMap[type]
104+
const fileTypesToUpload = BUTTON_TO_FILE_TYPES[type]
107105

108106
try {
109107
await uploadAllSegments(props.route.fullname, props.route.maxqlog + 1, fileTypesToUpload)
@@ -117,7 +115,7 @@ const RouteUploadButtons: VoidComponent<RouteUploadButtonsProps> = (props) => {
117115
return (
118116
<div class="flex flex-col rounded-b-md m-5">
119117
<div class="grid grid-cols-2 gap-3 w-full lg:grid-cols-4">
120-
<UploadButton text="Road" icon="videocam" state={uploadStore.states.cameras} onClick={() => handleUpload('cameras')} />
118+
<UploadButton text="Road" icon="videocam" state={uploadStore.states.road} onClick={() => handleUpload('road')} />
121119
<UploadButton text="Driver" icon="person" state={uploadStore.states.driver} onClick={() => handleUpload('driver')} />
122120
<UploadButton text="Logs" icon="description" state={uploadStore.states.logs} onClick={() => handleUpload('logs')} />
123121
<UploadButton text="All" icon="upload" state={uploadStore.states.route} onClick={() => handleUpload('route')} />

0 commit comments

Comments
 (0)