Skip to content

FIX: Unwanted Errors on Sudo Page When Submitting Calls #11426

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

Merged
merged 2 commits into from
Mar 27, 2025
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
57 changes: 18 additions & 39 deletions packages/page-sudo/src/Sudo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// 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 { Button, Icon, InputNumber, styled, Toggle, TxButton } from '@polkadot/react-components';
import { Button, Icon, styled, Toggle, TxButton } from '@polkadot/react-components';
import { useApi, useToggle } from '@polkadot/react-hooks';
import { Extrinsic } from '@polkadot/react-params';
import { BN_ZERO, isFunction } from '@polkadot/util';
import { isFunction } from '@polkadot/util';

import { useTranslation } from './translate.js';

Expand All @@ -21,67 +20,46 @@ interface Props {

function Sudo ({ className, isMine, sudoKey }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api, apiDefaultTxSudo } = useApi();
const { api } = useApi();
const [withWeight, toggleWithWeight] = useToggle();
const [method, setMethod] = useState<SubmittableExtrinsic<'promise'> | null>(null);
const [weight, setWeight] = useState<BN>(BN_ZERO);
const [extrinsic, setExtrinsic] = useState<SubmittableExtrinsic<'promise'> | null>(null);

const _onChangeExtrinsic = useCallback(
(method: SubmittableExtrinsic<'promise'> | null = null) => setMethod(() => method),
[]
);

const _onChangeWeight = useCallback(
(weight: BN = BN_ZERO) => setWeight(weight),
(method?: SubmittableExtrinsic<'promise'>) => {
setExtrinsic(() => method || null);
},
[]
);

return isMine
? (
<StyledSection className={className}>
<Extrinsic
defaultValue={apiDefaultTxSudo}
defaultValue={withWeight ? api.tx.sudo.sudoUncheckedWeight : api.tx.sudo.sudo}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes state loss when the weight override is toggled. Please fix.

isDisabled
key={String(withWeight)}
label={t('submit the following change')}
onChange={_onChangeExtrinsic}
/>
{isFunction(api.tx.sudo.sudoUncheckedWeight) && (
<InputNumber
isDisabled={!withWeight}
isError={weight.eq(BN_ZERO)}
isZeroable={false}
label={t('unchecked weight for this call')}
labelExtra={
<Toggle
className='sudoToggle'
label={t('with weight override')}
onChange={toggleWithWeight}
value={withWeight}
/>
}
onChange={_onChangeWeight}
value={weight}
<Toggle
className='sudoToggle'
label={t('with weight override')}
onChange={toggleWithWeight}
value={withWeight}
/>
)}
<Button.Group>
<TxButton
accountId={sudoKey}
extrinsic={extrinsic}
icon='sign-in-alt'
isDisabled={!method || (withWeight ? weight.eq(BN_ZERO) : false)}
isDisabled={!extrinsic}
label={
withWeight
? t('Submit Sudo Unchecked')
: t('Submit Sudo')
}
params={
withWeight
? [method, weight]
: [method]
}
tx={
withWeight
? api.tx.sudo.sudoUncheckedWeight
: api.tx.sudo.sudo
}
/>
</Button.Group>
</StyledSection>
Expand All @@ -100,6 +78,7 @@ const StyledSection = styled.section`
.sudoToggle {
width: 100%;
text-align: right;
padding-top: 1rem;
}
`;

Expand Down