import { Children , ReactElement , ComponentType , createElement } from 'react' ; import { Location } from 'history' ; import warning from '../util/warning' ; import useAuthenticated from './useAuthenticated' ; import usePermissionsOptimized from './usePermissionsOptimized' ; export interface WithPermissionsChildrenParams { permissions : any ; } type WithPermissionsChildren = ( params : WithPermissionsChildrenParams ) => ReactElement ; export interface WithPermissionsProps { authParams ? : object ; children ? : WithPermissionsChildren ; component ? : ComponentType < any > ; location ? : Location ; render ? : WithPermissionsChildren ; staticContext ? : object ; [ key ] : any ; } const isEmptyChildren = children => Children . count ( children ) === 0 ; example const WithPermissions = ( props ) => { const { authParams , children , render , component , staticContext , ... rest } = props ; warning ( ( render && children && ! isEmptyChildren ( children ) ) || ( render && component ) || ( component && children && ! isEmptyChildren ( children ) ) , 'You should only use one of the `component`, `render` and `children` props in <WithPermissions>' ) ; useAuthenticated ( authParams ) ; const { permissions } = usePermissionsOptimized ( authParams ) ; if ( component ) { return createElement ( component , { permissions , ... rest } ) ; } if ( render ) { return render ( { permissions , ... rest } ) ; } if ( children ) { return children ( { permissions , ... rest } ) ; } } ; export default WithPermissions as ComponentType < WithPermissionsProps > ;