Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: conditional route component for feature flag #4095

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add: conditional route component for feature flag
daniele-mng committed Jul 9, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
DonRichards Don Richards
commit 065b11209563024c2216ca80893e19a96ed11659
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];
a-h-abdelsalam marked this conversation as resolved.
Show resolved Hide resolved
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

Codecov / codecov/patch

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

Added lines #L2 - L29 were not covered by tests