import { createContext, useContext, useEffect } from "react" import type { HabitsTrackerPresenter, HabitsTrackerPresenterState, } from "@/presentation/presenters/HabitsTracker" import { usePresenterState } from "@/presentation/react/hooks/usePresenterState" import { habitsTrackerPresenter } from "@/infrastructure" import { useAuthentication } from "./Authentication" export interface HabitsTrackerContextValue extends HabitsTrackerPresenterState { habitsTrackerPresenter: HabitsTrackerPresenter } const defaultContextValue = {} as HabitsTrackerContextValue const HabitsTrackerContext = createContext(defaultContextValue) interface HabitsTrackerProviderProps extends React.PropsWithChildren {} export const HabitsTrackerProvider: React.FC = ( props, ) => { const { children } = props const { user } = useAuthentication() useEffect(() => { if (user == null) { return } habitsTrackerPresenter .retrieveHabitsTracker({ userId: user.id }) .catch((error) => { console.error(error) }) }, [user]) const habitsTrackerPresenterState = usePresenterState(habitsTrackerPresenter) return ( {children} ) } export const useHabitsTracker = (): HabitsTrackerContextValue => { const context = useContext(HabitsTrackerContext) if (context === defaultContextValue) { throw new Error( "`useHabitsTracker` must be used within a `HabitsTrackerProvider`.", ) } return context }