Skip to content

Commit 33ed851

Browse files
committed
refactor: duration badge
1 parent a079c9f commit 33ed851

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import DateManager from "@/utilities/DateManager";
22
import classNames from "classnames";
33
import { useRouter } from "next/router";
4-
import React, { useMemo } from "react";
4+
import React, { useCallback, useMemo } from "react";
55

66
interface DurationBadgeProps {
77
value: number;
@@ -10,22 +10,21 @@ interface DurationBadgeProps {
1010

1111
export default function DurationBadge({ value, type = "gray" }: DurationBadgeProps) {
1212
const router = useRouter();
13+
const getDuration = useCallback((value: number, locale: string) => {
14+
if (!value) return 0;
15+
return DateManager.humanize(value, locale);
16+
}, []);
1317
const duration = useMemo(() => {
14-
return (value: number) => {
15-
if (!value) {
16-
return 0;
17-
}
18-
return DateManager.humanize(value, router.locale as string);
19-
};
20-
}, [router.locale]);
18+
return getDuration(value, router.locale as string);
19+
}, [router.locale, value, getDuration]);
2120
return (
2221
<div
2322
className={classNames("text-xxs uppercase font-semibold px-2 rounded-3xl inline-block text-gray-500 text-nowrap", {
2423
"bg-gray-200": type === "gray",
2524
"border border-gray-200": type === "bordered",
2625
})}
2726
>
28-
{duration(value)}
27+
{duration}
2928
</div>
3029
);
3130
}

0 commit comments

Comments
 (0)