import React, {
useState,
useEffect,
ReactNode,
createContext,
useContext,
} from 'react';
import { getCurrentUser } from '@/lib/appwrite';
// Define the props for the GlobalProvider component
interface GlobalProviderProps {
children: ReactNode;
}
interface GlobalContextType {
isLoggedIn: boolean;
setIsLoggedIn: (value: boolean) => void;
user: any;
setUser: (user: any) => void;
isLoading: boolean;
}
// Create the context with a default value
export const GlobalContext = createContext<GlobalContextType | undefined>(
undefined
);
// Custom hook to use the GlobalContext
export const useGlobalContext = (): GlobalContextType => {
const context = useContext(GlobalContext);
if (context === undefined) {
throw new Error('useGlobalContext must be used within a GlobalProvider');
}
return context;
};
// GlobalProvider component to provide the context to its children
export const GlobalProvider = ({
children,
}: GlobalProviderProps): JSX.Element => {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [user, setUser] = useState<any>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
getCurrentUser()
.then((res) => {
if (res) {
setIsLoggedIn(true);
setUser(res);
} else {
setIsLoggedIn(false);
}
setIsLoading(false);
})
.catch((err) => {
console.log(err);
setIsLoading(false); // Ensure loading state is updated even on error
});
}, []);
return (
<GlobalContext.Provider
value={{ isLoggedIn, setIsLoggedIn, user, setUser, isLoading }}
>
{children}
</GlobalContext.Provider>
);
};
export default GlobalProvider;
enter image description here
Type ‘{}’ is missing the following properties from type ‘ReactElement<any, any>’: type, props, keyts(2739)
Cannot find namespace ‘GlobalContext’.ts(2503)
Please help me with this issue i want provider to work so everyone who have session is automatically redirect to /explore