From 2e93a1b27ff0f59540f2c7beee781e15a1663a65 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 22 Oct 2023 10:50:10 +0100 Subject: [PATCH] cleanup --- src/components/Navbar.tsx | 6 +-- src/components/player/PlayerInfo.tsx | 4 +- src/components/player/PlayerRanking.tsx | 2 +- src/components/player/PlayerRankingMobile.tsx | 2 +- src/store/playerScoresStore.ts | 26 +++++----- src/store/settingsStore.ts | 48 ++++--------------- 6 files changed, 29 insertions(+), 59 deletions(-) diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 3bca09f..2ce84ba 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -46,17 +46,17 @@ export default function Navbar() { return ( <>
- {settingsStore && settingsStore.profilePicture && ( + {settingsStore !== undefined && settingsStore.player && ( } - href={`/player/${settingsStore.userId}`} + href={`/player/${settingsStore.player.id}`} /> )} diff --git a/src/components/player/PlayerInfo.tsx b/src/components/player/PlayerInfo.tsx index 1e89671..97aaf08 100644 --- a/src/components/player/PlayerInfo.tsx +++ b/src/components/player/PlayerInfo.tsx @@ -36,7 +36,7 @@ export default function PlayerInfo({ playerData }: PlayerInfoProps) { const toastId: any = useRef(null); async function claimProfile() { - settingsStore?.setUserId(playerId); + settingsStore?.setProfile(playerData); addProfile(false); } @@ -91,7 +91,7 @@ export default function PlayerInfo({ playerData }: PlayerInfoProps) { } } - const isOwnProfile = settingsStore?.userId == playerId; + const isOwnProfile = settingsStore.player?.id == playerId; return ( diff --git a/src/components/player/PlayerRanking.tsx b/src/components/player/PlayerRanking.tsx index c6c34ac..c9cff22 100644 --- a/src/components/player/PlayerRanking.tsx +++ b/src/components/player/PlayerRanking.tsx @@ -35,7 +35,7 @@ export default function PlayerRanking({ >

()( const players = usePlayerScoresStore.getState().players; const friends = useSettingsStore.getState().friends; - const localPlayer = { - id: useSettingsStore.getState().userId, - scores: { - scoresaber: [], - }, - }; - let allPlayers: any = friends; - if (players.findIndex((player) => player.id == localPlayer.id) == -1) { + let allPlayers = new Array(); + for (const friend of friends) { + allPlayers.push(friend); + } + const localPlayer = useSettingsStore.getState().player; + if (localPlayer) { allPlayers.push(localPlayer); } // add local player and friends if they don't exist for (const player of allPlayers) { if (usePlayerScoresStore.getState().get(player.id) == undefined) { - toast.success( + toast.info( `${ - player.id == localPlayer.id + player.id == localPlayer?.id ? `You were` : `Friend ${player.name} was` } missing from the scores database, adding...`, ); - console.log( - await usePlayerScoresStore.getState().addPlayer(player.id), - ); + await usePlayerScoresStore.getState().addPlayer(player.id); toast.success( `${ - player.id == useSettingsStore.getState().userId + player.id == useSettingsStore.getState().player?.id ? `You were` : `Friend ${player.name} was` } added to the scores database`, @@ -259,6 +256,7 @@ export const usePlayerScoresStore = create()( players: newPlayers, lastUpdated: Date.now(), }); + console.log(friends); } }, }), diff --git a/src/store/settingsStore.ts b/src/store/settingsStore.ts index f870b2c..81813f0 100644 --- a/src/store/settingsStore.ts +++ b/src/store/settingsStore.ts @@ -8,14 +8,12 @@ import { create } from "zustand"; import { createJSONStorage, persist } from "zustand/middleware"; interface SettingsStore { - userId: string | undefined; - profilePicture: string | undefined; + player: ScoresaberPlayer | undefined; lastUsedSortType: SortType; friends: ScoresaberPlayer[]; profilesLastUpdated: number; - setUserId: (userId: string) => void; - setProfilePicture: (profilePicture: string) => void; + setProfile: (playerData: ScoresaberPlayer) => void; setLastUsedSortType: (sortType: SortType) => void; addFriend: (friendId: string) => Promise; removeFriend: (friendId: string) => void; @@ -30,18 +28,17 @@ const UPDATE_INTERVAL = 1000 * 60 * 10; // 10 minutes export const useSettingsStore = create()( persist( (set) => ({ - userId: undefined, - profilePicture: undefined, + player: undefined, lastUsedSortType: SortTypes.top, friends: [], profilesLastUpdated: 0, - setUserId: (userId: string) => { - set({ userId }); + async setProfile(playerData: ScoresaberPlayer) { + set({ + player: playerData, + }); }, - setProfilePicture: (profilePicture: string) => set({ profilePicture }), - setLastUsedSortType: (sortType: SortType) => set({ lastUsedSortType: sortType }), @@ -72,8 +69,9 @@ export const useSettingsStore = create()( return friends.some((friend) => friend.id == friendId); }, - setProfilesLastUpdated: (profilesLastUpdated: number) => - set({ profilesLastUpdated }), + setProfilesLastUpdated: (profilesLastUpdated: number) => { + set({ profilesLastUpdated }); + }, async refreshProfiles() { const timeUntilRefreshMs = @@ -89,32 +87,6 @@ export const useSettingsStore = create()( return; } - const userId = useSettingsStore.getState().userId; - const profiles = - useSettingsStore.getState().friends.map((f) => f.id) ?? []; - if (userId) { - profiles.push(userId); - } - - for (const profileId of profiles) { - const profile = await getPlayerInfo(profileId); - if (profile == undefined || profile == null) return; - - if (this.isFriend(profileId)) { - const friends = useSettingsStore.getState().friends; - const friendIndex = friends.findIndex( - (friend) => friend.id == profileId, - ); - friends[friendIndex] = profile; - set({ friends }); - } else { - this.setProfilePicture(profile.profilePicture); - set({ userId: profile.id }); - } - - console.log("Updated profile:", profile.id); - } - useSettingsStore.setState({ profilesLastUpdated: Date.now() }); }, }),