-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Added FeatureRoute component to make it easier to feature toggle ro…
…utes
- Loading branch information
Showing
2 changed files
with
38 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,37 @@ | ||
import { FC, ReactNode } from 'react'; | ||
import { Navigate, Route } from 'react-router-dom'; | ||
|
||
import { | ||
useFeatureToggling, | ||
UseFeatureTogglingOptions, | ||
} from 'src/hooks/useFeatureToggling'; | ||
|
||
interface CommonFeatureRouteProps extends UseFeatureTogglingOptions { | ||
path: string; | ||
element: ReactNode; | ||
redirectPath?: string; | ||
} | ||
|
||
interface FeatureRouteWithFallbackProps extends CommonFeatureRouteProps { | ||
redirectPath?: undefined; | ||
fallback: ReactNode; | ||
} | ||
|
||
export const FeatureRoute: FC< | ||
CommonFeatureRouteProps | FeatureRouteWithFallbackProps | ||
> = ({ path, element, ...props }) => { | ||
const { showContent } = useFeatureToggling({ ...props }); | ||
|
||
if (showContent) { | ||
return <Route path={path} element={element} />; | ||
} | ||
|
||
if ('fallback' in props) { | ||
return <Route path={path} element={props.fallback} />; | ||
} | ||
|
||
const redirectPath = | ||
'redirectPath' in props && props.redirectPath ? props.redirectPath : '/'; | ||
|
||
return <Route path={path} element={<Navigate replace to={redirectPath} />} />; | ||
}; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { Feature } from './Feature/Feature'; | ||
export { FeatureRoute } from './FeatureRoute/FeatureRoute'; |