ghost-portal/packages/page-sudo/src/Sudo.tsx
Jaco 5174f5ffbe
Bump API & common (#6797)
* Bump API & common

* BN imports

* Root imports

* Bump
2022-01-07 17:37:36 +02:00

106 lines
3.1 KiB
TypeScript

// Copyright 2017-2022 @polkadot/app-js authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { SubmittableExtrinsic } from '@polkadot/api/types';
import type { BN } from '@polkadot/util';
import React, { useCallback, useState } from 'react';
import styled from 'styled-components';
import { Button, Extrinsic, Icon, InputNumber, Toggle, TxButton } from '@polkadot/react-components';
import { useApi, useToggle } from '@polkadot/react-hooks';
import { BN_ZERO, isFunction } from '@polkadot/util';
import { useTranslation } from './translate';
interface Props {
className?: string;
isMine: boolean;
sudoKey?: string;
}
function Sudo ({ className, isMine, sudoKey }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api, apiDefaultTxSudo } = useApi();
const [withWeight, toggleWithWeight] = useToggle();
const [method, setMethod] = useState<SubmittableExtrinsic<'promise'> | null>(null);
const [weight, setWeight] = useState<BN>(BN_ZERO);
const _onChangeExtrinsic = useCallback(
(method: SubmittableExtrinsic<'promise'> | null = null) => setMethod(() => method),
[]
);
const _onChangeWeight = useCallback(
(weight: BN = BN_ZERO) => setWeight(weight),
[]
);
return isMine
? (
<section className={className}>
<Extrinsic
defaultValue={apiDefaultTxSudo}
label={t<string>('submit the following change')}
onChange={_onChangeExtrinsic}
/>
{isFunction(api.tx.sudo.sudoUncheckedWeight) && (
<InputNumber
help={t<string>('The unchecked weight as specified for the sudoUncheckedWeight call.')}
isDisabled={!withWeight}
isError={weight.eq(BN_ZERO)}
isZeroable={false}
label={t<string>('unchecked weight for this call')}
onChange={_onChangeWeight}
value={weight}
>
<Toggle
className='sudoToggle'
isOverlay
label={t<string>('with weight override')}
onChange={toggleWithWeight}
value={withWeight}
/>
</InputNumber>
)}
<Button.Group>
<TxButton
accountId={sudoKey}
icon='sign-in-alt'
isDisabled={!method || (withWeight ? weight.eq(BN_ZERO) : false)}
label={
withWeight
? t<string>('Submit Sudo Unchecked')
: t<string>('Submit Sudo')
}
params={
withWeight
? [method, weight]
: [method]
}
tx={
withWeight
? api.tx.sudo.sudoUncheckedWeight
: api.tx.sudo.sudo
}
/>
</Button.Group>
</section>
)
: (
<article className='error padded'>
<div>
<Icon icon='ban' />
{t<string>('You do not have access to the current sudo key')}
</div>
</article>
);
}
export default React.memo(styled(Sudo)`
.sudoToggle {
width: 100%;
text-align: right;
}
`);