40 lines
944 B
TypeScript
40 lines
944 B
TypeScript
"use client";
|
|
|
|
import React, { createContext, useContext, useState, ReactNode } from 'react';
|
|
import { User } from '../types';
|
|
import { MOCK_USER } from '../lib/mockData';
|
|
|
|
interface UserContextType {
|
|
user: User | null;
|
|
login: () => void;
|
|
logout: () => void;
|
|
}
|
|
|
|
const UserContext = createContext<UserContextType | undefined>(undefined);
|
|
|
|
export function UserProvider({ children }: { children: ReactNode }) {
|
|
const [user, setUser] = useState<User | null>(null);
|
|
|
|
const login = () => {
|
|
setUser(MOCK_USER);
|
|
};
|
|
|
|
const logout = () => {
|
|
setUser(null);
|
|
};
|
|
|
|
return (
|
|
<UserContext.Provider value={{ user, login, logout }}>
|
|
{children}
|
|
</UserContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useUser() {
|
|
const context = useContext(UserContext);
|
|
if (context === undefined) {
|
|
throw new Error('useUser must be used within a UserProvider');
|
|
}
|
|
return context;
|
|
}
|