Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions messages/cs-CZ.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "",
"chartLastNDays": "",
"chartNow": "",
"chartToday": "",
"chartYesterday": "",
"chartDaysAgo": "",
"chartDaysAgoShort": "",
"chartOneWeekAgo": "",
"chartTwoWeeksAgo": "",
"chartWeeksAgoShort": "",
"chartOneMonthAgo": "",
"chartMonthsAgoShort": "",
"chartAvgPerDay": "",
"chartEstimatedSuffix": "",
"chartTooltipRank": "",
Expand Down
9 changes: 0 additions & 9 deletions messages/de-DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "Alle",
"chartLastNDays": "Letzte {days} Tage",
"chartNow": "Jetzt",
"chartToday": "Heute",
"chartYesterday": "Gestern",
"chartDaysAgo": "Vor {days} Tagen",
"chartDaysAgoShort": "",
"chartOneWeekAgo": "",
"chartTwoWeeksAgo": "",
"chartWeeksAgoShort": "",
"chartOneMonthAgo": "",
"chartMonthsAgoShort": "",
"chartAvgPerDay": "",
"chartEstimatedSuffix": "",
"chartTooltipRank": "",
Expand Down
9 changes: 0 additions & 9 deletions messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -595,15 +595,6 @@
"chartTimeAll": "All",
"chartLastNDays": "Last {days} Days",
"chartNow": "Now",
"chartToday": "Today",
"chartYesterday": "Yesterday",
"chartDaysAgo": "{days} days ago",
"chartDaysAgoShort": "{days}d ago",
"chartOneWeekAgo": "1 week ago",
"chartTwoWeeksAgo": "2 weeks ago",
"chartWeeksAgoShort": "{weeks}w ago",
"chartOneMonthAgo": "1 month ago",
"chartMonthsAgoShort": "{months}mo ago",
"chartAvgPerDay": "Avg/Day",
"chartEstimatedSuffix": " (estimated)",
"chartTooltipRank": "Rank: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/es-ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "Todo",
"chartLastNDays": "Últimos {days} Días",
"chartNow": "Ahora",
"chartToday": "Hoy",
"chartYesterday": "Ayer",
"chartDaysAgo": "Hace {days} días",
"chartDaysAgoShort": "Hace {days}d",
"chartOneWeekAgo": "Hace 1 semana",
"chartTwoWeeksAgo": "Hace 2 semanas",
"chartWeeksAgoShort": "Hace {weeks} sem.",
"chartOneMonthAgo": "Hace 1 mes",
"chartMonthsAgoShort": "Hace {months} meses",
"chartAvgPerDay": "Media diaria",
"chartEstimatedSuffix": " (estimado)",
"chartTooltipRank": "Rango: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "Tout",
"chartLastNDays": "{days} derniers jours",
"chartNow": "Actuel",
"chartToday": "Aujourd'hui",
"chartYesterday": "Hier",
"chartDaysAgo": "Il y a {days} jours",
"chartDaysAgoShort": "Il y a {days}j",
"chartOneWeekAgo": "Il y a 1 semaine",
"chartTwoWeeksAgo": "Il y a 2 semaines",
"chartWeeksAgoShort": "Il y a {weeks}sem",
"chartOneMonthAgo": "Il y a 1 mois",
"chartMonthsAgoShort": "Il y a {months}mo",
"chartAvgPerDay": "Moy./Jour",
"chartEstimatedSuffix": " (estimé)",
"chartTooltipRank": "Classement : #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/it-IT.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "Tutto",
"chartLastNDays": "Ultimi {days} giorni",
"chartNow": "Ora",
"chartToday": "Oggi",
"chartYesterday": "Ieri",
"chartDaysAgo": "{days} {days, plural, one {giorno} other {giorni}} fa",
"chartDaysAgoShort": "{days}gg fa",
"chartOneWeekAgo": "Una settimana fa",
"chartTwoWeeksAgo": "2 settimane fa",
"chartWeeksAgoShort": "{weeks}sett. fa",
"chartOneMonthAgo": "Un mese fa",
"chartMonthsAgoShort": "{months}m fa",
"chartAvgPerDay": "Media/Giorno",
"chartEstimatedSuffix": " (stimato)",
"chartTooltipRank": "Posizione: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "全て",
"chartLastNDays": "過去{days}日",
"chartNow": "現在",
"chartToday": "今日",
"chartYesterday": "昨日",
"chartDaysAgo": "{days} 日前",
"chartDaysAgoShort": "{days} 日前",
"chartOneWeekAgo": "1週間前",
"chartTwoWeeksAgo": "2週間前",
"chartWeeksAgoShort": "{weeks} 週間前",
"chartOneMonthAgo": "1ヶ月前",
"chartMonthsAgoShort": "{months} ヶ月前",
"chartAvgPerDay": "平均/日",
"chartEstimatedSuffix": "(推定)",
"chartTooltipRank": "ランク: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/ko-KR.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "전체",
"chartLastNDays": "최근 {days}일",
"chartNow": "지금",
"chartToday": "오늘",
"chartYesterday": "어제",
"chartDaysAgo": "{days}일 전",
"chartDaysAgoShort": "{days}일 전",
"chartOneWeekAgo": "1주 전",
"chartTwoWeeksAgo": "2주 전",
"chartWeeksAgoShort": "{weeks}주 전",
"chartOneMonthAgo": "1개월 전",
"chartMonthsAgoShort": "{months}개월 전",
"chartAvgPerDay": "일평균",
"chartEstimatedSuffix": " (추정)",
"chartTooltipRank": "순위: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/nl-NL.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "Alles",
"chartLastNDays": "Laatste {days} dagen",
"chartNow": "Nu",
"chartToday": "Vandaag",
"chartYesterday": "Gisteren",
"chartDaysAgo": "{days} dagen geleden",
"chartDaysAgoShort": "{days}d geleden",
"chartOneWeekAgo": "1 week geleden",
"chartTwoWeeksAgo": "2 weken geleden",
"chartWeeksAgoShort": "{weeks}w geleden",
"chartOneMonthAgo": "1 maand geleden",
"chartMonthsAgoShort": "{months}mnd geleden",
"chartAvgPerDay": "Gem./dag",
"chartEstimatedSuffix": " (geschat)",
"chartTooltipRank": "Rang: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/pl-PL.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "Pełny zakres",
"chartLastNDays": "Ostatnie {days} dni",
"chartNow": "Teraz",
"chartToday": "Dzisiaj",
"chartYesterday": "Wczoraj",
"chartDaysAgo": "{days} dni temu",
"chartDaysAgoShort": "{days} dni temu",
"chartOneWeekAgo": "Tydzień temu",
"chartTwoWeeksAgo": "2 tygodnie temu",
"chartWeeksAgoShort": "{weeks} tyg. temu",
"chartOneMonthAgo": "Miesiąc temu",
"chartMonthsAgoShort": "{months} mies. temu",
"chartAvgPerDay": "Śr./dzień",
"chartEstimatedSuffix": " (szacowane)",
"chartTooltipRank": "Ranga: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "Todos",
"chartLastNDays": "Últimos dias de {days}",
"chartNow": "Agora",
"chartToday": "Hoje",
"chartYesterday": "Ontem",
"chartDaysAgo": "{days} dias atrás",
"chartDaysAgoShort": "há {days}d",
"chartOneWeekAgo": "1 semana atrás",
"chartTwoWeeksAgo": "2 semanas atrás",
"chartWeeksAgoShort": "{weeks}s ago",
"chartOneMonthAgo": "1 mês atrás",
"chartMonthsAgoShort": "{months}meses atrás",
"chartAvgPerDay": "Média/dia",
"chartEstimatedSuffix": "(estimado)",
"chartTooltipRank": "Classificação: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "Все",
"chartLastNDays": "За последние {days} дней",
"chartNow": "Только что",
"chartToday": "Сегодня",
"chartYesterday": "Вчера",
"chartDaysAgo": "{days} дней назад",
"chartDaysAgoShort": "{days}дн. назад",
"chartOneWeekAgo": "Неделю назад",
"chartTwoWeeksAgo": "2 недели назад",
"chartWeeksAgoShort": "{weeks}н. назад",
"chartOneMonthAgo": "Месяц назад",
"chartMonthsAgoShort": "{months}мес. назад",
"chartAvgPerDay": "Средн./в день",
"chartEstimatedSuffix": " (примерно)",
"chartTooltipRank": "Место: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "全部",
"chartLastNDays": "前 {days} 天",
"chartNow": "现在",
"chartToday": "今天",
"chartYesterday": "昨天",
"chartDaysAgo": "{days}天前",
"chartDaysAgoShort": "{days}天前",
"chartOneWeekAgo": "一星期前",
"chartTwoWeeksAgo": "两星期前",
"chartWeeksAgoShort": "{weeks}星期前",
"chartOneMonthAgo": "一个月前",
"chartMonthsAgoShort": "{months}个月前",
"chartAvgPerDay": "平均变化/天",
"chartEstimatedSuffix": " (估计值)",
"chartTooltipRank": "排名: #{rank}",
Expand Down
9 changes: 0 additions & 9 deletions messages/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,6 @@
"chartTimeAll": "",
"chartLastNDays": "",
"chartNow": "",
"chartToday": "",
"chartYesterday": "",
"chartDaysAgo": "",
"chartDaysAgoShort": "",
"chartOneWeekAgo": "",
"chartTwoWeeksAgo": "",
"chartWeeksAgoShort": "",
"chartOneMonthAgo": "",
"chartMonthsAgoShort": "",
"chartAvgPerDay": "",
"chartEstimatedSuffix": "",
"chartTooltipRank": "",
Expand Down
39 changes: 22 additions & 17 deletions src/modules/player/chart/use-player-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore

import { Result } from 'better-result';
import type { Chart } from 'chart.js';
import { useTranslations } from 'use-intl';
import { useLocale, useTranslations } from 'use-intl';
import { z } from 'zod';

import type { MetricKey, MetricLabel, PlayerChartStats, TimeRange } from '@/modules/player/chart/chart-types';
Expand Down Expand Up @@ -65,32 +65,35 @@ function getIsMobile() {
return mobileQuery?.matches ?? false;
}

type Translator = ReturnType<typeof useTranslations<'player'>>;

function formatDaysAgo(t: Translator, daysAgo: number, short: boolean) {
if (daysAgo === 0) return t('chartToday');
function formatDaysAgo(formatters: ChartDateFormatters, daysAgo: number, short: boolean) {
if (daysAgo < 7) return formatters[short ? 'short' : 'long'].format(-daysAgo, 'day');
if (short) {
if (daysAgo < 7) return t('chartDaysAgoShort', { days: daysAgo });
if (daysAgo < 30) return t('chartWeeksAgoShort', { weeks: Math.round(daysAgo / 7) });
return t('chartMonthsAgoShort', { months: Math.round(daysAgo / 30) });
if (daysAgo < 30) return formatters.short.format(-Math.round(daysAgo / 7), 'week');
return formatters.short.format(-Math.round(daysAgo / 30), 'month');
}
if (daysAgo === 1) return t('chartYesterday');
if (daysAgo === 7) return t('chartOneWeekAgo');
if (daysAgo === 14) return t('chartTwoWeeksAgo');
if (daysAgo === 30) return t('chartOneMonthAgo');
return t('chartDaysAgo', { days: daysAgo });
return formatters.long.format(-daysAgo, 'day');
}

function getDaysAgo(date: Date, now: Date) {
return Math.max(0, Math.round((now.getTime() - date.getTime()) / DAY_MS));
}

type ChartDateFormatters = ReturnType<typeof createChartDateFormatters>;

function createChartDateFormatters(locale: string) {
return {
long: new Intl.RelativeTimeFormat(locale, { numeric: 'auto', style: 'long' }),
short: new Intl.RelativeTimeFormat(locale, { numeric: 'auto', style: 'narrow' })
};
}

export function usePlayerChart(
playerId: string,
stats: PlayerChartStats,
history: PlayerControllerGetPlayerHistoryItem[],
enabledMetrics: MetricKey[] = [...METRIC_KEYS]
) {
const locale = useLocale();
const t = useTranslations('player');
const tc = useTranslations('common');
const initialPrefs = useRef(loadPrefs()).current;
Expand Down Expand Up @@ -212,6 +215,7 @@ export function usePlayerChart(
const visibleDays = useMemo(() => getVisibleChartDayCount(sortedHistory, now), [sortedHistory, now]);
const minTime = sortedHistory.length > 0 ? new Date(sortedHistory[0].createdAt).getTime() : nowTime;
const maxTime = nowTime;
const chartDateFormatters = useMemo(() => createChartDateFormatters(locale), [locale]);

const chartPadding = useMemo(() => getPlayerChartPadding(sortedHistory.length + 1), [sortedHistory.length]);

Expand Down Expand Up @@ -276,7 +280,8 @@ export function usePlayerChart(
]);

const scales = useMemo(() => {
const formatTick = (value: number) => (value === maxTime ? t('chartNow') : formatDaysAgo(t, getDaysAgo(new Date(value), now), isMobile));
const formatTick = (value: number) =>
value === maxTime ? t('chartNow') : formatDaysAgo(chartDateFormatters, getDaysAgo(new Date(value), now), isMobile);
return buildPlayerChartScales({
activeKeys,
chartColors,
Expand All @@ -288,7 +293,7 @@ export function usePlayerChart(
maxTime,
formatTick
});
}, [activeKeys, chartColors, isSingle, isMobile, t, metricLabels, visibleDays, minTime, maxTime, now]);
}, [activeKeys, chartColors, isSingle, isMobile, t, metricLabels, visibleDays, minTime, maxTime, now, chartDateFormatters]);

const avgPerDayLabel = t('chartAvgPerDay');

Expand Down Expand Up @@ -316,7 +321,7 @@ export function usePlayerChart(
const x = context[0]?.parsed.x;
if (x == null) return '';
if (x === nowTime) return t('chartNow');
return formatDaysAgo(t, getDaysAgo(new Date(x), now), false);
return formatDaysAgo(chartDateFormatters, getDaysAgo(new Date(x), now), false);
},
label: (context: { datasetIndex: number; dataIndex: number; parsed: { y: number | null } }) => {
const key = activeKeys[context.datasetIndex];
Expand All @@ -332,7 +337,7 @@ export function usePlayerChart(
return { borderColor: color, backgroundColor: color };
}
}),
[activeKeys, chartPadding, estimatedFlags, chartColors.metricBorder, formatTooltipValue, t, now, nowTime]
[activeKeys, chartPadding, estimatedFlags, chartColors.metricBorder, formatTooltipValue, t, now, nowTime, chartDateFormatters]
);

return {
Expand Down