import React, { createContext, useState, useMemo, FC, useContext, Dispatch, SetStateAction, } from 'react' import {QuestionWhereInput} from '../../__generated__/graphql-schema' import {UserContext, useCurrentUser} from "../../contexts"; type ProviderValue = { where: QuestionWhereInput setWhere: Dispatch> } const DashboardContext = createContext(null) export const useDashboardContext = () => { const context = useContext(DashboardContext) if (context === null) { throw new Error('You probably forgot to put .') } return context } export const whereDefaultState = (userContext: UserContext) => ( userContext.isOnlyTeacher ? {userId: userContext.user?.id} : {} ) type DashboardProviderProps = { children: React.ReactNode } export const DashboardProvider = ({children}: DashboardProviderProps) => { const userContext = useCurrentUser() const [where, setWhere] = useState(whereDefaultState(userContext)) const providerValue = useMemo(() => ({where, setWhere}), [ where, setWhere, ]) return ( {children} ) }