Download react-material-dashboard/src/contexts/user-context.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.67 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-material-dashboard/src/contexts/user-context.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-material-dashboard/src/contexts/user-context.tsx
-
curl -L -o user-context.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-material-dashboard/src/contexts/user-context.tsx
1.67 kB
| 'use client'; | |
| import * as React from 'react'; | |
| import type { User } from '@/types/user'; | |
| import { authClient } from '@/lib/auth/client'; | |
| import { logger } from '@/lib/default-logger'; | |
| export interface UserContextValue { | |
| user: User | null; | |
| error: string | null; | |
| isLoading: boolean; | |
| checkSession?: () => Promise<void>; | |
| } | |
| export const UserContext = React.createContext<UserContextValue | undefined>(undefined); | |
| export interface UserProviderProps { | |
| children: React.ReactNode; | |
| } | |
| export function UserProvider({ children }: UserProviderProps): React.JSX.Element { | |
| const [state, setState] = React.useState<{ user: User | null; error: string | null; isLoading: boolean }>({ | |
| user: null, | |
| error: null, | |
| isLoading: true, | |
| }); | |
| const checkSession = React.useCallback(async (): Promise<void> => { | |
| try { | |
| const { data, error } = await authClient.getUser(); | |
| if (error) { | |
| logger.error(error); | |
| setState((prev) => ({ ...prev, user: null, error: 'Something went wrong', isLoading: false })); | |
| return; | |
| } | |
| setState((prev) => ({ ...prev, user: data ?? null, error: null, isLoading: false })); | |
| } catch (error) { | |
| logger.error(error); | |
| setState((prev) => ({ ...prev, user: null, error: 'Something went wrong', isLoading: false })); | |
| } | |
| }, []); | |
| React.useEffect(() => { | |
| checkSession().catch((error) => { | |
| logger.error(error); | |
| // noop | |
| }); | |
| // eslint-disable-next-line react-hooks/exhaustive-deps -- Expected | |
| }, []); | |
| return <UserContext.Provider value={{ ...state, checkSession }}>{children}</UserContext.Provider>; | |
| } | |
| export const UserConsumer = UserContext.Consumer; | |