-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Dates): Set zone for dates coming from the backend as UTC by defa…
…ult & load the user tz in the browser
- Loading branch information
Showing
4 changed files
with
26 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,40 @@ | ||
import clsx from "clsx"; | ||
import useRelativeTime from "core/hooks/useRelativeTime"; | ||
import { DateTime, DateTimeOptions } from "luxon"; | ||
import { memo, useMemo } from "react"; | ||
import { useMemo } from "react"; | ||
|
||
type Props = { | ||
datetime: string; | ||
zone?: string; | ||
className?: string; | ||
relative?: boolean; | ||
format?: DateTimeOptions; | ||
}; | ||
|
||
const Time = (props: Props) => { | ||
const datetime = useMemo( | ||
() => DateTime.fromISO(props.datetime), | ||
// By default, all dates from the backend are in UTC | ||
() => DateTime.fromISO(props.datetime).toLocal(), | ||
[props.datetime], | ||
); | ||
|
||
const relativeDate = useRelativeTime(datetime); | ||
|
||
const value = useMemo(() => { | ||
if (props.relative) { | ||
return relativeDate; | ||
} else { | ||
return datetime.toLocaleString(props.format ?? DateTime.DATETIME_SHORT); | ||
} | ||
// We use the toggle variable to force React to recompute this value | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [datetime, props.format, props.relative, relativeDate]); | ||
|
||
if (!datetime?.isValid) return null; | ||
|
||
const isoDate = datetime.toISO(); | ||
return ( | ||
<time | ||
suppressHydrationWarning={true} | ||
title={datetime.toISO() ?? undefined} | ||
dateTime={datetime.toISO() ?? undefined} | ||
title={isoDate ?? undefined} | ||
dateTime={isoDate ?? undefined} | ||
className={clsx("whitespace-nowrap", props.className)} | ||
> | ||
{value} | ||
{props.relative | ||
? relativeDate | ||
: datetime.toLocaleString(props.format ?? DateTime.DATETIME_SHORT)} | ||
</time> | ||
); | ||
}; | ||
|
||
export default memo(Time); | ||
export default Time; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters