This commit is contained in:
parent
85495bfc18
commit
2e93a1b27f
@ -46,17 +46,17 @@ export default function Navbar() {
|
||||
return (
|
||||
<>
|
||||
<div className="flex h-fit w-full rounded-md bg-gray-800">
|
||||
{settingsStore && settingsStore.profilePicture && (
|
||||
{settingsStore !== undefined && settingsStore.player && (
|
||||
<NavbarButton
|
||||
text="You"
|
||||
icon={
|
||||
<Avatar
|
||||
url={settingsStore.profilePicture}
|
||||
url={settingsStore.player.profilePicture}
|
||||
label="Your avatar"
|
||||
size={32}
|
||||
/>
|
||||
}
|
||||
href={`/player/${settingsStore.userId}`}
|
||||
href={`/player/${settingsStore.player.id}`}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
@ -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 (
|
||||
<Card className="mt-2">
|
||||
|
@ -35,7 +35,7 @@ export default function PlayerRanking({
|
||||
>
|
||||
<p
|
||||
className={
|
||||
player.id == settingsStore?.userId
|
||||
player.id == settingsStore.player?.id
|
||||
? "transform-gpu text-red-500 transition-all hover:text-blue-500"
|
||||
: ""
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ export default function PlayerRankingMobile({
|
||||
)}
|
||||
<p
|
||||
className={
|
||||
player.id == settingsStore?.userId
|
||||
player.id == settingsStore.player?.id
|
||||
? "transform-gpu text-red-500 transition-all hover:text-blue-500"
|
||||
: ""
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { ScoresaberPlayer } from "@/schemas/scoresaber/player";
|
||||
import { ScoresaberPlayerScore } from "@/schemas/scoresaber/playerScore";
|
||||
import { fetchAllScores, fetchScores } from "@/utils/scoresaber/api";
|
||||
import moment from "moment";
|
||||
@ -147,33 +148,29 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
|
||||
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<ScoresaberPlayer>();
|
||||
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<PlayerScoresStore>()(
|
||||
players: newPlayers,
|
||||
lastUpdated: Date.now(),
|
||||
});
|
||||
console.log(friends);
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
@ -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<boolean>;
|
||||
removeFriend: (friendId: string) => void;
|
||||
@ -30,18 +28,17 @@ const UPDATE_INTERVAL = 1000 * 60 * 10; // 10 minutes
|
||||
export const useSettingsStore = create<SettingsStore>()(
|
||||
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<SettingsStore>()(
|
||||
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<SettingsStore>()(
|
||||
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() });
|
||||
},
|
||||
}),
|
||||
|
Loading…
x
Reference in New Issue
Block a user