Skip to content

Commit bb47cbf

Browse files
committed
improve board time handling
1 parent 51dea7b commit bb47cbf

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/routes/departureboard/-components/BoardEntry.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ import { BoardPlatformInfo } from "@/routes/departureboard/-components/BoardPlat
44
import { BoardTimeInfo } from "@/routes/departureboard/-components/BoardTimeInfo.tsx";
55
import { BoardTrainInfo } from "@/routes/departureboard/-components/BoardTrainInfo.tsx";
66
import { BoardViaInfo } from "@/routes/departureboard/-components/BoardViaInfo.tsx";
7+
import type { DateTime } from "luxon";
78
import type { FC } from "react";
89

910
type BoardEntryProps = {
1011
isFreight: boolean;
1112
entry: BoardEntryDto;
1213
currentPoint: PointInfoDto;
13-
timeFormatter: (isoTime: string) => string;
14+
timeParser: (isoTime: string) => DateTime;
1415
};
1516

16-
export const BoardEntry: FC<BoardEntryProps> = ({ isFreight, entry, currentPoint, timeFormatter }) => {
17+
export const BoardEntry: FC<BoardEntryProps> = ({ isFreight, entry, currentPoint, timeParser }) => {
1718
return (
1819
<li className="even:bg-gray-100 overflow-hidden">
1920
<div className="p-4">
2021
<div className={"flex flex-col w-full"}>
2122
<div className="flex flex-row">
22-
<BoardTimeInfo time={entry.scheduledTime} timeFormatter={timeFormatter} />
23+
<BoardTimeInfo time={entry.scheduledTime} timeParser={timeParser} />
2324
<BoardTrainInfo isFreight={isFreight} via={entry.via} transport={entry.transport} />
2425
<BoardDestinationInfo currentPointName={currentPoint.name} via={entry.via} />
2526
<BoardPlatformInfo
@@ -28,11 +29,7 @@ export const BoardEntry: FC<BoardEntryProps> = ({ isFreight, entry, currentPoint
2829
/>
2930
</div>
3031
<div className="flex flex-row items-center mt-1">
31-
<BoardTimeInfo
32-
time={entry.realtimeTime}
33-
scheduledTime={entry.scheduledTime}
34-
timeFormatter={timeFormatter}
35-
/>
32+
<BoardTimeInfo time={entry.realtimeTime} scheduledTime={entry.scheduledTime} timeParser={timeParser} />
3633
<BoardViaInfo via={entry.via} />
3734
</div>
3835
</div>

src/routes/departureboard/-components/BoardEntryTable.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ export const BoardEntryTable: FC<BoardEntryTableProps> = ({
4848
}
4949

5050
// formats the given iso date/time in the server timezone using the optionally provided format
51-
const timeFormatter = (isoTime: string, format?: string) => {
51+
const timeParser = (isoTime: string) => {
5252
const serverTz = server.timezoneId === "Z" ? "utc" : server.timezoneId;
5353
const givenDt = DateTime.fromISO(isoTime);
5454
const dtZoned = givenDt.setZone(serverTz);
55-
const dt = dtZoned.isValid ? dtZoned : givenDt;
56-
return dt.toFormat(format ?? "HH:mm");
55+
return dtZoned.isValid ? dtZoned : givenDt;
5756
};
5857

5958
return (
@@ -68,7 +67,7 @@ export const BoardEntryTable: FC<BoardEntryTableProps> = ({
6867
})
6968
.filter(train => !onlyPassengerTrains || !train.isFreight)
7069
.map(train => (
71-
<BoardEntry key={train.entry.journeyId} {...train} currentPoint={point} timeFormatter={timeFormatter} />
70+
<BoardEntry key={train.entry.journeyId} {...train} currentPoint={point} timeParser={timeParser} />
7271
))}
7372
</ul>
7473
</div>

src/routes/departureboard/-components/BoardTimeInfo.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
import { cn } from "@/lib/utils.ts";
22
import type { ClassValue } from "clsx";
3+
import type { DateTime } from "luxon";
34
import type { FC } from "react";
45

56
type BoardTimeInfoProps = {
67
time: string;
78
scheduledTime?: string;
8-
timeFormatter: (isoTime: string) => string;
9+
timeParser: (isoTime: string) => DateTime;
910
};
1011

11-
export const BoardTimeInfo: FC<BoardTimeInfoProps> = ({ time, scheduledTime, timeFormatter }) => {
12-
const timeFormatted = timeFormatter(time);
13-
const scheduledTimeFormatted = scheduledTime && timeFormatter(scheduledTime);
14-
const timeColorClass = getTimeColorClass(timeFormatted, scheduledTimeFormatted);
12+
export const BoardTimeInfo: FC<BoardTimeInfoProps> = ({ time, scheduledTime, timeParser }) => {
13+
const dtTime = timeParser(time);
14+
const dtScheduled = scheduledTime ? timeParser(scheduledTime) : undefined;
15+
const timeColorClass = getTimeColorClass(dtTime, dtScheduled);
1516
return (
16-
<span className={cn("text-5xl font-bold", scheduledTime && "font-semibold", timeColorClass)}>{timeFormatted}</span>
17+
<span className={cn("text-5xl font-bold", scheduledTime && "font-semibold", timeColorClass)}>
18+
{dtTime.toFormat("HH:mm")}
19+
</span>
1720
);
1821
};
1922

2023
/**
21-
*
22-
* @param time
23-
* @param scheduledTime
24+
* Resolves the color to use for the given time information.
25+
* @param time the time to get the color for.
26+
* @param scheduledTime an optional scheduled time to compare against for color picking.
2427
*/
25-
const getTimeColorClass = (time: string, scheduledTime?: string): ClassValue => {
28+
const getTimeColorClass = (time: DateTime, scheduledTime?: DateTime): ClassValue => {
2629
if (!scheduledTime) {
2730
// if no realtime information is available, print the time black
2831
return "text-black";

0 commit comments

Comments
 (0)