diff --git a/package-lock.json b/package-lock.json index 18c9173f..ea509d7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@massalabs/react-ui-kit", - "version": "1.0.1", + "version": "1.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@massalabs/react-ui-kit", - "version": "1.0.1", + "version": "1.0.2", "dependencies": { "@headlessui/react": "^1.7.15", "@massalabs/massa-web3": "^5.1.1", diff --git a/package.json b/package.json index 38f9eaff..20d768cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@massalabs/react-ui-kit", - "version": "1.0.1", + "version": "1.0.2", "type": "module", "files": ["src", "presets"], "main": "src/index.ts", diff --git a/src/lib/ConnectMassaWallets/components/MASBalance.tsx b/src/lib/ConnectMassaWallets/components/MASBalance.tsx index 8fa2fc9e..e155134f 100644 --- a/src/lib/ConnectMassaWallets/components/MASBalance.tsx +++ b/src/lib/ConnectMassaWallets/components/MASBalance.tsx @@ -1,8 +1,6 @@ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore -import React from 'react'; - -import { useEffect, useState } from 'react'; +import React, { useEffect } from 'react'; import Intl from '../i18n'; import { useAccountStore } from '../store'; @@ -11,18 +9,7 @@ import { massaToken } from '../../massa-react/utils/const'; import { formatAmount } from '../../util/parseAmount'; export function MASBalance() { - const [balance, setBalance] = useState(); - - const { connectedAccount, currentWallet, network } = useAccountStore(); - - useEffect(() => { - if (!connectedAccount) return; - const fetchBalance = async () => { - const balance = await connectedAccount.balance(false); - setBalance(balance); - }; - fetchBalance(); - }, [connectedAccount, setBalance, currentWallet, network]); + const { balance } = useAccountStore(); const formattedBalance = formatAmount(balance?.toString() || '0', 9).full; diff --git a/src/lib/ConnectMassaWallets/store/accountStore.ts b/src/lib/ConnectMassaWallets/store/accountStore.ts index 29e28527..827ae99f 100644 --- a/src/lib/ConnectMassaWallets/store/accountStore.ts +++ b/src/lib/ConnectMassaWallets/store/accountStore.ts @@ -4,6 +4,7 @@ import { Wallet, WalletName } from '@massalabs/wallet-provider'; export interface AccountStoreState { connectedAccount?: Provider; + balance?: bigint; accounts?: Provider[]; currentWallet?: Wallet; wallets: Wallet[]; @@ -20,12 +21,14 @@ export interface AccountStoreState { setWallets: (wallets: Wallet[]) => void; setConnectedAccount: (account?: Provider) => void; setCurrentNetwork: (network: Network) => void; + refreshBalance: (final: boolean) => void; } export const useAccountStore = create((set, get) => ({ accounts: undefined, network: undefined, connectedAccount: undefined, + balance: undefined, accountObserver: undefined, networkObserver: undefined, currentWallet: undefined, @@ -78,7 +81,7 @@ export const useAccountStore = create((set, get) => ({ setAccounts: async (wallet: Wallet, account?: Provider) => { const accounts = await wallet.accounts(); set({ accounts }); - set({ connectedAccount: account || accounts[0] }); + get().setConnectedAccount(account || accounts[0]); }, setWallets: (wallets: Wallet[]) => { @@ -88,16 +91,33 @@ export const useAccountStore = create((set, get) => ({ } }, - setConnectedAccount: (connectedAccount?: Provider) => { + setConnectedAccount: async (connectedAccount?: Provider) => { set({ connectedAccount }); + if (!connectedAccount) return; + setBalance(connectedAccount, false, set); }, setCurrentNetwork: (network: Network) => { if (network === get().network) return; set({ network }); }, + + refreshBalance: async (final: boolean) => { + const { connectedAccount } = get(); + if (!connectedAccount) return; + setBalance(connectedAccount, final, set); + }, })); +async function setBalance( + provider: Provider, + final: boolean, + set: (partial: Partial) => void, +) { + const balance = await provider.balance(final); + set({ balance }); +} + function resetObservers( get: () => AccountStoreState, set: (partial: Partial) => void,