Skip to content

Commit

Permalink
Merge pull request #299 from 10up/upkeep/component-is-admin
Browse files Browse the repository at this point in the history
upkeep:  Add TS support for `IsAdmin` component
  • Loading branch information
fabiankaegy authored Mar 4, 2024
2 parents f2602be + d84d2a1 commit 35eed23
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions components/is-admin/index.js → components/is-admin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { useSelect } from '@wordpress/data';

interface IsAdminProps {
/**
* Fallback component.
*/
fallback: React.ReactNode | null;

/**
* Child component.
*/
children: React.ReactNode;
}

/**
* IsAdmin
*
* A wrapper component that checks wether the current user has admin capabilities
* and only returns the child components if the user is an admin. You can pass a
* fallback component via the fallback prop.
*
* @param {object} props react props
* @param {*} props.fallback fallback component
* @param {*} props.children child components
* @returns {*}
*/
export const IsAdmin = ({ fallback = null, children }) => {
const hasAdminPermissions = useSelect(
export const IsAdmin: React.FC<IsAdminProps> = ({
fallback = null,
children,
}) => {
const hasAdminPermissions: boolean = useSelect(
(select) => select('core').canUser('read', 'users?roles=1'),
[],
);
Expand Down

0 comments on commit 35eed23

Please sign in to comment.