1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-24 17:51:14 +02:00
unleash.unleash/frontend/src/component/splash/SplashPageRedirect/SplashPageRedirect.tsx
olav d8143c6ff4 chore: update react-router to v6 (#946)
* refactor: fix child selector warnings

* refactor: update react-router-dom

* refactor: use BrowserRouter as in react-router docs

* refactor: replace Redirect with Navigate

* refactor: replace Switch with Routes

* refactor: replace useHistory with useNavigate

* refactor: replace useParams types with useRequiredPathParam

* refactor: replace NavLink activeStyle with callback

* refactor: fix matchPath arg order

* refactor: Remove unused link state

* refactor: delete broken snapshot test

* refactor: render 404 page without redirect

* refactor: normalize path parameter names

* refactor: fix Route component usage
2022-05-05 13:42:18 +02:00

58 lines
1.8 KiB
TypeScript

import { useAuthSplash } from 'hooks/api/getters/useAuth/useAuthSplash';
import { useLocation, Navigate } from 'react-router-dom';
import { matchPath } from 'react-router';
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
import { IFlags } from 'interfaces/uiConfig';
import { IAuthSplash } from 'hooks/api/getters/useAuth/useAuthEndpoint';
import { activeSplashIds, SplashId } from 'component/splash/splash';
import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
export const SplashPageRedirect = () => {
const { pathname } = useLocation();
const { user } = useAuthUser();
const { splash } = useAuthSplash();
const { uiConfig, loading } = useUiConfig();
if (!user || !splash || !uiConfig || loading) {
// Wait for everything to load.
return null;
}
if (matchPath('/splash/:splashId', pathname)) {
// We've already redirected to the splash page.
return null;
}
// Read-only API users should never see splash screens
// since they don't have access to mark them as seen.
if (user.isAPI) {
return null;
}
// Find the splash page to show (if any).
const showSplashId = activeSplashIds.find(splashId => {
return (
isSplashRelevant(splashId, uiConfig.flags) &&
!hasSeenSplashId(splashId, splash)
);
});
if (!showSplashId) {
return null;
}
return <Navigate to={`/splash/${showSplashId}`} replace />;
};
const hasSeenSplashId = (splashId: SplashId, splash: IAuthSplash): boolean => {
return Boolean(splash[splashId]);
};
const isSplashRelevant = (splashId: SplashId, flags: IFlags): boolean => {
if (splashId === 'operators') {
return Boolean(flags.C || flags.CO);
}
return true;
};