Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MASBalance component to use balance from account store and s… #508

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 2 additions & 15 deletions src/lib/ConnectMassaWallets/components/MASBalance.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,18 +9,7 @@ import { massaToken } from '../../massa-react/utils/const';
import { formatAmount } from '../../util/parseAmount';

export function MASBalance() {
const [balance, setBalance] = useState<bigint>();

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;

Expand Down
17 changes: 15 additions & 2 deletions src/lib/ConnectMassaWallets/store/accountStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Wallet, WalletName } from '@massalabs/wallet-provider';

export interface AccountStoreState {
connectedAccount?: Provider;
balance?: bigint;
accounts?: Provider[];
currentWallet?: Wallet;
wallets: Wallet[];
Expand All @@ -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<AccountStoreState>((set, get) => ({
accounts: undefined,
network: undefined,
connectedAccount: undefined,
balance: undefined,
accountObserver: undefined,
networkObserver: undefined,
currentWallet: undefined,
Expand Down Expand Up @@ -78,7 +81,7 @@ export const useAccountStore = create<AccountStoreState>((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[]) => {
Expand All @@ -88,14 +91,24 @@ export const useAccountStore = create<AccountStoreState>((set, get) => ({
}
},

setConnectedAccount: (connectedAccount?: Provider) => {
setConnectedAccount: async (connectedAccount?: Provider) => {
set({ connectedAccount });
if (!connectedAccount) return;
const balance = await connectedAccount.balance(false);
set({ balance });
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
},

setCurrentNetwork: (network: Network) => {
if (network === get().network) return;
set({ network });
},

refreshBalance: async (final: boolean) => {
const { connectedAccount } = get();
if (!connectedAccount) return;
const balance = await connectedAccount.balance(final);
set({ balance });
},
}));

function resetObservers(
Expand Down
Loading