2021-11-26 11:12:37 +01:00
|
|
|
import ConditionallyRender from './common/ConditionallyRender';
|
|
|
|
import EnvironmentSplash from './common/EnvironmentSplash/EnvironmentSplash';
|
2022-02-10 17:04:10 +01:00
|
|
|
import Feedback from './common/Feedback/Feedback';
|
|
|
|
import LayoutPicker from './layout/LayoutPicker/LayoutPicker';
|
2021-11-26 11:12:37 +01:00
|
|
|
import Loader from './common/Loader/Loader';
|
2022-02-10 17:04:10 +01:00
|
|
|
import NotFound from './common/NotFound/NotFound';
|
|
|
|
import ProtectedRoute from './common/ProtectedRoute/ProtectedRoute';
|
|
|
|
import SWRProvider from './providers/SWRProvider/SWRProvider';
|
2022-01-14 15:50:02 +01:00
|
|
|
import ToastRenderer from './common/ToastRenderer/ToastRenderer';
|
2022-02-10 17:04:10 +01:00
|
|
|
import styles from './styles.module.scss';
|
|
|
|
import { Redirect, Route, Switch } from 'react-router-dom';
|
|
|
|
import { routes } from './menu/routes';
|
|
|
|
import { useAuthDetails } from '../hooks/api/getters/useAuth/useAuthDetails';
|
|
|
|
import { useAuthUser } from '../hooks/api/getters/useAuth/useAuthUser';
|
|
|
|
import { useAuthSplash } from '../hooks/api/getters/useAuth/useAuthSplash';
|
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 { splash, refetchSplash } = useAuthSplash();
|
|
|
|
const { authDetails } = useAuthDetails();
|
|
|
|
const { user } = useAuthUser();
|
2021-05-04 09:59:42 +02:00
|
|
|
|
2022-02-10 17:04:10 +01:00
|
|
|
const isLoggedIn = Boolean(user?.id);
|
|
|
|
const hasFetchedAuth = Boolean(authDetails || user);
|
|
|
|
const showEnvSplash = isLoggedIn && splash?.environment === false;
|
2021-12-06 15:43:08 +01:00
|
|
|
|
2021-04-12 15:04:03 +02:00
|
|
|
const renderMainLayoutRoutes = () => {
|
|
|
|
return routes.filter(route => route.layout === 'main').map(renderRoute);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderStandaloneRoutes = () => {
|
|
|
|
return routes
|
|
|
|
.filter(route => route.layout === 'standalone')
|
|
|
|
.map(renderRoute);
|
|
|
|
};
|
|
|
|
|
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 />
|
|
|
|
|
2021-11-26 11:12:37 +01:00
|
|
|
<ConditionallyRender
|
2022-02-10 17:04:10 +01:00
|
|
|
condition={showEnvSplash}
|
2021-11-26 11:12:37 +01:00
|
|
|
show={
|
2022-02-10 17:04:10 +01:00
|
|
|
<EnvironmentSplash onFinish={refetchSplash} />
|
2021-11-26 11:12:37 +01:00
|
|
|
}
|
|
|
|
elseShow={
|
|
|
|
<LayoutPicker location={location}>
|
|
|
|
<Switch>
|
|
|
|
<ProtectedRoute
|
|
|
|
exact
|
|
|
|
path="/"
|
|
|
|
unauthorized={isUnauthorized()}
|
|
|
|
component={Redirect}
|
|
|
|
renderProps={{ to: '/features' }}
|
|
|
|
/>
|
|
|
|
{renderMainLayoutRoutes()}
|
|
|
|
{renderStandaloneRoutes()}
|
|
|
|
<Route
|
|
|
|
path="/404"
|
|
|
|
component={NotFound}
|
|
|
|
/>
|
|
|
|
<Redirect to="/404" />
|
|
|
|
</Switch>
|
2022-02-10 17:04:10 +01:00
|
|
|
<Feedback openUrl="http://feedback.unleash.run" />
|
2021-11-26 11:12:37 +01:00
|
|
|
</LayoutPicker>
|
|
|
|
}
|
2021-10-15 09:21:38 +02:00
|
|
|
/>
|
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
|
|
|
);
|
|
|
|
};
|