fix time formatting
All checks were successful
deploy / deploy (push) Successful in 57s

This commit is contained in:
Lee 2023-10-23 16:27:44 +01:00
parent b88c33e91c
commit cc1b6d9a63
3 changed files with 33 additions and 6 deletions

@ -3,6 +3,7 @@
import { ScoresaberPlayer } from "@/schemas/scoresaber/player"; import { ScoresaberPlayer } from "@/schemas/scoresaber/player";
import { ScoresaberSmallerPlayerScore } from "@/schemas/scoresaber/smaller/smallerPlayerScore"; import { ScoresaberSmallerPlayerScore } from "@/schemas/scoresaber/smaller/smallerPlayerScore";
import { ScoreSaberAPI } from "@/utils/scoresaber/api"; import { ScoreSaberAPI } from "@/utils/scoresaber/api";
import { formatMsToTime } from "@/utils/timeUtils";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
import { create } from "zustand"; import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware"; import { createJSONStorage, persist } from "zustand/middleware";
@ -244,15 +245,16 @@ export const useScoresaberScoresStore = create<ScoreSaberScoresStore>()(
if (player.lastUpdated == undefined) { if (player.lastUpdated == undefined) {
player.lastUpdated = Date.now(); player.lastUpdated = Date.now();
} }
const playerData = useSettingsStore.getState().getProfile(player.id);
// Skip if we refreshed the scores recently // Skip if we refreshed the scores recently
const timeUntilRefreshMs = const timeUntilRefreshMs =
UPDATE_INTERVAL - (Date.now() - player.lastUpdated); UPDATE_INTERVAL - (Date.now() - player.lastUpdated);
if (timeUntilRefreshMs > 0) { if (timeUntilRefreshMs > 0) {
console.log( console.log(
"Waiting", `Waiting ${formatMsToTime(
timeUntilRefreshMs / 1000, timeUntilRefreshMs,
"seconds to refresh scores for " + player.id, )} to refresh player: ${playerData?.name}`,
); );
continue; continue;
} }

@ -3,6 +3,7 @@
import { ScoresaberPlayer } from "@/schemas/scoresaber/player"; import { ScoresaberPlayer } from "@/schemas/scoresaber/player";
import { SortType, SortTypes } from "@/types/SortTypes"; import { SortType, SortTypes } from "@/types/SortTypes";
import { ScoreSaberAPI } from "@/utils/scoresaber/api"; import { ScoreSaberAPI } from "@/utils/scoresaber/api";
import { formatMsToTime } from "@/utils/timeUtils";
import { create } from "zustand"; import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware"; import { createJSONStorage, persist } from "zustand/middleware";
import { IDBStorage } from "./IndexedDBStorage"; import { IDBStorage } from "./IndexedDBStorage";
@ -22,6 +23,7 @@ interface SettingsStore {
setLastUsedSortType: (sortType: SortType) => void; setLastUsedSortType: (sortType: SortType) => void;
setProfilesLastUpdated: (profilesLastUpdated: number) => void; setProfilesLastUpdated: (profilesLastUpdated: number) => void;
refreshProfiles: () => Promise<void>; refreshProfiles: () => Promise<void>;
getProfile(playerId: string): ScoresaberPlayer | undefined;
} }
const UPDATE_INTERVAL = 1000 * 60 * 10; // 10 minutes const UPDATE_INTERVAL = 1000 * 60 * 10; // 10 minutes
@ -82,9 +84,9 @@ export const useSettingsStore = create<SettingsStore>()(
UPDATE_INTERVAL - (Date.now() - get().profilesLastUpdated); UPDATE_INTERVAL - (Date.now() - get().profilesLastUpdated);
if (timeUntilRefreshMs > 0) { if (timeUntilRefreshMs > 0) {
console.log( console.log(
"Waiting", `Waiting ${formatMsToTime(
timeUntilRefreshMs / 1000, timeUntilRefreshMs,
"to refresh profiles", )} to refresh player profiles`,
); );
return; return;
} }
@ -109,6 +111,11 @@ export const useSettingsStore = create<SettingsStore>()(
); );
set({ profilesLastUpdated: Date.now(), friends: newFriends }); set({ profilesLastUpdated: Date.now(), friends: newFriends });
}, },
getProfile(playerId: string) {
const allProfiles = [get().player, ...get().friends];
return allProfiles.find((profile) => profile?.id == playerId);
},
}), }),
{ {
name: "settings", name: "settings",

@ -41,3 +41,21 @@ export function formatDate(timestamp: string) {
minute: "numeric", minute: "numeric",
}); });
} }
/**
* Formats a time in milliseconds to a human readable format
*
* @param ms the time in milliseconds
* @returns the formatted time
*/
export function formatMsToTime(ms: number) {
const seconds = Math.floor((ms / 1000) % 60);
const minutes = Math.floor((ms / (1000 * 60)) % 60);
const hours = Math.floor((ms / (1000 * 60 * 60)) % 24);
const hoursStr = hours > 0 ? hours.toString() + ":" : "";
const minutesStr = minutes.toString().padStart(2, "0") + ":";
const secondsStr = seconds.toString().padStart(2, "0");
return hoursStr + minutesStr + secondsStr;
}