diff --git a/src/store/scoresaberScoresStore.ts b/src/store/scoresaberScoresStore.ts index 34102b6..1ef656a 100644 --- a/src/store/scoresaberScoresStore.ts +++ b/src/store/scoresaberScoresStore.ts @@ -3,6 +3,7 @@ import { ScoresaberPlayer } from "@/schemas/scoresaber/player"; import { ScoresaberSmallerPlayerScore } from "@/schemas/scoresaber/smaller/smallerPlayerScore"; import { ScoreSaberAPI } from "@/utils/scoresaber/api"; +import { formatMsToTime } from "@/utils/timeUtils"; import { toast } from "react-toastify"; import { create } from "zustand"; import { createJSONStorage, persist } from "zustand/middleware"; @@ -244,15 +245,16 @@ export const useScoresaberScoresStore = create()( if (player.lastUpdated == undefined) { player.lastUpdated = Date.now(); } + const playerData = useSettingsStore.getState().getProfile(player.id); // Skip if we refreshed the scores recently const timeUntilRefreshMs = UPDATE_INTERVAL - (Date.now() - player.lastUpdated); if (timeUntilRefreshMs > 0) { console.log( - "Waiting", - timeUntilRefreshMs / 1000, - "seconds to refresh scores for " + player.id, + `Waiting ${formatMsToTime( + timeUntilRefreshMs, + )} to refresh player: ${playerData?.name}`, ); continue; } diff --git a/src/store/settingsStore.ts b/src/store/settingsStore.ts index bbe693c..64d5c23 100644 --- a/src/store/settingsStore.ts +++ b/src/store/settingsStore.ts @@ -3,6 +3,7 @@ import { ScoresaberPlayer } from "@/schemas/scoresaber/player"; import { SortType, SortTypes } from "@/types/SortTypes"; import { ScoreSaberAPI } from "@/utils/scoresaber/api"; +import { formatMsToTime } from "@/utils/timeUtils"; import { create } from "zustand"; import { createJSONStorage, persist } from "zustand/middleware"; import { IDBStorage } from "./IndexedDBStorage"; @@ -22,6 +23,7 @@ interface SettingsStore { setLastUsedSortType: (sortType: SortType) => void; setProfilesLastUpdated: (profilesLastUpdated: number) => void; refreshProfiles: () => Promise; + getProfile(playerId: string): ScoresaberPlayer | undefined; } const UPDATE_INTERVAL = 1000 * 60 * 10; // 10 minutes @@ -82,9 +84,9 @@ export const useSettingsStore = create()( UPDATE_INTERVAL - (Date.now() - get().profilesLastUpdated); if (timeUntilRefreshMs > 0) { console.log( - "Waiting", - timeUntilRefreshMs / 1000, - "to refresh profiles", + `Waiting ${formatMsToTime( + timeUntilRefreshMs, + )} to refresh player profiles`, ); return; } @@ -109,6 +111,11 @@ export const useSettingsStore = create()( ); set({ profilesLastUpdated: Date.now(), friends: newFriends }); }, + + getProfile(playerId: string) { + const allProfiles = [get().player, ...get().friends]; + return allProfiles.find((profile) => profile?.id == playerId); + }, }), { name: "settings", diff --git a/src/utils/timeUtils.ts b/src/utils/timeUtils.ts index a51559c..7df0978 100644 --- a/src/utils/timeUtils.ts +++ b/src/utils/timeUtils.ts @@ -41,3 +41,21 @@ export function formatDate(timestamp: string) { 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; +}