Skip to content

Commit

Permalink
✨ Added FeatureRoute component to make it easier to feature toggle ro…
Browse files Browse the repository at this point in the history
…utes
  • Loading branch information
mariush2 committed Dec 16, 2024
1 parent adaed2a commit 219b448
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/components/FeatureRoute/FeatureRoute.tsx
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} />} />;
};
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { Feature } from './Feature/Feature';
export { FeatureRoute } from './FeatureRoute/FeatureRoute';

0 comments on commit 219b448

Please sign in to comment.