Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upkeep: Add TS support for IsAdmin component #299

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading