import { Navigate, Route, RouterProvider, createBrowserRouter, createRoutesFromElements, useLocation, useRouteLoaderData } from 'react-router-dom'
import PropTypes from 'prop-types'
import { UserProvider } from './store/user'
import { ajax } from './utils/fetch'
import Layout from './components/Layout'
import Index from './pages/Index'
import Login from './pages/Login'
import Register from './pages/Register'
import NoMatch from './pages/NoMatch'
import Engine from './pages/Engine'
import Quests from './pages/Quests'
import User from './pages/User'
import { useRole } from './utils/roles'
import Quest from './pages/admin/Quest'
const router = createBrowserRouter(
createRoutesFromElements(
}
loader={async () => ajax('/api/user').catch(x => { console.log(x); return null })}
>
}
/>
}
loader={() => ajax('/api/games').catch(x => { console.log(x); return null })}
/>
} />
} />
} />
}
loader={({ params }) => ajax(`/api/engine/${params.gameId}`).catch(x => { console.log(x); return null })}
/>
}
// loader={() => ajax(`/api/admin/games`)}
/>
}
// loader={() => ajax(`/api/admin/games`)}
/>
} />
)
)
function App () {
return (
)
}
function Auth (props) {
const baseUser = useRouteLoaderData('root')
const { user } = UserProvider.useContainer()
const { hasRole } = useRole()
const location = useLocation()
if (!user && !baseUser) {
return
}
if (props.role && !hasRole(props.role)) {
return
}
return props.children
}
Auth.propTypes = {
children: PropTypes.any,
role: PropTypes.string
}
export default App