tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
only use server tz on initial page load
awarm.space
4 months ago
08a4317c
142b1941
+11
-3
1 changed file
expand all
collapse all
unified
split
src
hooks
useLocalizedDate.ts
+11
-3
src/hooks/useLocalizedDate.ts
···
2
2
import { useContext, useMemo } from "react";
3
3
import { DateTime } from "luxon";
4
4
import { RequestHeadersContext } from "components/Providers/RequestHeadersProvider";
5
5
+
import { useInitialPageLoad } from "components/InitialPageLoadProvider";
5
6
6
7
/**
7
8
* Hook that formats a date string using Luxon with timezone and locale from request headers.
9
9
+
* On initial page load, uses the timezone from request headers. After hydration, uses the system timezone.
8
10
*
9
11
* @param dateString - ISO date string to format
10
12
* @param options - Intl.DateTimeFormatOptions for formatting
···
18
20
options?: Intl.DateTimeFormatOptions,
19
21
): string {
20
22
const { timezone, language } = useContext(RequestHeadersContext);
23
23
+
const isInitialPageLoad = useInitialPageLoad();
21
24
22
25
return useMemo(() => {
23
26
// Parse the date string to Luxon DateTime
24
27
let dateTime = DateTime.fromISO(dateString);
25
28
29
29
+
// On initial page load, use header timezone. After hydration, use system timezone
30
30
+
const effectiveTimezone = isInitialPageLoad
31
31
+
? timezone
32
32
+
: Intl.DateTimeFormat().resolvedOptions().timeZone;
33
33
+
26
34
// Apply timezone if available
27
27
-
if (timezone) {
28
28
-
dateTime = dateTime.setZone(timezone);
35
35
+
if (effectiveTimezone) {
36
36
+
dateTime = dateTime.setZone(effectiveTimezone);
29
37
}
30
38
31
39
// Parse locale from accept-language header (take first locale)
···
33
41
const locale = language?.split(",")[0]?.split(";")[0]?.trim() || "en-US";
34
42
35
43
return dateTime.toLocaleString(options, { locale });
36
36
-
}, [dateString, options, timezone, language]);
44
44
+
}, [dateString, options, timezone, language, isInitialPageLoad]);
37
45
}