28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef } from "react";
|
|
import { useLocale } from "@/components/I18n";
|
|
import { intlLocale } from "@peppa/shared/i18n";
|
|
|
|
/** HH:MM (with the date when not today) in the viewer's time zone; the server renders its own zone first. */
|
|
export function LocalTime({ iso }: { iso: string }) {
|
|
const d = new Date(iso);
|
|
const loc = intlLocale(useLocale());
|
|
const today = new Date().toDateString() === d.toDateString();
|
|
const time = d.toLocaleTimeString(loc, { hour: "2-digit", minute: "2-digit" });
|
|
return (
|
|
<time dateTime={iso} suppressHydrationWarning>
|
|
{today ? time : `${d.toLocaleDateString(loc, { day: "2-digit", month: "2-digit" })} ${time}`}
|
|
</time>
|
|
);
|
|
}
|
|
|
|
/** On narrow screens the side panel sits under the list: bring the chosen runner into view. */
|
|
export function ScrollIntoViewOnNarrow({ id }: { id: string | number }) {
|
|
const ref = useRef<HTMLSpanElement>(null);
|
|
useEffect(() => {
|
|
if (window.matchMedia("(max-width: 1279px)").matches) ref.current?.scrollIntoView({ block: "start" });
|
|
}, [id]);
|
|
return <span ref={ref} className="block scroll-mt-16" />;
|
|
}
|