Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: time related issues in the "map by line" page #936

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions src/hooks/useSingleLineData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export const useSingleLineData = (lineRef?: number, routeIds?: number[]) => {
const [filteredPositions, setFilteredPositions] = useState<Point[]>([])
const [startTime, setStartTime] = useState<string>('00:00:00')
const [plannedRouteStops, setPlannedRouteStops] = useState<BusStop[]>([])

const today = new Date(timestamp)
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
const { locations, isLoading: locationsAreLoading } = useVehicleLocations({
from: +new Date(timestamp).setHours(0, 0, 0, 0),
to: +new Date(timestamp).setHours(23, 59, 59, 999),
from: +today.setHours(0, 0, 0, 0),
to: +tomorrow.setHours(0, 0, 0, 0),
lineRef,
splitMinutes: 60,
pause: !lineRef,
Expand All @@ -35,17 +37,45 @@ export const useSingleLineData = (lineRef?: number, routeIds?: number[]) => {
return pos
}, [locations])

function convertTo24HourAndToNumber(time: string): number {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name does not reflect that the output of this method is the number of minutes since midnight. Would you consider renaming this method?
This is my suggestion, but feel free to implement anything you like.

Suggested change
function convertTo24HourAndToNumber(time: string): number {
function timestringToMinutes(time: string): number {

const match = time.match(/(\d+):(\d+):(\d+)\s(AM|PM)/)
if (!match) return 0

const [, hour, minute, , modifier] = match
let newHour = parseInt(hour, 10)
if (modifier === 'AM' && newHour === 12) newHour = 0
if (modifier === 'PM' && newHour !== 12) newHour += 12

return newHour * 60 + parseInt(minute, 10)
}

const options = useMemo(() => {
const options = positions
.map((position) => position.point?.siri_ride__scheduled_start_time) // get all start times
.filter((time, i, arr) => arr.indexOf(time) === i) // unique
.map((time) => new Date(time ?? 0).toLocaleTimeString()) // convert to strings
const filteredPositions = positions.filter((position) => {
const startTime = position.point?.siri_ride__scheduled_start_time
return !!startTime && +new Date(startTime) > +today.setHours(0, 0, 0, 0)
})

if (filteredPositions.length === 0) return []

const uniqueTimes = Array.from(
new Set(
filteredPositions
.map((position) => position.point?.siri_ride__scheduled_start_time)
.filter((time): time is string => !!time)
.map((time) => time.trim()),
),
)
.map((time) => new Date(time).toLocaleTimeString()) // Convert to 24-hour time string
.map((time) => ({
// convert to options
value: time,
label: time,
}))
return options

const sortedOptions = uniqueTimes.sort(
(a, b) => convertTo24HourAndToNumber(a.value) - convertTo24HourAndToNumber(b.value),
)

return sortedOptions
}, [positions])

useEffect(() => {
Expand Down
Loading