This commit is contained in:
parent
2b91388b95
commit
1001e68e1a
@ -59,7 +59,7 @@ export default function Player({ params }: { params: { id: string } }) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScoreSaberAPI.getPlayerInfo(params.id).then((playerResponse) => {
|
ScoreSaberAPI.fetchPlayerData(params.id).then((playerResponse) => {
|
||||||
if (!playerResponse) {
|
if (!playerResponse) {
|
||||||
setError(true);
|
setError(true);
|
||||||
setErrorMessage("Failed to fetch player. Is the ID correct?");
|
setErrorMessage("Failed to fetch player. Is the ID correct?");
|
||||||
|
@ -27,7 +27,7 @@ export default function SearchPlayer() {
|
|||||||
const id = search.split("/").pop();
|
const id = search.split("/").pop();
|
||||||
if (id == undefined) return;
|
if (id == undefined) return;
|
||||||
|
|
||||||
const player = await ScoreSaberAPI.getPlayerInfo(id);
|
const player = await ScoreSaberAPI.fetchPlayerData(id);
|
||||||
if (player == undefined) return;
|
if (player == undefined) return;
|
||||||
|
|
||||||
setPlayers([player]);
|
setPlayers([player]);
|
||||||
|
@ -314,7 +314,6 @@ export const useScoresaberScoresStore = create<ScoreSaberScoresStore>()(
|
|||||||
players: newPlayers,
|
players: newPlayers,
|
||||||
lastUpdated: Date.now(),
|
lastUpdated: Date.now(),
|
||||||
});
|
});
|
||||||
console.log(friends);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
@ -9,16 +9,16 @@ import { createJSONStorage, persist } from "zustand/middleware";
|
|||||||
|
|
||||||
interface SettingsStore {
|
interface SettingsStore {
|
||||||
player: ScoresaberPlayer | undefined;
|
player: ScoresaberPlayer | undefined;
|
||||||
lastUsedSortType: SortType;
|
|
||||||
friends: ScoresaberPlayer[];
|
friends: ScoresaberPlayer[];
|
||||||
|
lastUsedSortType: SortType;
|
||||||
profilesLastUpdated: number;
|
profilesLastUpdated: number;
|
||||||
|
|
||||||
setProfile: (playerData: ScoresaberPlayer) => void;
|
setProfile: (playerData: ScoresaberPlayer) => void;
|
||||||
setLastUsedSortType: (sortType: SortType) => void;
|
|
||||||
addFriend: (friendId: string) => Promise<boolean>;
|
addFriend: (friendId: string) => Promise<boolean>;
|
||||||
removeFriend: (friendId: string) => void;
|
removeFriend: (friendId: string) => void;
|
||||||
isFriend: (friendId: string) => boolean;
|
isFriend: (friendId: string) => boolean;
|
||||||
clearFriends: () => void;
|
clearFriends: () => void;
|
||||||
|
setLastUsedSortType: (sortType: SortType) => void;
|
||||||
setProfilesLastUpdated: (profilesLastUpdated: number) => void;
|
setProfilesLastUpdated: (profilesLastUpdated: number) => void;
|
||||||
refreshProfiles: () => void;
|
refreshProfiles: () => void;
|
||||||
}
|
}
|
||||||
@ -39,16 +39,13 @@ export const useSettingsStore = create<SettingsStore>()(
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
setLastUsedSortType: (sortType: SortType) =>
|
|
||||||
set({ lastUsedSortType: sortType }),
|
|
||||||
|
|
||||||
async addFriend(friendId: string) {
|
async addFriend(friendId: string) {
|
||||||
const friends = useSettingsStore.getState().friends;
|
const friends = useSettingsStore.getState().friends;
|
||||||
if (friends.some((friend) => friend.id == friendId)) {
|
if (friends.some((friend) => friend.id == friendId)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const friend = await ScoreSaberAPI.getPlayerInfo(friendId);
|
const friend = await ScoreSaberAPI.fetchPlayerData(friendId);
|
||||||
if (friend == undefined || friend == null) return false;
|
if (friend == undefined || friend == null) return false;
|
||||||
|
|
||||||
set({ friends: [...friends, friend] });
|
set({ friends: [...friends, friend] });
|
||||||
@ -73,6 +70,10 @@ export const useSettingsStore = create<SettingsStore>()(
|
|||||||
set({ profilesLastUpdated });
|
set({ profilesLastUpdated });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
setLastUsedSortType: (sortType: SortType) => {
|
||||||
|
set({ lastUsedSortType: sortType });
|
||||||
|
},
|
||||||
|
|
||||||
async refreshProfiles() {
|
async refreshProfiles() {
|
||||||
const timeUntilRefreshMs =
|
const timeUntilRefreshMs =
|
||||||
UPDATE_INTERVAL -
|
UPDATE_INTERVAL -
|
||||||
@ -87,6 +88,26 @@ export const useSettingsStore = create<SettingsStore>()(
|
|||||||
return;
|
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() });
|
useSettingsStore.setState({ profilesLastUpdated: Date.now() });
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
@ -50,7 +50,7 @@ async function searchByName(
|
|||||||
* @param playerId the id of the player
|
* @param playerId the id of the player
|
||||||
* @returns the player info
|
* @returns the player info
|
||||||
*/
|
*/
|
||||||
async function getPlayerInfo(
|
async function fetchPlayerData(
|
||||||
playerId: string,
|
playerId: string,
|
||||||
): Promise<ScoresaberPlayer | undefined | null> {
|
): Promise<ScoresaberPlayer | undefined | null> {
|
||||||
const response = await fetchQueue.fetch(
|
const response = await fetchQueue.fetch(
|
||||||
@ -201,7 +201,7 @@ async function fetchTopPlayers(
|
|||||||
|
|
||||||
export const ScoreSaberAPI = {
|
export const ScoreSaberAPI = {
|
||||||
searchByName,
|
searchByName,
|
||||||
getPlayerInfo,
|
fetchPlayerData,
|
||||||
fetchScores,
|
fetchScores,
|
||||||
fetchAllScores,
|
fetchAllScores,
|
||||||
fetchTopPlayers,
|
fetchTopPlayers,
|
||||||
|
Loading…
Reference in New Issue
Block a user