Skip to content

Commit f1638ab

Browse files
committed
Made it safer for SSR
1 parent d9498b4 commit f1638ab

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

apps/webapp/app/components/primitives/DateTime.tsx

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,14 @@ export const DateTime = ({
2222
showTimezone = false,
2323
}: DateTimeProps) => {
2424
const locales = useLocales();
25-
const realDate = typeof date === "string" ? new Date(date) : date;
26-
const resolvedOptions = Intl.DateTimeFormat().resolvedOptions();
27-
const localTimeZone = resolvedOptions.timeZone;
28-
29-
const initialFormattedDateTime = formatDateTime(
30-
realDate,
31-
timeZone ?? "UTC",
32-
locales,
33-
includeSeconds,
34-
includeTime
35-
);
25+
const [localTimeZone, setLocalTimeZone] = useState<string>("UTC");
3626

37-
const [formattedDateTime, setFormattedDateTime] = useState<string>(initialFormattedDateTime);
27+
const realDate = typeof date === "string" ? new Date(date) : date;
3828

3929
useEffect(() => {
40-
setFormattedDateTime(
41-
formatDateTime(
42-
realDate,
43-
timeZone ?? resolvedOptions.timeZone,
44-
locales,
45-
includeSeconds,
46-
includeTime
47-
)
48-
);
49-
}, [locales, includeSeconds, realDate]);
30+
const resolvedOptions = Intl.DateTimeFormat().resolvedOptions();
31+
setLocalTimeZone(resolvedOptions.timeZone);
32+
}, []);
5033

5134
const tooltipContent = (
5235
<div className="flex flex-col gap-1">
@@ -89,7 +72,13 @@ export const DateTime = ({
8972
<SimpleTooltip
9073
button={
9174
<Fragment>
92-
{formattedDateTime.replace(/\s/g, String.fromCharCode(32))}
75+
{formatDateTime(
76+
realDate,
77+
timeZone ?? localTimeZone,
78+
locales,
79+
includeSeconds,
80+
includeTime
81+
).replace(/\s/g, String.fromCharCode(32))}
9382
{showTimezone ? ` (${timeZone ?? "UTC"})` : null}
9483
</Fragment>
9584
}

0 commit comments

Comments
 (0)