import { createContext, useState, useEffect, useMemo, useContext } from 'react' import { useTheme } from 'next-themes' import setLanguage from 'next-translate/setLanguage' import useTranslation from 'next-translate/useTranslation' import { Authentication, PagePropsWithAuthentication } from '.' import { UserCurrent } from '../../models/User' export interface AuthenticationValue { authentication: Authentication user: UserCurrent setUser: React.Dispatch> } const defaultAuthenticationContext: AuthenticationValue = {} as any const AuthenticationContext = createContext( defaultAuthenticationContext ) export const AuthenticationProvider: React.FC< React.PropsWithChildren > = (props) => { const { lang } = useTranslation() const { theme, setTheme } = useTheme() const [user, setUser] = useState(props.authentication.user) const authentication = useMemo(() => { const disableSocketIO = typeof window === 'undefined' return new Authentication(props.authentication.tokens, disableSocketIO) // eslint-disable-next-line react-hooks/exhaustive-deps -- We only want to run this memo once }, []) useEffect(() => { setLanguage(props.authentication.user.settings.language).catch(() => {}) setTheme(props.authentication.user.settings.theme) return () => { authentication?.socket?.disconnect() } // eslint-disable-next-line react-hooks/exhaustive-deps -- We only want to run this effect once }, []) useEffect(() => { authentication.api .put('/users/current/settings', { theme, language: lang }) .then(({ data: userCurrentSettings }) => { setUser((oldUser) => { return { ...oldUser, settings: { ...oldUser.settings, ...userCurrentSettings.settings } } }) }) .catch(() => {}) }, [theme, lang, authentication.api]) return ( {props.children} ) } export const useAuthentication = (): AuthenticationValue => { return useContext(AuthenticationContext) }