@@ -13,10 +13,12 @@ export const useSingleLineData = (lineRef?: number, routeIds?: number[]) => {
13
13
const [ filteredPositions , setFilteredPositions ] = useState < Point [ ] > ( [ ] )
14
14
const [ startTime , setStartTime ] = useState < string > ( '00:00:00' )
15
15
const [ plannedRouteStops , setPlannedRouteStops ] = useState < BusStop [ ] > ( [ ] )
16
-
16
+ const today = new Date ( timestamp )
17
+ const tomorrow = new Date ( today )
18
+ tomorrow . setDate ( tomorrow . getDate ( ) + 1 )
17
19
const { locations, isLoading : locationsAreLoading } = useVehicleLocations ( {
18
- from : + new Date ( timestamp ) . setHours ( 0 , 0 , 0 , 0 ) ,
19
- to : + new Date ( timestamp ) . setHours ( 23 , 59 , 59 , 999 ) ,
20
+ from : + today . setHours ( 0 , 0 , 0 , 0 ) ,
21
+ to : + tomorrow . setHours ( 0 , 0 , 0 , 0 ) ,
20
22
lineRef,
21
23
splitMinutes : 360 ,
22
24
pause : ! lineRef ,
@@ -35,17 +37,45 @@ export const useSingleLineData = (lineRef?: number, routeIds?: number[]) => {
35
37
return pos
36
38
} , [ locations ] )
37
39
40
+ function convertTo24HourAndToNumber ( time : string ) : number {
41
+ const match = time . match ( / ( \d + ) : ( \d + ) : ( \d + ) \s ( A M | P M ) / )
42
+ if ( ! match ) return 0
43
+
44
+ const [ , hour , minute , , modifier ] = match
45
+ let newHour = parseInt ( hour , 10 )
46
+ if ( modifier === 'AM' && newHour === 12 ) newHour = 0
47
+ if ( modifier === 'PM' && newHour !== 12 ) newHour += 12
48
+
49
+ return newHour * 60 + parseInt ( minute , 10 )
50
+ }
51
+
38
52
const options = useMemo ( ( ) => {
39
- const options = positions
40
- . map ( ( position ) => position . point ?. siri_ride__scheduled_start_time ) // get all start times
41
- . filter ( ( time , i , arr ) => arr . indexOf ( time ) === i ) // unique
42
- . map ( ( time ) => new Date ( time ?? 0 ) . toLocaleTimeString ( ) ) // convert to strings
53
+ const filteredPositions = positions . filter ( ( position ) => {
54
+ const startTime = position . point ?. siri_ride__scheduled_start_time
55
+ return ! ! startTime && + new Date ( startTime ) > + today . setHours ( 0 , 0 , 0 , 0 )
56
+ } )
57
+
58
+ if ( filteredPositions . length === 0 ) return [ ]
59
+
60
+ const uniqueTimes = Array . from (
61
+ new Set (
62
+ filteredPositions
63
+ . map ( ( position ) => position . point ?. siri_ride__scheduled_start_time )
64
+ . filter ( ( time ) : time is string => ! ! time )
65
+ . map ( ( time ) => time . trim ( ) ) ,
66
+ ) ,
67
+ )
68
+ . map ( ( time ) => new Date ( time ) . toLocaleTimeString ( ) ) // Convert to 24-hour time string
43
69
. map ( ( time ) => ( {
44
- // convert to options
45
70
value : time ,
46
71
label : time ,
47
72
} ) )
48
- return options
73
+
74
+ const sortedOptions = uniqueTimes . sort (
75
+ ( a , b ) => convertTo24HourAndToNumber ( a . value ) - convertTo24HourAndToNumber ( b . value ) ,
76
+ )
77
+
78
+ return sortedOptions
49
79
} , [ positions ] )
50
80
51
81
useEffect ( ( ) => {
0 commit comments