mirror of
https://github.com/TheRealOwenRees/chess-scribe-website.git
synced 2026-07-23 01:46:57 +00:00
44 lines
946 B
TypeScript
44 lines
946 B
TypeScript
import {
|
|
createContext,
|
|
type Dispatch,
|
|
type ReactNode,
|
|
type SetStateAction,
|
|
useContext,
|
|
useState,
|
|
} from "react";
|
|
|
|
interface ILichessUser {
|
|
username: string;
|
|
id: string;
|
|
isLoggedIn: boolean;
|
|
}
|
|
|
|
interface ILichessUserContextType {
|
|
user: ILichessUser | null;
|
|
setUser: Dispatch<SetStateAction<ILichessUser | null>>
|
|
}
|
|
|
|
const LichessUserContext = createContext<ILichessUserContextType | undefined>(
|
|
undefined,
|
|
);
|
|
|
|
export const LichessUserProvider = ({ children }: { children: ReactNode }) => {
|
|
const [user, setUser] = useState<ILichessUser | null>(null);
|
|
|
|
// TODO useEffect to check and login user on mount
|
|
|
|
return (
|
|
<LichessUserContext.Provider value={{ user, setUser }}>
|
|
{children}
|
|
</LichessUserContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useLichessUser = () => {
|
|
const context = useContext(LichessUserContext);
|
|
if (!context) {
|
|
throw new Error("useLichessUser must be used within a LichessUserProvider");
|
|
}
|
|
return context;
|
|
};
|