@@ -4,11 +4,21 @@ import clsx from 'clsx'
44
55import Icon , { type IconName } from '~/components/material/Icon'
66import Button from './material/Button'
7- import { FileTypes , uploadAllSegments } from '~/api/upload'
7+ import { uploadAllSegments , type FileType } from '~/api/upload'
88import 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+
1020interface 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-
5257interface RouteUploadButtonsProps {
5358 route ?: Route
5459}
5560
5661const 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