import React, { PropsWithChildren, useEffect } from 'react'
import { Auth0Provider, useAuth0 } from '@auth0/auth0-react'
import Router, { useRouter } from 'next/router'
import { LmAppProps, MyApp } from './_app'
import { useUserActions } from '../auth/useAuth'
import { withAppEmotionCache_mui } from '../global-theme/muiCache'
export { reportWebVitals } from './_appDefault'
const onRedirectCallback = (appState: any) => {
return Router.replace(appState?.returnTo || '/home')
}
function AppContainer({ children }: PropsWithChildren) {
const { locales, asPath, locale, push } = useRouter()
const { user, error } = useAuth0()
const { setUser } = useUserActions()
useEffect(() => {
if (user && (asPath === '/' || asPath === '/home')) {
const currentUserLocale = (user.lang || user.locale || 'en').substring(
0,
2
)
if (
currentUserLocale &&
locale !== currentUserLocale &&
locales?.includes(currentUserLocale)
) {
push(asPath, undefined, { locale: currentUserLocale })
}
}
}, [user, locales, asPath, locale, push])
useEffect(() => {
if (user) {
setUser({
id: user.id,
firstName: user.given_name,
lastName: user.family_name,
...user
})
} else {
setUser(null)
}
}, [setUser, user])
if (error) {
console.error(error)
}
return <>{children}>
}
function Auth0Wrap({ children }: PropsWithChildren) {
return (
{children}
)
}
export const Auth0App = withAppEmotionCache_mui(function Auth0AppFunc(
props: LmAppProps
) {
return (
)
})