Skip to content

Commit

Permalink
fix: show relative time correctly (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmouk authored Jun 19, 2024
1 parent 3d22420 commit 3a35bb4
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions apps/web/src/app/[locale]/_components/TimeEntryTreeItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { differenceInDays, formatISO, isSameYear } from "date-fns";
import { useFormatter } from "next-intl";
import { differenceInDays, formatISO, isSameYear, startOfDay } from "date-fns";
import { useFormatter, useLocale } from "next-intl";
import { useMemo } from "react";
import { PiTimer } from "react-icons/pi";
import { Duration } from "../_components/Duration";
Expand All @@ -13,6 +13,10 @@ type Props = {
now: Date;
};

const capitalize = (text: string) => {
return `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
};

export const TimeEntryTreeItem = ({
depth,
color,
Expand All @@ -21,15 +25,23 @@ export const TimeEntryTreeItem = ({
now,
}: Props) => {
const formatDate = useFormatter();
const locale = useLocale();

const readableDate = useMemo(() => {
return differenceInDays(now, startedAt) < 4
? formatDate.relativeTime(startedAt, { now, unit: "day" })
? capitalize(
new Intl.RelativeTimeFormat(locale, {
numeric: "auto",
}).format(
differenceInDays(startOfDay(now), startOfDay(startedAt)),
"day",
),
)
: formatDate.dateTime(
startedAt,
isSameYear(startedAt, now) ? "short" : "long",
);
}, [formatDate, startedAt, now]);
}, [formatDate, startedAt, locale, now]);

const handleClick = () => {
/** TODO */
Expand Down

0 comments on commit 3a35bb4

Please sign in to comment.