Skip to content

Commit

Permalink
Add: conditional route component for feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng committed Jul 9, 2024
1 parent ba2df9c commit 065b112
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/web/components/conditionalRoute/ConditionalRoute.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import PropTypes from 'prop-types';

import {Route, Redirect} from 'react-router-dom';
import useCapabilities from 'web/hooks/useCapabilities';

const ConditionalRoute = ({component: Component, feature, ...rest}) => {
const capabilities = useCapabilities();
const isEnabled = capabilities._featuresEnabled[feature];
return (
<Route
render={props =>
isEnabled ? <Component {...props} /> : <Redirect to="/notfound" />
}
{...rest}
/>
);
};

ConditionalRoute.propTypes = {
component: PropTypes.elementType.isRequired,
feature: PropTypes.string.isRequired,
};

export default ConditionalRoute;

Check warning on line 29 in src/web/components/conditionalRoute/ConditionalRoute.jsx

View check run for this annotation

Codecov / codecov/patch

src/web/components/conditionalRoute/ConditionalRoute.jsx#L2-L29

Added lines #L2 - L29 were not covered by tests

0 comments on commit 065b112

Please sign in to comment.