add context

This commit is contained in:
2026-06-07 23:07:59 +02:00
parent 82c24ba9d4
commit ba05fbedd3
3 changed files with 70 additions and 18 deletions
+47
View File
@@ -0,0 +1,47 @@
import {
createContext,
type Dispatch,
type ReactNode,
type SetStateAction,
useContext,
useState,
} from "react";
interface ILichessUser {
username: string;
isLoggedIn: boolean;
}
interface ILichessUserContextType {
user: ILichessUser | null;
setUser: Dispatch<SetStateAction<ILichessUser | null>>;
logout: () => void;
}
const LichessUserContext = createContext<ILichessUserContextType | undefined>(
undefined,
);
export const LichessUserProvider = ({ children }: { children: ReactNode }) => {
const [user, setUser] = useState<ILichessUser | null>(null);
const logout = () => {
setUser(null);
};
// TODO useEffect to check and login user on mount
return (
<LichessUserContext.Provider value={{ user, setUser, logout }}>
{children}
</LichessUserContext.Provider>
);
};
export const useLichessUser = () => {
const context = useContext(LichessUserContext);
if (!context) {
throw new Error("useLichessUser must be used within a LichessUserProvider");
}
return context;
};