2022-03-28 10:49:59 +02:00
|
|
|
import ConditionallyRender from 'component/common/ConditionallyRender';
|
|
|
|
import Feedback from 'component/common/Feedback/Feedback';
|
|
|
|
import LayoutPicker from 'component/layout/LayoutPicker/LayoutPicker';
|
|
|
|
import Loader from 'component/common/Loader/Loader';
|
|
|
|
import NotFound from 'component/common/NotFound/NotFound';
|
|
|
|
import ProtectedRoute from 'component/common/ProtectedRoute/ProtectedRoute';
|
|
|
|
import SWRProvider from 'component/providers/SWRProvider/SWRProvider';
|
|
|
|
import ToastRenderer from 'component/common/ToastRenderer/ToastRenderer';
|
|
|
|
import styles from 'component/styles.module.scss';
|
2022-02-10 17:04:10 +01:00
|
|
|
import { Redirect, Route, Switch } from 'react-router-dom';
|
2022-03-28 10:49:59 +02:00
|
|
|
import { routes } from 'component/menu/routes';
|
2022-03-22 08:23:51 +01:00
|
|
|
import { useAuthDetails } from 'hooks/api/getters/useAuth/useAuthDetails';
|
|
|
|
import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
|
|
|
|
import { SplashPageRedirect } from 'component/splash/SplashPageRedirect/SplashPageRedirect';
|
2021-10-15 09:21:38 +02:00
|
|
|
|
2022-02-11 11:19:55 +01:00
|
|
|
export const App = () => {
|
2022-02-10 17:04:10 +01:00
|
|
|
const { authDetails } = useAuthDetails();
|
|
|
|
const { user } = useAuthUser();
|
|
|
|
const isLoggedIn = Boolean(user?.id);
|
|
|
|
const hasFetchedAuth = Boolean(authDetails || user);
|
2021-04-12 15:04:03 +02:00
|
|
|
|
2022-02-10 17:04:10 +01:00
|
|
|
const isUnauthorized = (): boolean => {
|
|
|
|
return !isLoggedIn;
|
2021-04-12 15:04:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Change this to IRoute once snags with HashRouter and TS is worked out
|
|
|
|
const renderRoute = (route: any) => {
|
|
|
|
if (route.type === 'protected') {
|
|
|
|
const unauthorized = isUnauthorized();
|
|
|
|
return (
|
|
|
|
<ProtectedRoute
|
|
|
|
key={route.path}
|
|
|
|
path={route.path}
|
|
|
|
component={route.component}
|
|
|
|
unauthorized={unauthorized}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Route
|
|
|
|
key={route.path}
|
|
|
|
path={route.path}
|
2021-04-12 15:34:37 +02:00
|
|
|
render={props => (
|
|
|
|
<route.component
|
|
|
|
{...props}
|
|
|
|
isUnauthorized={isUnauthorized}
|
2022-02-10 17:04:10 +01:00
|
|
|
authDetails={authDetails}
|
2021-04-12 15:34:37 +02:00
|
|
|
/>
|
|
|
|
)}
|
2021-04-12 15:04:03 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-02-10 17:04:10 +01:00
|
|
|
<SWRProvider isUnauthorized={isUnauthorized}>
|
2021-11-26 11:12:37 +01:00
|
|
|
<ConditionallyRender
|
2022-02-10 17:04:10 +01:00
|
|
|
condition={!hasFetchedAuth}
|
2021-11-26 11:12:37 +01:00
|
|
|
show={<Loader />}
|
|
|
|
elseShow={
|
|
|
|
<div className={styles.container}>
|
2022-01-28 10:51:48 +01:00
|
|
|
<ToastRenderer />
|
2022-03-22 08:23:51 +01:00
|
|
|
<LayoutPicker>
|
|
|
|
<Switch>
|
|
|
|
<ProtectedRoute
|
|
|
|
exact
|
|
|
|
path="/"
|
|
|
|
unauthorized={isUnauthorized()}
|
|
|
|
component={Redirect}
|
|
|
|
renderProps={{ to: '/features' }}
|
|
|
|
/>
|
|
|
|
{routes.map(renderRoute)}
|
|
|
|
<Route path="/404" component={NotFound} />
|
|
|
|
<Redirect to="/404" />
|
|
|
|
</Switch>
|
|
|
|
<Feedback openUrl="http://feedback.unleash.run" />
|
|
|
|
<SplashPageRedirect />
|
|
|
|
</LayoutPicker>
|
2021-11-26 11:12:37 +01:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
2021-10-19 13:08:25 +02:00
|
|
|
</SWRProvider>
|
2021-04-12 15:04:03 +02:00
|
|
|
);
|
|
|
|
};
|