import { Navigate } from 'react-router-dom';

import useCommunityFeature from 'src/hooks/useCommunityFeature';
import { CommunityFeature } from 'src/types/communityFeatures';

export type CommunityFeatureGuardProps = {
  requiredCommunityFeature: CommunityFeature;
};

const CommunityFeatureGuard = ({
  requiredCommunityFeature,
  children,
}: React.PropsWithChildren<CommunityFeatureGuardProps>) => {
  const hasRequiredCommunityFeature = useCommunityFeature(
    requiredCommunityFeature,
  );

  if (!hasRequiredCommunityFeature) {
    return <Navigate to="/" />;
  }

  return <>{children}</>;
};

export default CommunityFeatureGuard;
