scoresaber-reloaded-v2/src/store/settingsStore.ts
Liam 1ff7c246c3
All checks were successful
deploy / deploy (push) Successful in 2m11s
added local score fetching
2023-10-22 02:17:21 +01:00

50 lines
1.2 KiB
TypeScript

"use client";
import { getPlayerInfo } from "@/utils/scoresaber/api";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
interface SettingsStore {
userId: string | undefined;
profilePicture: string | undefined;
setUserId: (userId: string) => void;
setProfilePicture: (profilePicture: string) => void;
refreshProfile: () => void;
}
export const useSettingsStore = create<SettingsStore>()(
persist(
(set) => ({
userId: undefined,
profilePicture: undefined,
setUserId: (userId: string) => {
set({ userId });
},
setProfilePicture: (profilePicture: string) => set({ profilePicture }),
async refreshProfile() {
const id = useSettingsStore.getState().userId;
if (!id) return;
const profile = await getPlayerInfo(id);
if (profile == undefined || profile == null) return;
useSettingsStore.setState({
userId: profile.id,
profilePicture: profile.profilePicture,
});
console.log("Updated profile:", profile.id);
},
}),
{
name: "settings",
storage: createJSONStorage(() => localStorage),
},
),
);
useSettingsStore.getState().refreshProfile();