Sudo.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import React from 'react';
  2. import { Message } from 'semantic-ui-react';
  3. import { AccountId } from '@polkadot/types/interfaces';
  4. import { withCalls, withMulti } from '@polkadot/react-api/with';
  5. import { useMyAccount } from '@polkadot/joy-utils/MyAccountContext';
  6. type OnlySudoProps = {
  7. sudo?: AccountId;
  8. };
  9. function OnlySudo<P extends OnlySudoProps> (Component: React.ComponentType<P>) {
  10. return function (props: P) {
  11. const { sudo } = props;
  12. if (!sudo) {
  13. return <em>Loading sudo key...</em>;
  14. }
  15. const { state: { address: myAddress } } = useMyAccount();
  16. const iAmSudo = myAddress === sudo.toString();
  17. if (iAmSudo) {
  18. return <Component {...props} />;
  19. } else {
  20. return (
  21. <Message warning className='JoyMainStatus'>
  22. <Message.Header>Only sudo can access this functionality.</Message.Header>
  23. </Message>
  24. );
  25. }
  26. };
  27. }
  28. export const withOnlySudo = <P extends OnlySudoProps> (Component: React.ComponentType<P>) =>
  29. withMulti(
  30. Component,
  31. withCalls(['query.sudo.key', { propName: 'sudo' }]),
  32. OnlySudo
  33. );