Skip to content

Instantly share code, notes, and snippets.

@francois-roget
Created May 25, 2021 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francois-roget/084bcf0abd22a1cedfabafd208b6af34 to your computer and use it in GitHub Desktop.
Save francois-roget/084bcf0abd22a1cedfabafd208b6af34 to your computer and use it in GitHub Desktop.
type Props = {
to: Permission;
fallback?: JSX.Element | string;
loadingComponent?: JSX.Element | string;
};
// This component is meant to be used everywhere a restriction based on user permission is needed
const Restricted: React.FunctionComponent<Props> = ({to, fallback, loadingComponent, children}) => {
// We "connect" to the provider thanks to the PermissionContext
const [loading, allowed] = usePermission(to);
if(loading){
return <>{loadingComponent}</>;
}
// If the user has that permission, render the children
if(allowed){
return <>{children}</>;
}
// Otherwise, render the fallback
return <>{fallback}</>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment