import { Alert, Badge, Center, Group, ScrollArea, SimpleGrid, Stack, Text } from "@mantine/core";
import { IconAlertTriangle } from "@tabler/icons-react";
import dayjs from "dayjs";
import { clientApi } from "@homarr/api/client";
import { useScopedI18n } from "@homarr/translation/client";
import type { DynamicSelectOption } from "../_inputs/widget-dynamic-select-input";
import type { WidgetComponentProps } from "../definition";
export default function TimetableWidget({
options,
displayMode = "compact",
width,
itemId,
}: WidgetComponentProps<"timetable">) {
const t = useScopedI18n("widget.timetable");
if (!options.station) {
return
{t("noStation")};
}
return (
);
}
interface TimetableWidgetInnerProps {
station: DynamicSelectOption;
baseUrl: string;
itemId?: string;
displayMode: "compact" | "advanced";
width: number;
}
const TimetableWidgetInner = ({ station, baseUrl, itemId, displayMode, width }: TimetableWidgetInnerProps) => {
const { data: timetable, error } = clientApi.widget.timetable.getTimetable.useQuery({
baseUrl,
itemId,
stationId: station.value,
limit: displayMode === "advanced" ? 50 : 10,
});
const t = useScopedI18n("widget.timetable");
if (error && timetable === undefined) throw error;
const entries = timetable?.entries ?? [];
const staleTime = timetable ? dayjs(timetable.timestamp).format("HH:mm:ss") : undefined;
const staleWarning = error && staleTime ? t("warning.stale", { time: staleTime }) : undefined;
const compactStaleWarning = error && staleTime ? t("warning.staleCompact") : undefined;
return (
{t("title", { station: station.label })}
{compactStaleWarning && displayMode === "compact" && (
{compactStaleWarning}
)}
{staleWarning && displayMode === "advanced" && (
} p="xs">
)}
= 760 ? 2 : 1} spacing="xs">
{entries.map((entry) => (
))}
{displayMode === "advanced" && timetable && (
{dayjs(timetable.timestamp).format("HH:mm:ss")}
)}
);
};
interface TimetableEntryView {
timestamp: Date;
delay: number;
line: { name: string; color: string | null } | null;
location: string;
platform: { name: string; hasChanged: boolean } | null;
}
function DepartureRow({ entry }: { entry: TimetableEntryView }) {
return (
{entry.line && (
{entry.line.name}
)}
{dayjs(entry.timestamp).format("HH:mm")}{" "}
{entry.delay >= 1 && (
+{entry.delay}'
)}
{entry.location}
{entry.platform && (
{entry.platform.name}
)}
);
}