Files
js/@3rdweb-sdk/react/components/roles/admin-only.tsx
T
Jonas Daniels 3b647fccc1 react 18 upgrade (#80)
* react 18 upgrade (WIP)

* fix typography logic

* fix lots of lint issues
2022-05-12 18:03:53 -07:00

57 lines
1.2 KiB
TypeScript

import {
isContractWithRoles,
useIsAccountRole,
useWeb3,
} from "@3rdweb-sdk/react";
import { ValidContractInstance } from "@thirdweb-dev/sdk";
import { ComponentWithChildren } from "types/component-with-children";
interface AdminOnlyProps {
contract?: ValidContractInstance;
fallback?: JSX.Element;
}
export const AdminOnly: ComponentWithChildren<AdminOnlyProps> = ({
children,
contract,
fallback,
}) => {
const { address } = useWeb3();
const isAdmin = useIsAccountRole(
"admin",
isContractWithRoles(contract) ? contract : undefined,
address,
);
if (!isAdmin) {
return fallback ?? null;
}
return <>{children}</>;
};
interface AdminOrSelfOnlyProps extends AdminOnlyProps {
/**
* The address of the account to check against
*/
self: string;
}
export const AdminOrSelfOnly: ComponentWithChildren<AdminOrSelfOnlyProps> = ({
children,
self,
fallback,
contract,
}) => {
const { address } = useWeb3();
const isAdmin = useIsAccountRole(
"admin",
isContractWithRoles(contract) ? contract : undefined,
address,
);
if (!isAdmin && address !== self) {
return fallback ?? null;
}
return <>{children}</>;
};