Skip to content

Commit 2647884

Browse files
committed
SD-98: Add loading states and mutation tracking to token list buttons
1 parent c9b86e7 commit 2647884

File tree

8 files changed

+49
-22
lines changed

8 files changed

+49
-22
lines changed

src/domains/misc/components/Layout/TopBar/NavBox.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ export const BrandCanvas = styled(Canvas)`
105105
export const UserCanvas = styled(Canvas)`
106106
justify-content: end;
107107
padding: 0;
108-
padding-inline: ${vars('--spacing-s')};
109108
border: 1px solid transparent;
109+
padding-inline: ${vars('--spacing-s')};
110110
transition: all ${transitionTime};
111-
height: 100%;
112111
113112
@media (width <= ${BREAKPOINTS.lg}) { /* stylelint-disable-line media-query-no-invalid */
114113
border-bottom-left-radius: ${vars('--spacing-l')};

src/domains/misc/components/Layout/TopBar/TopBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ const AccountIcon = styled(UserIcon)`
9696
`;
9797

9898
const Divider = styled.div`
99+
height: 54px;
99100
width: 1px;
100101
background: ${vars('--color-neutral-stroke-2-rest')};
101-
height: 54px;
102102
`;
103103

104104
const PowerButton = styled(Button)`

src/domains/misc/utils/getQueryKey.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import type { Address } from 'viem';
2+
3+
import { NetworkEnvironment } from 'src/domains/chains/types/misc';
4+
15
export const QUERY_KEYS = {
26
shielderFees: 'shielderFees',
37
shielderClient: 'shielder-client',
@@ -11,9 +15,10 @@ export const QUERY_KEYS = {
1115
wasmCryptoClient: 'wasm-crypto-client',
1216
} as const;
1317

14-
import type { Address } from 'viem';
15-
16-
import { NetworkEnvironment } from 'src/domains/chains/types/misc';
18+
export const MUTATION_KEYS = {
19+
shield: 'shield',
20+
withdraw: 'withdraw',
21+
} as const;
1722

1823
const getQueryKey = {
1924
shielderFees: (walletAddress: Address, chainId: string) => [

src/domains/shielder/components/TokenList/TokenListItem.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { useIsMutating } from '@tanstack/react-query';
12
import styled from 'styled-components';
23

34
import Button from 'src/domains/misc/components/Button';
45
import { useModal } from 'src/domains/misc/components/Modal';
56
import Skeleton from 'src/domains/misc/components/Skeleton';
67
import TokenIcon from 'src/domains/misc/components/TokenIcon';
8+
import { MUTATION_KEYS } from 'src/domains/misc/utils/getQueryKey.ts';
79
import isPresent from 'src/domains/misc/utils/isPresent';
810
import formatBalance from 'src/domains/numbers/utils/formatBalance';
911
import useShielderStore from 'src/domains/shielder/stores/shielder';
@@ -37,11 +39,32 @@ const TokenListItem = ({ token }: Props) => {
3739

3840
const selectedToken = { ...token, symbol, decimals, balance: activeBalance };
3941

40-
const { open: openShieldModal } = useModal(
41-
42-
);
42+
const { open: openShieldModal } = useModal();
4343
const { open: openSendModal } = useModal();
4444

45+
const isShielding = !!useIsMutating({
46+
predicate: mutation => {
47+
const variables = mutation.state.variables as { token: Token } | undefined;
48+
if (!variables?.token) return false;
49+
return mutation.options.mutationKey?.[0] === MUTATION_KEYS.shield &&
50+
variables.token.address === token.address &&
51+
variables.token.isNative === token.isNative;
52+
},
53+
});
54+
55+
const isWithdrawing = !!useIsMutating({
56+
predicate: mutation => {
57+
const variables = mutation.state.variables as { token: Token } | undefined;
58+
if (!variables?.token) return false;
59+
return mutation.options.mutationKey?.[0] === MUTATION_KEYS.withdraw &&
60+
variables.token.address === token.address &&
61+
variables.token.isNative === token.isNative;
62+
},
63+
});
64+
65+
const isProcessing = isShielding || isWithdrawing;
66+
const processingText = isShielding ? 'Shielding' : isWithdrawing ? 'Sending privately' : undefined;
67+
4568
return (
4669
<Container>
4770
<TokenIcon size={40} Icon={token.icon} />
@@ -60,21 +83,23 @@ const TokenListItem = ({ token }: Props) => {
6083
<Button
6184
variant="primary"
6285
size="small"
86+
isLoading={isProcessing}
6387
leftIcon="Shielded"
64-
disabled={isDisabled}
88+
disabled={isDisabled || isProcessing}
6589
onClick={() => void openShieldModal(<ShieldModal token={selectedToken} />)}
6690
>
67-
Shield
91+
{processingText ?? 'Shield'}
6892
</Button>
6993
) : (
7094
<Button
7195
variant="primary"
7296
size="small"
7397
leftIcon="ArrowUpRight"
74-
disabled={isDisabled}
98+
isLoading={isProcessing}
99+
disabled={isDisabled || isProcessing}
75100
onClick={() => void openSendModal(<SendModal token={selectedToken} />)}
76101
>
77-
Send
102+
{processingText ?? 'Send'}
78103
</Button>
79104
)}
80105
</Container>

src/domains/shielder/components/Welcome/Welcome.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import styled from 'styled-components';
33
import ConnectModal from 'src/domains/chains/components/ConnectModal';
44
import Button from 'src/domains/misc/components/Button';
55
import DoubleBorderBox from 'src/domains/misc/components/DoubleBorderBox';
6+
import { useModal } from 'src/domains/misc/components/Modal';
67
import { typography } from 'src/domains/styling/utils/tokens';
7-
import vars from 'src/domains/styling/utils/vars.ts';
8-
9-
import { useModal } from '../../../misc/components/Modal';
8+
import vars from 'src/domains/styling/utils/vars';
109

1110
import breakTheTrace from './breakTheTrace.png';
1211

src/domains/shielder/utils/useActivityHistory.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ export const useActivityHistory = () => {
1313

1414
const isReady = !!address && !!chainId;
1515

16-
const db = useMemo(
17-
() => (address ? getLocalShielderActivityHistoryIndexedDB(address) : undefined),
18-
[address]
19-
);
16+
const db = address ? getLocalShielderActivityHistoryIndexedDB(address) : undefined;
2017

2118
const upsertTransaction = useCallback(
2219
async (...params: Parameters<NonNullable<typeof db>['upsertItem']>) => {

src/domains/shielder/utils/useShield.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useAccount, usePublicClient, useSendTransaction, useWalletClient } from
88
import { Token } from 'src/domains/chains/types/misc';
99
import useChain from 'src/domains/chains/utils/useChain';
1010
import { useToast } from 'src/domains/misc/components/Toast';
11-
import getQueryKey from 'src/domains/misc/utils/getQueryKey';
11+
import getQueryKey, { MUTATION_KEYS } from 'src/domains/misc/utils/getQueryKey';
1212
import { Fee } from 'src/domains/shielder/stores/getShielderIndexedDB';
1313
import { useActivityHistory } from 'src/domains/shielder/utils/useActivityHistory';
1414
import useActivityModal from 'src/domains/shielder/utils/useActivityModal';
@@ -92,6 +92,7 @@ const useShield = () => {
9292
};
9393

9494
const { mutateAsync: shield, isPending: isShielding, ...meta } = useMutation({
95+
mutationKey: [MUTATION_KEYS.shield],
9596
mutationFn: async ({
9697
token,
9798
amount,

src/domains/shielder/utils/useWithdraw.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useAccount } from 'wagmi';
66

77
import { Token } from 'src/domains/chains/types/misc';
88
import { useToast } from 'src/domains/misc/components/Toast';
9-
import getQueryKey from 'src/domains/misc/utils/getQueryKey';
9+
import getQueryKey, { MUTATION_KEYS } from 'src/domains/misc/utils/getQueryKey';
1010
import { Fee } from 'src/domains/shielder/stores/getShielderIndexedDB';
1111
import { useActivityHistory } from 'src/domains/shielder/utils/useActivityHistory';
1212
import useActivityModal from 'src/domains/shielder/utils/useActivityModal';
@@ -27,6 +27,7 @@ const useWithdraw = () => {
2727
isPending: isWithdrawing,
2828
...meta
2929
} = useMutation({
30+
mutationKey: [MUTATION_KEYS.withdraw],
3031
mutationFn: async ({
3132
token,
3233
amount,

0 commit comments

Comments
 (0)