import { createContext , useContext , useEffect , useState } from "react" ; import type { GetUserResponseBody , UserResponseObject } from "../pages/api/user/api-types" ; export type LoginUser = UserResponseObject ; const userContext = createContext < LoginUser | null > ( null ) ; export const UserProvider = ( { children } ) => { const [ user , setUser ] = useState < LoginUser | null > ( null ) ; useEffect ( ( ) => { const abortController = new AbortController ( ) ; fetch ( "/api/user/get" , { signal : abortController . signal } ) . then ( ( res ) => res . json ( ) ) . then ( ( json ) => { if ( json . isLogin ) { setUser ( json ) ; } return json ; } ) . catch ( ( error ) => { console . error ( "Failed to Fetch" , error ) ; } ) ; return ( ) => { abortController . abort ( ) ; } ; } , [ ] ) ; return < userContext . Provider value = { user } > { children } < /userContext.Provider>; } ; /**
 * get login user
 */ export function useLoginUser ( ) { return useContext ( userContext ) ; }