-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: conditional route component for feature flag
- Loading branch information
1 parent
ba2df9c
commit 065b112
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||