1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-18 11:14:57 +02:00
unleash.unleash/frontend/src/component/splash/SplashPageRedirect/SplashPageRedirect.tsx
olav 6efa9fe75c fix: hide splash screens from API users (#852)
* refactor: add missing user isAPI field

* fix: hide splash screens from API users

Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
2022-04-05 20:34:23 +02:00

58 lines
1.8 KiB
TypeScript

import { useAuthSplash } from 'hooks/api/getters/useAuth/useAuthSplash';
import { useLocation, Redirect } 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(pathname, { path: '/splash/:splashId' })) {
// 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 <Redirect to={`/splash/${showSplashId}`} />;
};
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;
};