scoresaber-reloaded-v2/src/store/settingsStore.ts

48 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-10-19 19:31:05 +00:00
"use client";
2023-10-21 21:16:46 +00:00
import { getPlayerInfo } from "@/utils/scoresaber/api";
2023-10-19 19:31:05 +00:00
import { create } from "zustand";
2023-10-21 21:16:46 +00:00
import { persist } from "zustand/middleware";
2023-10-19 19:31:05 +00:00
2023-10-21 21:16:46 +00:00
interface SettingsStore {
userId: string | undefined;
profilePicture: string | undefined;
setUserId: (userId: string) => void;
setProfilePicture: (profilePicture: string) => void;
}
export const useSettingsStore = create<SettingsStore>()(
2023-10-19 19:31:05 +00:00
persist(
2023-10-21 21:16:46 +00:00
(set) => ({
userId: undefined,
profilePicture: undefined,
2023-10-19 19:31:05 +00:00
2023-10-21 21:16:46 +00:00
setUserId: (userId: string) => {
set({ userId });
},
setProfilePicture: (profilePicture: string) => set({ profilePicture }),
2023-10-19 19:31:05 +00:00
}),
{
2023-10-21 21:16:46 +00:00
name: "settings",
getStorage: () => localStorage,
2023-10-19 19:31:05 +00:00
},
),
);
2023-10-21 21:16:46 +00:00
async function 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);
}
refreshProfile();