🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
feat(weekdays): use local data and time formats
Andrii Andrushchyshyn467f584e11aa
diff --git a/client/src/app/[site]/main/components/sections/Weekdays.tsx b/client/src/app/[site]/main/components/sections/Weekdays.tsxindex cf1f18507..83f7e7334 100644--- a/client/src/app/[site]/main/components/sections/Weekdays.tsx+++ b/client/src/app/[site]/main/components/sections/Weekdays.tsx@@ -30,6 +30,13 @@ import {import { StatType, useStore } from "../../../../../lib/store";import { cn } from "../../../../../lib/utils";+import {+ shortDayNames,+ longDayNames,+ hourLabels,+ hourMinuteLabels+} from "../../../../../lib/dateTimeUtils";+export function Weekdays() {const { site, time } = useStore();const [metric, setMetric] = useState<StatType>("users");@@ -121,29 +128,6 @@ export function Weekdays() {return max;}, [heatmapData]);- // Days of the week for column headers and full day names- const days = ["Mon", "Tues", "Weds", "Thus", "Fri", "Sat", "Sun"];- const fullDayNames = [- "Monday",- "Tuesday",- "Wednesday",- "Thursday",- "Friday",- "Saturday",- "Sunday",- ];-- // Generate time labels for the row headers- const timeLabels = useMemo(() => {- return Array(24)- .fill(0)- .map((_, hour) => {- const isPM = hour >= 12;- const h = hour === 0 ? 12 : hour > 12 ? hour - 12 : hour;- return `${h} ${isPM ? "PM" : "AM"}`;- });- }, []);-// Get color intensity based on valueconst getColorIntensity = (value: number) => {if (maxValue === 0 || !isFinite(value) || isNaN(value))@@ -248,7 +232,7 @@ export function Weekdays() {key={hour}className="h-4 text-xs flex items-center justify-end pr-2 text-neutral-400">- {hour % 2 === 0 ? timeLabels[hour] : ""}+ {hour % 2 === 0 ? hourLabels[hour] : ""}</div>))}</div>@@ -256,7 +240,7 @@ export function Weekdays() {<div className="flex-1">{/* Day labels */}<div className="flex h-5">- {days.map((day, i) => (+ {shortDayNames.map((day, i) => (<divkey={i}className="flex-1 text-center text-xs text-neutral-400"@@ -297,7 +281,7 @@ export function Weekdays() {</TooltipTrigger><TooltipContent className="flex flex-col gap-1 p-2"><div className="font-medium text-sm">- {fullDayNames[day]} at {timeLabels[hour]}+ {longDayNames[day]} at {hourMinuteLabels[hour]}</div><div className="flex items-center gap-2"><span className="font-semibold">diff --git a/client/src/lib/dateTimeUtils.ts b/client/src/lib/dateTimeUtils.tsindex e1ae745b0..1e3e995a6 100644--- a/client/src/lib/dateTimeUtils.ts+++ b/client/src/lib/dateTimeUtils.ts@@ -1,6 +1,61 @@import { Duration, DateTime } from "luxon";+/**+ * Returns localized weekday names, starting from Monday.+ *+ * @param locale - The locale to use (e.g., "en-US", "uk-UA").+ * @param format - The weekday format: "narrow" | "short" | "long".+ * @returns An array of localized weekday names from Monday to Sunday.+ */+export function getLocalizedWeekdayNames(+ locale: string,+ format: "narrow" | "short" | "long" = "long"+): string[] {+ const formatter = new Intl.DateTimeFormat(locale, { weekday: format });++ const fallback: Record<"narrow" | "short" | "long", string[]> = {+ narrow: ["M", "T", "W", "T", "F", "S", "S"],+ short: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],+ long: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],+ };++ const today = new Date();+ const startDay = 1; // Monday++ return Array.from({ length: 7 }, (_, i) => {+ const day = startDay + i;+ const date = new Date(today);+ date.setDate(today.getDate() + ((day - today.getDay() + 7) % 7));++ const formattedParts = formatter.formatToParts(date);+ const weekdayPart = formattedParts.find((part) => part.type === "weekday");++ return weekdayPart?.value ?? fallback[format][i];+ });+}++/**+ * Returns a list of 24 localized hour labels (e.g., "0", "12 AM", "1 PM")+ * based on the given locale and formatting options.+ *+ * @param locale - The locale to use (e.g., "en-US", "uk-UA").+ * @param options - Intl.DateTimeFormat options (e.g., { hour: "numeric", hour12: true }).+ * @returns An array of 24 formatted hour strings.+ */+export function getLocalizedTimeLabels(+ locale: string,+ options: Intl.DateTimeFormatOptions+): string[] {+ const formatter = new Intl.DateTimeFormat(locale, options);++ return Array.from({ length: 24 }, (_, hour) => {+ const date = new Date();+ date.setHours(hour, 0, 0, 0); // hour:00:00.000+ return formatter.format(date);+ });+}+/*** Formats a duration given in minutes and seconds into a human-readable string.*@@ -56,3 +111,26 @@ export const userLocale = typeof navigator !== "undefined" ? navigator.language// Detect user 12h/24h preferenceconst resolved = new Intl.DateTimeFormat(undefined, { hour: "numeric" }).resolvedOptions();export const is24Hour = resolved.hourCycle === "h23" || resolved.hourCycle === "h24";++// localized full day names+// e.g., "Monday" "Tuesday" "Wednesday"+export const longDayNames:string[] = getLocalizedWeekdayNames(userLocale, "long");++// localized short day names+// e.g., "Mon" "Tue" "Wed"+export const shortDayNames:string[] = getLocalizedWeekdayNames(userLocale, "short");++// localized time (hour with minutes) labels+// e.g., "00" "01" "02"+export const hourLabels: string[] = getLocalizedTimeLabels(userLocale, {+ hour: "numeric",+ hour12: !is24Hour,+});++// localized time (hour with minutes) labels+// e.g., "00:00" "01:00" "02:00"+export const hourMinuteLabels: string[] = getLocalizedTimeLabels(userLocale, {+ hour: "numeric",+ minute: "2-digit",+ hour12: !is24Hour,+});