Skip to content

Commit a1e5f18

Browse files
bug: select active account and remove fiat currency (namada-net#632)
* fix: select account with balance * feat: remove fiat currency info * fix: code review
1 parent d1fb1dd commit a1e5f18

9 files changed

Lines changed: 25 additions & 255 deletions

File tree

apps/namada-interface/src/App/AccountOverview/AccountOverview.tsx

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import BigNumber from "bignumber.js";
21
import { useState } from "react";
32
import { useNavigate } from "react-router-dom";
43

@@ -8,8 +7,7 @@ import {
87
useIntegrationConnection,
98
useUntilIntegrationAttached,
109
} from "@namada/integrations";
11-
import { Account, Chain, ExtensionKey, Extensions } from "@namada/types";
12-
import { formatCurrency } from "@namada/utils";
10+
import { Account, Chain, ExtensionKey, Extensions, TokenType, Tokens } from "@namada/types";
1311
import { TopLevelRoute } from "App/types";
1412
import { AccountsState, addAccounts, fetchBalances } from "slices/accounts";
1513
import { setIsConnected } from "slices/settings";
@@ -27,34 +25,15 @@ import {
2725
} from "./AccountOverview.components";
2826
import { DerivedAccounts } from "./DerivedAccounts";
2927

30-
import { atom, useAtomValue, useSetAtom } from "jotai";
31-
import { loadable } from "jotai/utils";
28+
import { useAtomValue, useSetAtom } from "jotai";
3229
import { accountsAtom, balancesAtom } from "slices/accounts";
33-
import { balanceToFiatAtom } from "slices/coins";
34-
import { fiatCurrencyAtom } from "slices/settings";
30+
import BigNumber from "bignumber.js";
3531

3632
//TODO: move to utils when we have one
3733
const isEmptyObject = (object: Record<string, unknown>): boolean => {
3834
return object ? Object.keys(object).length === 0 : true;
3935
};
4036

41-
const totalBalanceAtom = atom(async (get) => {
42-
const accounts = await get(accountsAtom);
43-
const balances = get(balancesAtom);
44-
const balanceToFiat = await get(balanceToFiatAtom);
45-
const fiatCurrency = get(fiatCurrencyAtom);
46-
47-
return accounts.reduce((acc, account) => {
48-
const balance = balances[account.address];
49-
50-
if (typeof balance === "undefined") {
51-
return acc;
52-
} else {
53-
return acc.plus(balanceToFiat(balance, fiatCurrency));
54-
}
55-
}, new BigNumber(0));
56-
});
57-
5837
export const AccountOverview = (): JSX.Element => {
5938
const navigate = useNavigate();
6039
const dispatch = useAppDispatch();
@@ -72,8 +51,6 @@ export const AccountOverview = (): JSX.Element => {
7251

7352
const { derived } = useAppSelector<AccountsState>((state) => state.accounts);
7453

75-
const fiatCurrency = useAtomValue(fiatCurrencyAtom);
76-
7754
const [integration, isConnectingToExtension, withConnection] =
7855
useIntegrationConnection(chains.namada.id);
7956
const extensionAlias = Extensions[chain.extension.id].alias;
@@ -82,17 +59,15 @@ export const AccountOverview = (): JSX.Element => {
8259
const currentExtensionAttachStatus =
8360
extensionAttachStatus[chain.extension.id];
8461

85-
const total = useAtomValue(loadable(totalBalanceAtom));
86-
87-
const totalDisplayString = formatCurrency(
88-
fiatCurrency,
89-
total.state === "hasData" ? total.data : new BigNumber(0)
90-
);
91-
9262
const handleDownloadExtension = (url: string): void => {
9363
window.open(url, "_blank", "noopener,noreferrer");
9464
};
9565

66+
const balances = useAtomValue(balancesAtom);
67+
const totalNativeBalance = Object.values(balances).reduce((acc, balance) => {
68+
return acc.plus(balance[chain.currency.symbol as TokenType] || BigNumber(0));
69+
}, BigNumber(0));
70+
9671
const handleConnectExtension = async (): Promise<void> => {
9772
withConnection(
9873
async () => {
@@ -133,8 +108,8 @@ export const AccountOverview = (): JSX.Element => {
133108
<TotalContainer>
134109
{!isEmptyObject(derived[chain.id]) && (
135110
<TotalAmount>
136-
<TotalAmountFiat>{fiatCurrency}</TotalAmountFiat>
137-
<TotalAmountValue>{totalDisplayString}</TotalAmountValue>
111+
<TotalAmountFiat>{Tokens[chain.currency.symbol as TokenType].symbol}</TotalAmountFiat>
112+
<TotalAmountValue>{totalNativeBalance.toString()}</TotalAmountValue>
138113
</TotalAmount>
139114
)}
140115
</TotalContainer>

apps/namada-interface/src/App/AccountOverview/DerivedAccounts/DerivedAccounts.tsx

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import { ThemeContext } from "styled-components";
33

44
import { chains } from "@namada/chains";
55
import { TokenType, Tokens } from "@namada/types";
6-
import { formatCurrency } from "@namada/utils";
76

87
import {
98
DerivedAccountAlias,
10-
DerivedAccountBalance,
119
DerivedAccountContainer,
1210
DerivedAccountInfo,
1311
DerivedAccountItem,
@@ -30,12 +28,7 @@ import AssetNamadaNamDark from "./assets/asset-namada-nam-dark.png";
3028
import AssetNamadaNamLight from "./assets/asset-namada-nam-light.png";
3129
import AssetPolkadot from "./assets/asset-polkadot-dot.png";
3230

33-
import { Balance } from "slices/accounts";
34-
import { balanceToFiatAtom, coinsAtom } from "slices/coins";
35-
import { SettingsState } from "slices/settings";
36-
import { useAppSelector } from "store";
37-
38-
import { useAtomValue, useSetAtom } from "jotai";
31+
import { useAtomValue } from "jotai";
3932
import { loadable } from "jotai/utils";
4033
import { accountsAtom, balancesAtom } from "slices/accounts";
4134

@@ -70,23 +63,6 @@ const assetIconByToken: Record<TokenType, { light: string; dark: string }> = {
7063
},
7164
};
7265

73-
const FiatBalanceDisplay: React.FC<{
74-
balance?: Balance;
75-
}> = ({ balance }) => {
76-
const { fiatCurrency } = useAppSelector<SettingsState>(
77-
(state) => state.settings
78-
);
79-
80-
const balanceToFiat = useAtomValue(loadable(balanceToFiatAtom));
81-
82-
const displayString =
83-
typeof balance !== "undefined" && balanceToFiat.state === "hasData"
84-
? formatCurrency(fiatCurrency, balanceToFiat.data(balance, fiatCurrency))
85-
: `${fiatCurrency} -`;
86-
87-
return <DerivedAccountBalance>{displayString}</DerivedAccountBalance>;
88-
};
89-
9066
const DerivedAccounts = (): JSX.Element => {
9167
const accountsLoadable = useAtomValue(loadable(accountsAtom));
9268
const accounts =
@@ -97,8 +73,6 @@ const DerivedAccounts = (): JSX.Element => {
9773
const themeContext = useContext(ThemeContext);
9874
const [activeAccountAddress, setActiveAccountAddress] = useState("");
9975

100-
const fetchConversionRates = useSetAtom(coinsAtom);
101-
10276
const chain = chains.namada;
10377
const { alias } = chain || {};
10478

@@ -118,9 +92,7 @@ const DerivedAccounts = (): JSX.Element => {
11892
setActiveAccountAddress(accounts[0]?.address);
11993
}, [accounts]);
12094

121-
useEffect(() => {
122-
fetchConversionRates();
123-
}, []);
95+
const symbol = chain.currency.symbol as TokenType;
12496

12597
return (
12698
<DerivedAccountsContainer>
@@ -135,6 +107,7 @@ const DerivedAccounts = (): JSX.Element => {
135107
.map((account) => {
136108
const { alias, address, isShielded } = account;
137109
const balance = balances[address];
110+
const nativeBalance = typeof balance === "undefined" ? "-" : (balance[symbol]?.toString() || "0");
138111

139112
return (
140113
<DerivedAccountItem key={address}>
@@ -151,7 +124,11 @@ const DerivedAccounts = (): JSX.Element => {
151124
)}
152125
</DerivedAccountType>
153126
</DerivedAccountInfo>
154-
<FiatBalanceDisplay balance={balance} />
127+
<div className="flex items-center">
128+
<span className="text-white">
129+
{Tokens[symbol].symbol} {nativeBalance}
130+
</span>
131+
</div>
155132
</DerivedAccountContainer>
156133
{balance && (
157134
<TokenTotals

apps/namada-interface/src/App/Settings/SettingsWalletSettings/SettingsWalletSettings.tsx

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,20 @@
1-
import { Currencies } from "currencies";
21
import { useNavigate } from "react-router-dom";
3-
import { setFiatCurrency } from "slices/settings";
4-
import { useAppDispatch, useAppSelector } from "store";
52

6-
import {
7-
Heading,
8-
Icon,
9-
NavigationContainer,
10-
Option,
11-
Select,
12-
Tooltip,
13-
} from "@namada/components";
14-
import { InputContainer } from "App/AccountOverview/AccountOverview.components";
3+
import { Heading, Icon, NavigationContainer } from "@namada/components";
154
import { BackButton } from "App/Token/TokenSend/TokenSendForm.components";
165
import { ButtonsContainer, SettingsContent } from "../Settings.components";
176
import { SettingsWalletSettingsContainer } from "./SettingsWalletSettings.components";
187

198
export const SettingsWalletSettings = (): JSX.Element => {
20-
const dispatch = useAppDispatch();
219
const navigate = useNavigate();
2210

23-
const currencies: Option<string>[] = Currencies.map((currency) => ({
24-
value: currency.currency,
25-
label: `${currency.currency} - ${currency.label}`,
26-
}));
27-
28-
const currentCurrency = useAppSelector(
29-
(state) => state.settings.fiatCurrency
30-
);
31-
32-
const handleCurrencySelect = (
33-
e: React.ChangeEvent<HTMLSelectElement>
34-
): void => {
35-
const { value } = e.target;
36-
37-
dispatch(setFiatCurrency(value));
38-
};
39-
4011
return (
4112
<SettingsWalletSettingsContainer>
4213
<NavigationContainer>
4314
<Heading level="h1">Wallet Settings</Heading>
4415
</NavigationContainer>
4516

46-
<SettingsContent>
47-
<InputContainer>
48-
<Select
49-
data={currencies}
50-
label={
51-
<div>
52-
Fiat Currency
53-
<Tooltip
54-
anchor={<Icon name="Info" />}
55-
tooltipText="Fiat currency in which balances may be displayed."
56-
/>
57-
</div>
58-
}
59-
value={currentCurrency}
60-
onChange={handleCurrencySelect}
61-
></Select>
62-
</InputContainer>
63-
</SettingsContent>
17+
<SettingsContent></SettingsContent>
6418
<ButtonsContainer>
6519
<BackButton
6620
onClick={() => {

apps/namada-interface/src/App/Token/TokenSend/TokenSend.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ const accountsWithBalanceIntoSelectData = (
5353
): Option<string>[] =>
5454
accountsWithBalance.flatMap(({ details, balance }) =>
5555
Object.entries(balance)
56-
.filter(
57-
([tokenType, balance]) =>
58-
!Tokens[tokenType as TokenType].isNut && balance.isGreaterThan(0)
59-
)
6056
.map(([tokenType, amount]) => ({
6157
value: `${details.address}|${tokenType}`,
6258
label: `${details.alias} ${amount} (${Tokens[tokenType as TokenType].symbol})`,
@@ -72,10 +68,11 @@ const TokenSend = (): JSX.Element => {
7268

7369
const shieldedAccountsWithBalance = accounts.filter(
7470
({ details }) => details.isShielded
75-
);
71+
).filter(({ balance }) => Object.values(balance).some((amount) => amount.isGreaterThan(0)));
72+
7673
const transparentAccountsWithBalance = accounts.filter(
7774
({ details }) => !details.isShielded
78-
);
75+
).filter(({ balance }) => Object.values(balance).some((amount) => amount.isGreaterThan(0)));
7976

8077
const shieldedTokenData = accountsWithBalanceIntoSelectData(
8178
shieldedAccountsWithBalance

apps/namada-interface/src/slices/StakingAndGovernance/fakeData.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
import BigNumber from "bignumber.js";
22

3-
import { Validator, StakingPosition, MyBalanceEntry } from "./types";
3+
import { MyBalanceEntry, StakingPosition, Validator } from "./types";
44
export const myBalancesData: MyBalanceEntry[] = [
55
{
66
uuid: "1",
77
key: "Total Balance",
88
baseCurrency: "NAM 33.00",
9-
fiatCurrency: "EUR 33.00",
109
},
1110
{
1211
uuid: "2",
1312
key: "Total Bonded",
1413
baseCurrency: "NAM 10.00",
15-
fiatCurrency: "EUR 10.00",
1614
},
1715
{
1816
uuid: "3",
1917
key: "Pending Rewards",
2018
baseCurrency: "NAM 23.00",
21-
fiatCurrency: "EUR 23.00",
2219
},
2320
{
2421
uuid: "4",
2522
key: "Available for Bonding",
2623
baseCurrency: "NAM 10.00",
27-
fiatCurrency: "EUR 10.00",
2824
},
2925
];
3026

apps/namada-interface/src/slices/StakingAndGovernance/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export type MyValidators = Unique & {
6363
export type MyBalanceEntry = Unique & {
6464
key: string;
6565
baseCurrency: string;
66-
fiatCurrency: string;
6766
};
6867

6968
// PLACEHOLDER

0 commit comments

Comments
 (0)