From 1001e68e1a34de43fe97c2a69022fd39a02bb0de Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 23 Oct 2023 06:16:51 +0100 Subject: [PATCH] remove debug and cleanup --- src/app/player/[id]/page.tsx | 2 +- src/components/player/SearchPlayer.tsx | 2 +- src/store/scoresaberScoresStore.ts | 1 - src/store/settingsStore.ts | 33 +++++++++++++++++++++----- src/utils/scoresaber/api.ts | 4 ++-- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/app/player/[id]/page.tsx b/src/app/player/[id]/page.tsx index dcb78cd..cfa0559 100644 --- a/src/app/player/[id]/page.tsx +++ b/src/app/player/[id]/page.tsx @@ -59,7 +59,7 @@ export default function Player({ params }: { params: { id: string } }) { return; } - ScoreSaberAPI.getPlayerInfo(params.id).then((playerResponse) => { + ScoreSaberAPI.fetchPlayerData(params.id).then((playerResponse) => { if (!playerResponse) { setError(true); setErrorMessage("Failed to fetch player. Is the ID correct?"); diff --git a/src/components/player/SearchPlayer.tsx b/src/components/player/SearchPlayer.tsx index 133b8e9..357b2e9 100644 --- a/src/components/player/SearchPlayer.tsx +++ b/src/components/player/SearchPlayer.tsx @@ -27,7 +27,7 @@ export default function SearchPlayer() { const id = search.split("/").pop(); if (id == undefined) return; - const player = await ScoreSaberAPI.getPlayerInfo(id); + const player = await ScoreSaberAPI.fetchPlayerData(id); if (player == undefined) return; setPlayers([player]); diff --git a/src/store/scoresaberScoresStore.ts b/src/store/scoresaberScoresStore.ts index 99c8664..48ec9f0 100644 --- a/src/store/scoresaberScoresStore.ts +++ b/src/store/scoresaberScoresStore.ts @@ -314,7 +314,6 @@ export const useScoresaberScoresStore = create()( players: newPlayers, lastUpdated: Date.now(), }); - console.log(friends); } }, }), diff --git a/src/store/settingsStore.ts b/src/store/settingsStore.ts index 4d9db05..ec39356 100644 --- a/src/store/settingsStore.ts +++ b/src/store/settingsStore.ts @@ -9,16 +9,16 @@ import { createJSONStorage, persist } from "zustand/middleware"; interface SettingsStore { player: ScoresaberPlayer | undefined; - lastUsedSortType: SortType; friends: ScoresaberPlayer[]; + lastUsedSortType: SortType; profilesLastUpdated: number; setProfile: (playerData: ScoresaberPlayer) => void; - setLastUsedSortType: (sortType: SortType) => void; addFriend: (friendId: string) => Promise; removeFriend: (friendId: string) => void; isFriend: (friendId: string) => boolean; clearFriends: () => void; + setLastUsedSortType: (sortType: SortType) => void; setProfilesLastUpdated: (profilesLastUpdated: number) => void; refreshProfiles: () => void; } @@ -39,16 +39,13 @@ export const useSettingsStore = create()( }); }, - setLastUsedSortType: (sortType: SortType) => - set({ lastUsedSortType: sortType }), - async addFriend(friendId: string) { const friends = useSettingsStore.getState().friends; if (friends.some((friend) => friend.id == friendId)) { return false; } - const friend = await ScoreSaberAPI.getPlayerInfo(friendId); + const friend = await ScoreSaberAPI.fetchPlayerData(friendId); if (friend == undefined || friend == null) return false; set({ friends: [...friends, friend] }); @@ -73,6 +70,10 @@ export const useSettingsStore = create()( set({ profilesLastUpdated }); }, + setLastUsedSortType: (sortType: SortType) => { + set({ lastUsedSortType: sortType }); + }, + async refreshProfiles() { const timeUntilRefreshMs = UPDATE_INTERVAL - @@ -87,6 +88,26 @@ export const useSettingsStore = create()( return; } + const player = useSettingsStore.getState().player; + if (player != undefined) { + const newPlayer = await ScoreSaberAPI.fetchPlayerData(player.id); + if (newPlayer != undefined && newPlayer != null) { + console.log("Updated player data for", newPlayer.name); + set({ player: newPlayer }); + } + } + + const friends = useSettingsStore.getState().friends; + const newFriends = await Promise.all( + friends.map(async (friend) => { + const newFriend = await ScoreSaberAPI.fetchPlayerData(friend.id); + if (newFriend == undefined || newFriend == null) return friend; + console.log("Updated friend data for", newFriend.name); + return newFriend; + }), + ); + set({ friends: newFriends }); + useSettingsStore.setState({ profilesLastUpdated: Date.now() }); }, }), diff --git a/src/utils/scoresaber/api.ts b/src/utils/scoresaber/api.ts index 86a2810..d37f221 100644 --- a/src/utils/scoresaber/api.ts +++ b/src/utils/scoresaber/api.ts @@ -50,7 +50,7 @@ async function searchByName( * @param playerId the id of the player * @returns the player info */ -async function getPlayerInfo( +async function fetchPlayerData( playerId: string, ): Promise { const response = await fetchQueue.fetch( @@ -201,7 +201,7 @@ async function fetchTopPlayers( export const ScoreSaberAPI = { searchByName, - getPlayerInfo, + fetchPlayerData, fetchScores, fetchAllScores, fetchTopPlayers,