This commit is contained in:
parent
85495bfc18
commit
2e93a1b27f
@ -46,17 +46,17 @@ export default function Navbar() {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="flex h-fit w-full rounded-md bg-gray-800">
|
<div className="flex h-fit w-full rounded-md bg-gray-800">
|
||||||
{settingsStore && settingsStore.profilePicture && (
|
{settingsStore !== undefined && settingsStore.player && (
|
||||||
<NavbarButton
|
<NavbarButton
|
||||||
text="You"
|
text="You"
|
||||||
icon={
|
icon={
|
||||||
<Avatar
|
<Avatar
|
||||||
url={settingsStore.profilePicture}
|
url={settingsStore.player.profilePicture}
|
||||||
label="Your avatar"
|
label="Your avatar"
|
||||||
size={32}
|
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);
|
const toastId: any = useRef(null);
|
||||||
|
|
||||||
async function claimProfile() {
|
async function claimProfile() {
|
||||||
settingsStore?.setUserId(playerId);
|
settingsStore?.setProfile(playerData);
|
||||||
addProfile(false);
|
addProfile(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ export default function PlayerInfo({ playerData }: PlayerInfoProps) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const isOwnProfile = settingsStore?.userId == playerId;
|
const isOwnProfile = settingsStore.player?.id == playerId;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="mt-2">
|
<Card className="mt-2">
|
||||||
|
@ -35,7 +35,7 @@ export default function PlayerRanking({
|
|||||||
>
|
>
|
||||||
<p
|
<p
|
||||||
className={
|
className={
|
||||||
player.id == settingsStore?.userId
|
player.id == settingsStore.player?.id
|
||||||
? "transform-gpu text-red-500 transition-all hover:text-blue-500"
|
? "transform-gpu text-red-500 transition-all hover:text-blue-500"
|
||||||
: ""
|
: ""
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ export default function PlayerRankingMobile({
|
|||||||
)}
|
)}
|
||||||
<p
|
<p
|
||||||
className={
|
className={
|
||||||
player.id == settingsStore?.userId
|
player.id == settingsStore.player?.id
|
||||||
? "transform-gpu text-red-500 transition-all hover:text-blue-500"
|
? "transform-gpu text-red-500 transition-all hover:text-blue-500"
|
||||||
: ""
|
: ""
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { ScoresaberPlayer } from "@/schemas/scoresaber/player";
|
||||||
import { ScoresaberPlayerScore } from "@/schemas/scoresaber/playerScore";
|
import { ScoresaberPlayerScore } from "@/schemas/scoresaber/playerScore";
|
||||||
import { fetchAllScores, fetchScores } from "@/utils/scoresaber/api";
|
import { fetchAllScores, fetchScores } from "@/utils/scoresaber/api";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
@ -147,33 +148,29 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
|
|||||||
const players = usePlayerScoresStore.getState().players;
|
const players = usePlayerScoresStore.getState().players;
|
||||||
const friends = useSettingsStore.getState().friends;
|
const friends = useSettingsStore.getState().friends;
|
||||||
|
|
||||||
const localPlayer = {
|
let allPlayers = new Array<ScoresaberPlayer>();
|
||||||
id: useSettingsStore.getState().userId,
|
for (const friend of friends) {
|
||||||
scores: {
|
allPlayers.push(friend);
|
||||||
scoresaber: [],
|
}
|
||||||
},
|
const localPlayer = useSettingsStore.getState().player;
|
||||||
};
|
if (localPlayer) {
|
||||||
let allPlayers: any = friends;
|
|
||||||
if (players.findIndex((player) => player.id == localPlayer.id) == -1) {
|
|
||||||
allPlayers.push(localPlayer);
|
allPlayers.push(localPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add local player and friends if they don't exist
|
// add local player and friends if they don't exist
|
||||||
for (const player of allPlayers) {
|
for (const player of allPlayers) {
|
||||||
if (usePlayerScoresStore.getState().get(player.id) == undefined) {
|
if (usePlayerScoresStore.getState().get(player.id) == undefined) {
|
||||||
toast.success(
|
toast.info(
|
||||||
`${
|
`${
|
||||||
player.id == localPlayer.id
|
player.id == localPlayer?.id
|
||||||
? `You were`
|
? `You were`
|
||||||
: `Friend ${player.name} was`
|
: `Friend ${player.name} was`
|
||||||
} missing from the scores database, adding...`,
|
} missing from the scores database, adding...`,
|
||||||
);
|
);
|
||||||
console.log(
|
await usePlayerScoresStore.getState().addPlayer(player.id);
|
||||||
await usePlayerScoresStore.getState().addPlayer(player.id),
|
|
||||||
);
|
|
||||||
toast.success(
|
toast.success(
|
||||||
`${
|
`${
|
||||||
player.id == useSettingsStore.getState().userId
|
player.id == useSettingsStore.getState().player?.id
|
||||||
? `You were`
|
? `You were`
|
||||||
: `Friend ${player.name} was`
|
: `Friend ${player.name} was`
|
||||||
} added to the scores database`,
|
} added to the scores database`,
|
||||||
@ -259,6 +256,7 @@ export const usePlayerScoresStore = create<PlayerScoresStore>()(
|
|||||||
players: newPlayers,
|
players: newPlayers,
|
||||||
lastUpdated: Date.now(),
|
lastUpdated: Date.now(),
|
||||||
});
|
});
|
||||||
|
console.log(friends);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
@ -8,14 +8,12 @@ import { create } from "zustand";
|
|||||||
import { createJSONStorage, persist } from "zustand/middleware";
|
import { createJSONStorage, persist } from "zustand/middleware";
|
||||||
|
|
||||||
interface SettingsStore {
|
interface SettingsStore {
|
||||||
userId: string | undefined;
|
player: ScoresaberPlayer | undefined;
|
||||||
profilePicture: string | undefined;
|
|
||||||
lastUsedSortType: SortType;
|
lastUsedSortType: SortType;
|
||||||
friends: ScoresaberPlayer[];
|
friends: ScoresaberPlayer[];
|
||||||
profilesLastUpdated: number;
|
profilesLastUpdated: number;
|
||||||
|
|
||||||
setUserId: (userId: string) => void;
|
setProfile: (playerData: ScoresaberPlayer) => void;
|
||||||
setProfilePicture: (profilePicture: string) => void;
|
|
||||||
setLastUsedSortType: (sortType: SortType) => void;
|
setLastUsedSortType: (sortType: SortType) => void;
|
||||||
addFriend: (friendId: string) => Promise<boolean>;
|
addFriend: (friendId: string) => Promise<boolean>;
|
||||||
removeFriend: (friendId: string) => void;
|
removeFriend: (friendId: string) => void;
|
||||||
@ -30,18 +28,17 @@ const UPDATE_INTERVAL = 1000 * 60 * 10; // 10 minutes
|
|||||||
export const useSettingsStore = create<SettingsStore>()(
|
export const useSettingsStore = create<SettingsStore>()(
|
||||||
persist(
|
persist(
|
||||||
(set) => ({
|
(set) => ({
|
||||||
userId: undefined,
|
player: undefined,
|
||||||
profilePicture: undefined,
|
|
||||||
lastUsedSortType: SortTypes.top,
|
lastUsedSortType: SortTypes.top,
|
||||||
friends: [],
|
friends: [],
|
||||||
profilesLastUpdated: 0,
|
profilesLastUpdated: 0,
|
||||||
|
|
||||||
setUserId: (userId: string) => {
|
async setProfile(playerData: ScoresaberPlayer) {
|
||||||
set({ userId });
|
set({
|
||||||
|
player: playerData,
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
setProfilePicture: (profilePicture: string) => set({ profilePicture }),
|
|
||||||
|
|
||||||
setLastUsedSortType: (sortType: SortType) =>
|
setLastUsedSortType: (sortType: SortType) =>
|
||||||
set({ lastUsedSortType: sortType }),
|
set({ lastUsedSortType: sortType }),
|
||||||
|
|
||||||
@ -72,8 +69,9 @@ export const useSettingsStore = create<SettingsStore>()(
|
|||||||
return friends.some((friend) => friend.id == friendId);
|
return friends.some((friend) => friend.id == friendId);
|
||||||
},
|
},
|
||||||
|
|
||||||
setProfilesLastUpdated: (profilesLastUpdated: number) =>
|
setProfilesLastUpdated: (profilesLastUpdated: number) => {
|
||||||
set({ profilesLastUpdated }),
|
set({ profilesLastUpdated });
|
||||||
|
},
|
||||||
|
|
||||||
async refreshProfiles() {
|
async refreshProfiles() {
|
||||||
const timeUntilRefreshMs =
|
const timeUntilRefreshMs =
|
||||||
@ -89,32 +87,6 @@ export const useSettingsStore = create<SettingsStore>()(
|
|||||||
return;
|
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() });
|
useSettingsStore.setState({ profilesLastUpdated: Date.now() });
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
Loading…
Reference in New Issue
Block a user