|
1 | 1 | // Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -// @ts-nocheck |
5 | 4 | /* eslint-disable react/jsx-max-props-per-line */
|
6 | 5 |
|
| 6 | +import type { ApiPromise } from '@polkadot/api'; |
7 | 7 | import type { Balance } from '@polkadot/types/interfaces';
|
| 8 | +import type { BN } from '@polkadot/util'; |
8 | 9 | import type { Proxy, ProxyItem, TxInfo } from '../../../util/types';
|
| 10 | +import type { DelegationInfo } from '../utils/types'; |
| 11 | +import type { ModifyModes } from './modify/ModifyDelegate'; |
9 | 12 |
|
10 |
| -import { Close as CloseIcon } from '@mui/icons-material'; |
11 |
| -import { Grid, Typography, useTheme } from '@mui/material'; |
12 | 13 | import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
13 | 14 |
|
14 |
| -import { ApiPromise } from '@polkadot/api'; |
15 | 15 | import { AccountsStore } from '@polkadot/extension-base/stores';
|
16 | 16 | import keyring from '@polkadot/ui-keyring';
|
17 |
| -import { BN, BN_ONE } from '@polkadot/util'; |
| 17 | +import { BN_ONE, noop } from '@polkadot/util'; |
18 | 18 | import { cryptoWaitReady } from '@polkadot/util-crypto';
|
19 | 19 |
|
20 | 20 | import { useAccountLocks, useBalances, useInfo, useProxies, useTracks, useTranslation } from '../../../hooks';
|
21 | 21 | import { PROXY_TYPE } from '../../../util/constants';
|
| 22 | +import SimpleModalTitle from '../../partials/SimpleModalTitle'; |
22 | 23 | import { DraggableModal } from '../components/DraggableModal';
|
23 | 24 | import SelectProxyModal2 from '../components/SelectProxyModal2';
|
24 | 25 | import WaitScreen from '../partials/WaitScreen';
|
25 |
| -import { DelegationInfo } from '../utils/types'; |
26 | 26 | import { getMyDelegationInfo } from '../utils/util';
|
27 | 27 | import ChooseDelegator from './delegate/ChooseDelegator';
|
28 | 28 | import DelegateVote from './delegate/Delegate';
|
29 |
| -import { ModifyModes } from './modify/ModifyDelegate'; |
30 | 29 | import { getAlreadyLockedValue } from './partial/AlreadyLockedTooltipText';
|
31 | 30 | import Confirmation from './partial/Confirmation';
|
32 | 31 | import About from './About';
|
@@ -76,12 +75,12 @@ export const STEPS = {
|
76 | 75 |
|
77 | 76 | export type DelegationStatus = 'Delegate' | 'Remove' | 'Modify';
|
78 | 77 |
|
79 |
| -export function Delegate({ address, open, setOpen, showDelegationNote }: Props): React.ReactElement<Props> { |
| 78 | +export function Delegate ({ address, open, setOpen, showDelegationNote }: Props): React.ReactElement<Props> { |
80 | 79 | const { t } = useTranslation();
|
81 |
| - const theme = useTheme(); |
82 | 80 | const { api, formatted } = useInfo(address);
|
83 | 81 | const tracksList = useTracks(address);
|
84 | 82 | const balances = useBalances(address, undefined, undefined, true);
|
| 83 | + |
85 | 84 | const lockedAmount = useMemo(() => getAlreadyLockedValue(balances), [balances]);
|
86 | 85 | const accountLocks = useAccountLocks(address, 'referenda', 'convictionVoting');
|
87 | 86 | const proxies = useProxies(api, formatted);
|
@@ -111,8 +110,64 @@ export function Delegate({ address, open, setOpen, showDelegationNote }: Props):
|
111 | 110 | setProxyStep(step);
|
112 | 111 | }, [step]);
|
113 | 112 |
|
| 113 | + const title = useMemo(() => { |
| 114 | + if (step === STEPS.ABOUT) { |
| 115 | + return t('Delegate Vote'); |
| 116 | + } |
| 117 | + |
| 118 | + if (step === STEPS.CHECK_SCREEN) { |
| 119 | + return t('Delegation status'); |
| 120 | + } |
| 121 | + |
| 122 | + if ([STEPS.INDEX, STEPS.CHOOSE_DELEGATOR].includes(step)) { |
| 123 | + return t('Delegate Vote ({{ step }}/3)', { replace: { step: step === STEPS.INDEX ? 1 : 2 } }); |
| 124 | + } |
| 125 | + |
| 126 | + if (step === STEPS.PREVIEW) { |
| 127 | + return t('Delegation details'); |
| 128 | + } |
| 129 | + |
| 130 | + if ([STEPS.REVIEW, STEPS.SIGN_QR].includes(step) && status === 'Delegate') { |
| 131 | + return t('Review Your Delegation (3/3)'); |
| 132 | + } |
| 133 | + |
| 134 | + if ([STEPS.REMOVE, STEPS.SIGN_QR].includes(step) && status === 'Remove') { |
| 135 | + return t('Remove Delegate'); |
| 136 | + } |
| 137 | + |
| 138 | + if ([STEPS.MODIFY, STEPS.SIGN_QR].includes(step) && status === 'Modify') { |
| 139 | + return t('Modify Delegate'); |
| 140 | + } |
| 141 | + |
| 142 | + if (step === STEPS.WAIT_SCREEN) { |
| 143 | + if (status === 'Delegate') { |
| 144 | + return t('Delegating'); |
| 145 | + } else if (status === 'Modify') { |
| 146 | + return t('Modifying Delegation'); |
| 147 | + } else { |
| 148 | + return t('Removing Delegation'); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (step === STEPS.CONFIRM) { |
| 153 | + if (status === 'Delegate') { |
| 154 | + return txInfo?.success ? t('Delegation Completed') : t('Delegation Failed'); |
| 155 | + } else if (status === 'Modify') { |
| 156 | + return txInfo?.success ? t('Delegations Modified') : t('Modifying Delegations Failed'); |
| 157 | + } else { |
| 158 | + return txInfo?.success ? t('Delegations Removed') : t('Removing Delegations Failed'); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if (step === STEPS.PROXY) { |
| 163 | + return t('Select Proxy'); |
| 164 | + } |
| 165 | + |
| 166 | + return ''; |
| 167 | + }, [status, step, t, txInfo?.success]); |
| 168 | + |
114 | 169 | const handleClose = useCallback(() => {
|
115 |
| - step !== STEPS.PROXY ? setOpen(false) : setStep(proxyStep); |
| 170 | + step !== STEPS.PROXY ? setOpen(false) : proxyStep && setStep(proxyStep); |
116 | 171 | }, [proxyStep, setOpen, step]);
|
117 | 172 |
|
118 | 173 | useEffect(() => {
|
@@ -148,7 +203,7 @@ export function Delegate({ address, open, setOpen, showDelegationNote }: Props):
|
148 | 203 |
|
149 | 204 | if (!api?.call?.['transactionPaymentApi']) {
|
150 | 205 | // eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
151 |
| - return setEstimatedFee(api?.createType('Balance', BN_ONE)); |
| 206 | + return setEstimatedFee(api?.createType('Balance', BN_ONE) as Balance); |
152 | 207 | }
|
153 | 208 |
|
154 | 209 | if (delegateInformation.delegatedTracks.length > 1) {
|
@@ -232,61 +287,11 @@ export function Delegate({ address, open, setOpen, showDelegationNote }: Props):
|
232 | 287 | return (
|
233 | 288 | <DraggableModal minHeight={550} onClose={handleClose} open={open}>
|
234 | 289 | <>
|
235 |
| - <Grid alignItems='center' container justifyContent='space-between' pt='5px'> |
236 |
| - <Grid item> |
237 |
| - <Typography fontSize='22px' fontWeight={700}> |
238 |
| - {step === STEPS.ABOUT && |
239 |
| - t('Delegate Vote') |
240 |
| - } |
241 |
| - {step === STEPS.CHECK_SCREEN && |
242 |
| - t('Delegation status') |
243 |
| - } |
244 |
| - {[STEPS.INDEX, STEPS.CHOOSE_DELEGATOR].includes(step) && |
245 |
| - t('Delegate Vote ({{ step }}/3)', { replace: { step: step === STEPS.INDEX ? 1 : 2 } }) |
246 |
| - } |
247 |
| - {step === STEPS.PREVIEW && |
248 |
| - t('Delegation details') |
249 |
| - } |
250 |
| - {[STEPS.REVIEW, STEPS.SIGN_QR].includes(step) && status === 'Delegate' && |
251 |
| - t('Review Your Delegation (3/3)') |
252 |
| - } |
253 |
| - {[STEPS.REMOVE, STEPS.SIGN_QR].includes(step) && status === 'Remove' && |
254 |
| - t('Remove Delegate') |
255 |
| - } |
256 |
| - {[STEPS.MODIFY, STEPS.SIGN_QR].includes(step) && status === 'Modify' && |
257 |
| - t('Modify Delegate') |
258 |
| - } |
259 |
| - {step === STEPS.WAIT_SCREEN |
260 |
| - ? status === 'Delegate' |
261 |
| - ? t('Delegating') |
262 |
| - : status === 'Modify' |
263 |
| - ? t('Modifying Delegation') |
264 |
| - : t('Removing Delegation') |
265 |
| - : undefined |
266 |
| - } |
267 |
| - {step === STEPS.CONFIRM |
268 |
| - ? status === 'Delegate' |
269 |
| - ? txInfo?.success |
270 |
| - ? t('Delegation Completed') |
271 |
| - : t('Delegation Failed') |
272 |
| - : status === 'Modify' |
273 |
| - ? txInfo?.success |
274 |
| - ? t('Delegations Modified') |
275 |
| - : t('Modifying Delegations Failed') |
276 |
| - : txInfo?.success |
277 |
| - ? t('Delegations Removed') |
278 |
| - : t('Removing Delegations Failed') |
279 |
| - : undefined |
280 |
| - } |
281 |
| - {step === STEPS.PROXY && |
282 |
| - t('Select Proxy') |
283 |
| - } |
284 |
| - </Typography> |
285 |
| - </Grid> |
286 |
| - <Grid item> |
287 |
| - {step !== STEPS.WAIT_SCREEN && <CloseIcon onClick={handleClose} sx={{ color: 'primary.main', cursor: 'pointer', stroke: theme.palette.primary.main, strokeWidth: 1.5 }} />} |
288 |
| - </Grid> |
289 |
| - </Grid> |
| 290 | + <SimpleModalTitle |
| 291 | + icon='vaadin:del' |
| 292 | + onClose={step !== STEPS.WAIT_SCREEN ? handleClose : noop} |
| 293 | + title= {title} |
| 294 | + /> |
290 | 295 | {step === STEPS.ABOUT &&
|
291 | 296 | <About
|
292 | 297 | setStep={setStep}
|
@@ -358,7 +363,7 @@ export function Delegate({ address, open, setOpen, showDelegationNote }: Props):
|
358 | 363 | <SelectProxyModal2
|
359 | 364 | address={address}
|
360 | 365 | // eslint-disable-next-line react/jsx-no-bind
|
361 |
| - closeSelectProxy={() => setStep(proxyStep)} |
| 366 | + closeSelectProxy={() => proxyStep && setStep(proxyStep)} |
362 | 367 | height={modalHeight}
|
363 | 368 | proxies={proxyItems}
|
364 | 369 | proxyTypeFilter={PROXY_TYPE.GOVERNANCE}
|
|
0 commit comments