-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathBalancesTableContainer.tsx
210 lines (178 loc) · 6.74 KB
/
BalancesTableContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {
ChevronDown,
ChevronUp,
CoinsStackedLineIcon,
SendPlanLineIcon,
} from '@webb-tools/icons';
import useNetworkStore from '@webb-tools/tangle-shared-ui/context/useNetworkStore';
import useApiRx from '@webb-tools/tangle-shared-ui/hooks/useApiRx';
import useLocalStorage, {
LocalStorageKey,
} from '@webb-tools/tangle-shared-ui/hooks/useLocalStorage';
import useSubstrateAddress from '@webb-tools/tangle-shared-ui/hooks/useSubstrateAddress';
import {
Card,
CardVariant,
InfoIconWithTooltip,
Typography,
} from '@webb-tools/webb-ui-components';
import { FC, useCallback, useEffect, useState } from 'react';
import TangleTokenIcon from '../../components/TangleTokenIcon';
import useBalances from '../../data/balances/useBalances';
import useVestingInfo from '../../data/vesting/useVestingInfo';
import { StaticSearchQueryPath } from '../../types';
import TransferTxModal from '../TransferTxModal';
import BalanceAction from './BalanceAction';
import BalanceCell from './BalanceCell';
import HeaderCell from './HeaderCell';
import LockedBalanceDetails from './LockedBalanceDetails/LockedBalanceDetails';
import useLongestVestingScheduleTime from './useLongestVestingScheduleTime';
import VestBalanceAction from './VestBalanceAction';
const BalancesTableContainer: FC = () => {
const [isTransferModalOpen, setIsTransferModalOpen] = useState(false);
const [isDetailsCollapsed, setIsDetailsCollapsed] = useState(false);
const { locked, transferable } = useBalances();
const activeSubstrateAddress = useSubstrateAddress();
const longestVestingScheduleTime = useLongestVestingScheduleTime();
const { hasClaimableTokens: hasVestedAmount, claimableAmount: vestedAmount } =
useVestingInfo();
const {
set: setCachedIsDetailsCollapsed,
valueOpt: cachedIsDetailsCollapsedOpt,
} = useLocalStorage(LocalStorageKey.IS_BALANCES_TABLE_DETAILS_COLLAPSED);
const { result: locks } = useApiRx(
useCallback(
(api) => {
if (!activeSubstrateAddress) {
return null;
}
return api.query.balances.locks(activeSubstrateAddress);
},
[activeSubstrateAddress],
),
);
// Load the cached collapsed state from local storage on mount.
useEffect(() => {
if (
cachedIsDetailsCollapsedOpt !== null &&
cachedIsDetailsCollapsedOpt.value !== null
) {
setIsDetailsCollapsed(cachedIsDetailsCollapsedOpt.value);
}
}, [cachedIsDetailsCollapsedOpt]);
const handleToggleDetails = useCallback(() => {
setIsDetailsCollapsed((prev) => {
const newValue = !prev;
setCachedIsDetailsCollapsed(newValue);
return newValue;
});
}, [setCachedIsDetailsCollapsed]);
const hasLocks = locks !== null && locks.length > 0;
return (
<>
<Card variant={CardVariant.GLASS} className="overflow-x-auto">
<div className="flex flex-row min-w-[630px]">
{/* Asset column */}
<div className="flex flex-col justify-between w-full">
<HeaderCell title="Asset" />
<AssetCell
title="Transferable Balance"
tooltip="The amount of tokens you can freely transfer right now. These tokens are not subject to any limitations."
/>
{hasVestedAmount && (
<AssetCell
title="Claimable Vested Balance"
tooltip="The amount of tokens that has vested from vesting schedules and is now available to be claimed. This is only a portion of the total vested balance, which can be released, but the rest may still be locked and subject to vesting schedules."
/>
)}
<AssetCell
title="Locked Balance"
tooltip="The total tokens subject to limitations, such as those locked in staking, democracy participation, or undergoing vesting."
/>
</div>
{/* Balance column */}
<div className="flex flex-col w-full">
<HeaderCell title="Balance" />
{/* Transferable balance */}
<div className="flex flex-row justify-between">
<BalanceCell amount={transferable} />
<div className="flex flex-row items-center gap-1 p-3">
<BalanceAction
Icon={SendPlanLineIcon}
tooltip="Send"
isDisabled={transferable === null || transferable.eqn(0)}
onClick={() => setIsTransferModalOpen(true)}
/>
<BalanceAction
Icon={CoinsStackedLineIcon}
tooltip="Nominate"
isDisabled={transferable === null || transferable.eqn(0)}
internalHref={StaticSearchQueryPath.NominationsTable}
/>
</div>
</div>
{/* Vested balance */}
{hasVestedAmount && (
<div className="flex flex-row items-center justify-between">
<BalanceCell
status={longestVestingScheduleTime ?? undefined}
amount={vestedAmount}
/>
<div className="p-3">
<VestBalanceAction />
</div>
</div>
)}
{/* Locked balance */}
<div className="flex flex-row items-center justify-between">
<BalanceCell amount={locked} />
{hasLocks && (
<div className="p-3">
<BalanceAction
Icon={isDetailsCollapsed ? ChevronDown : ChevronUp}
onClick={handleToggleDetails}
tooltip={`${
isDetailsCollapsed ? 'Show' : 'Collapse'
} Details`}
/>
</div>
)}
</div>
</div>
</div>
{hasLocks && !isDetailsCollapsed && <LockedBalanceDetails />}
</Card>
<TransferTxModal
isModalOpen={isTransferModalOpen}
setIsModalOpen={setIsTransferModalOpen}
/>
</>
);
};
/** @internal */
const AssetCell: FC<{
title: string;
tooltip?: string;
}> = ({ title, tooltip }) => {
const { nativeTokenSymbol } = useNetworkStore();
return (
<div className="flex gap-6 px-3 py-3">
<div className="flex flex-row items-center gap-1">
<TangleTokenIcon size="lg" />
<Typography variant="h5" className="text-mono-160 dark:text-mono-80">
{nativeTokenSymbol}
</Typography>
</div>
<div className="flex gap-1">
<Typography
variant="h5"
className="whitespace-nowrap text-mono-160 dark:text-mono-80"
>
{title}
</Typography>
{tooltip !== undefined && <InfoIconWithTooltip content={tooltip} />}
</div>
</div>
);
};
export default BalancesTableContainer;