Skip to content

Commit

Permalink
useMemo to create numberFormater instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamitto committed May 3, 2024
1 parent 1098418 commit 2ae3f95
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/renderer/src/pages/game-details/hero/hero-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface HeroPanelProps {
getGame: () => void;
}

const MAX_MINUTES_TO_SHOW_IN_PLAYTIME = 120;

export function HeroPanel({
game,
gameDetails,
Expand Down Expand Up @@ -58,23 +60,25 @@ export function HeroPanel({
}
}, [game?.lastTimePlayed, formatDistance]);

const numberFormatter = useMemo(() => {
return new Intl.NumberFormat(i18n.language, {
maximumFractionDigits: 1,
});
}, [i18n]);

const formatPlayTime = () => {
const milliseconds = game?.playTimeInMilliseconds || 0;
const seconds = milliseconds / 1000;
const minutes = seconds / 60;

if (minutes < 120) {
if (minutes < MAX_MINUTES_TO_SHOW_IN_PLAYTIME) {
return t("amount_minutes", {
amount: minutes.toFixed(0),
});
}

const numberFormat = new Intl.NumberFormat(i18n.language, {
maximumFractionDigits: 1,
});

const hours = minutes / 60;
return t("amount_hours", { amount: numberFormat.format(hours) });
return t("amount_hours", { amount: numberFormatter.format(hours) });
};

const finalDownloadSize = useMemo(() => {
Expand Down

0 comments on commit 2ae3f95

Please sign in to comment.