You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/react/hooks/useLimitToBE.tsx

39 lines
988 B

import { useRouter } from '@uirouter/react';
import { ComponentType } from 'react';
import { isBE } from '@/react/portainer/feature-flags/feature-flags.service';
export function useLimitToBE(defaultPath = 'portainer.home') {
const router = useRouter();
if (!isBE) {
router.stateService.go(defaultPath);
return true;
}
return false;
}
export function withLimitToBE<T>(
WrappedComponent: ComponentType<T>,
defaultPath = 'portainer.home'
): ComponentType<T> {
// Try to create a nice displayName for React Dev Tools.
const displayName =
WrappedComponent.displayName || WrappedComponent.name || 'Component';
function WrapperComponent(props: T) {
const isLimitedToBE = useLimitToBE(defaultPath);
if (isLimitedToBE) {
return null;
}
// eslint-disable-next-line react/jsx-props-no-spreading
return <WrappedComponent {...props} />;
}
WrapperComponent.displayName = `withLimitToBE(${displayName})`;
return WrapperComponent;
}