fix days ago labels (made it statically generate them instead of being based on the data provided
Some checks failed
Deploy / deploy (push) Failing after 2m24s

This commit is contained in:
Lee 2024-10-02 10:37:18 +01:00
parent 31416e21a1
commit c949931621

@ -1,6 +1,6 @@
"use client"; "use client";
import { getDaysAgo, parseDate } from "@/common/time-utils"; import { parseDate } from "@/common/time-utils";
import ScoreSaberPlayer from "@/common/model/player/impl/scoresaber-player"; import ScoreSaberPlayer from "@/common/model/player/impl/scoresaber-player";
import React from "react"; import React from "react";
import GenericChart, { DatasetConfig } from "@/components/chart/generic-chart"; import GenericChart, { DatasetConfig } from "@/components/chart/generic-chart";
@ -18,6 +18,20 @@ type Props = {
datasetConfig: DatasetConfig[]; datasetConfig: DatasetConfig[];
}; };
// Set up the labels
const labels: string[] = [];
const historyDays = 50;
for (let day = 0; day < historyDays; day++) {
if (day == 0) {
labels.push("Today");
} else if (day == 1) {
labels.push("Yesterday");
} else {
labels.push(`${day + 1} days ago`);
}
}
labels.reverse();
export default function GenericPlayerChart({ player, datasetConfig }: Props) { export default function GenericPlayerChart({ player, datasetConfig }: Props) {
if (!player.statisticHistory || Object.keys(player.statisticHistory).length === 0) { if (!player.statisticHistory || Object.keys(player.statisticHistory).length === 0) {
return ( return (
@ -26,8 +40,6 @@ export default function GenericPlayerChart({ player, datasetConfig }: Props) {
</div> </div>
); );
} }
const labels: string[] = [];
const histories: Record<string, (number | null)[]> = {}; const histories: Record<string, (number | null)[]> = {};
// Initialize histories for each dataset // Initialize histories for each dataset
@ -49,19 +61,13 @@ export default function GenericPlayerChart({ player, datasetConfig }: Props) {
// Fill in missing days with null values // Fill in missing days with null values
if (previousDate) { if (previousDate) {
const diffDays = Math.floor((currentDate.getTime() - previousDate.getTime()) / (1000 * 60 * 60 * 24)); const diffDays = Math.floor((currentDate.getTime() - previousDate.getTime()) / (1000 * 60 * 60 * 24));
for (let i = 1; i < diffDays; i++) { for (let i = 1; i < diffDays; i++) {
labels.push(`${getDaysAgo(new Date(currentDate.getTime() - i * 24 * 60 * 60 * 1000))} days ago`);
datasetConfig.forEach(config => { datasetConfig.forEach(config => {
histories[config.field].push(null); histories[config.field].push(null);
}); });
} }
} }
// Add today's label
const daysAgo = getDaysAgo(currentDate);
labels.push(daysAgo === 0 ? "Today" : `${daysAgo} days ago`);
// Push the historical data to histories // Push the historical data to histories
datasetConfig.forEach(config => { datasetConfig.forEach(config => {
histories[config.field].push(getValueFromHistory(history, config.field) ?? null); histories[config.field].push(getValueFromHistory(history, config.field) ?? null);