Skip to content

Commit

Permalink
feat: show all balances in switcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdethier committed Jun 26, 2024
1 parent c606c8a commit dece0dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/common/AddressSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useNavigate } from "react-router-dom";

export default function AddressSwitcher() {
const { logout, accounts, authenticate, selectAddress } = useLogionChain();
const { colorTheme, balanceState } = useCommonContext();
const { colorTheme, balanceState, accountsBalances } = useCommonContext();
const [ confirm, setConfirm ] = useState<boolean>(false);
const navigate = useNavigate();

Expand Down Expand Up @@ -64,6 +64,7 @@ export default function AddressSwitcher() {
onClick={ () => account.token ? selectAddressCallback(account.accountId) : undefined }
>
<AccountAddress
balance={ accountsBalances[account.accountId.address] }
account={ account }
disabled={ account.token === undefined }
login={ () => authenticateCallback(account.accountId) }
Expand Down
13 changes: 12 additions & 1 deletion src/common/CommonContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useContext, useEffect, useReducer, Reducer, useCallback } from "
import { DateTime } from 'luxon';
import { LegalOfficerClass, BalanceState, LogionClient, Endpoint } from "@logion/client";
import { InjectedAccount } from "@logion/extension";
import { ValidAccountId } from "@logion/node-api";
import { ValidAccountId, TypesAccountData } from "@logion/node-api";

import { useLogionChain } from '../logion-chain';
import { Children } from './types/Helpers';
Expand All @@ -25,6 +25,7 @@ export type Viewer = 'User' | 'LegalOfficer';

export interface CommonContext {
balanceState?: BalanceState;
accountsBalances: Record<string, TypesAccountData>;
colorTheme: ColorTheme;
setColorTheme: ((colorTheme: ColorTheme) => void) | null;
refresh: (clearOnRefresh: boolean) => void;
Expand Down Expand Up @@ -87,6 +88,7 @@ function initialContextValue(): FullCommonContext {
expectNewTransaction: () => {},
stopExpectNewTransaction: () => {},
setNotification: () => {},
accountsBalances: {},
}
}

Expand Down Expand Up @@ -136,6 +138,7 @@ interface Action {
stopExpectNewTransaction?: () => void;
notificationText?: string;
setNotification?: (text?: string) => void;
accountsBalances?: Record<string, TypesAccountData>;
}

const MAX_REFRESH_COUNT = 12;
Expand Down Expand Up @@ -167,6 +170,7 @@ const reducer: Reducer<FullCommonContext, Action> = (state: FullCommonContext, a
return {
...state,
balanceState: action.balanceState,
accountsBalances: action.accountsBalances || {},
availableLegalOfficers: action.availableLegalOfficers!,
backendConfig: action.backendConfig!,
nodesUp,
Expand Down Expand Up @@ -315,6 +319,12 @@ export function CommonContextProvider(props: Props) {
balanceState = await client.balanceState();
}

const accountsBalances: Record<string, TypesAccountData> = {};
for (const account of accounts.all || []) {
const balance = await client.logionApi.queries.getAccountData(account.accountId);
accountsBalances[account.accountId.address] = balance;
}

const configPromises = await Promise.allSettled(client.legalOfficers.map(legalOfficer => legalOfficer.getConfig()));
const backendConfigs: Record<string, BackendConfig> = {};
for(let i = 0; i < configPromises.length; ++i) {
Expand All @@ -341,6 +351,7 @@ export function CommonContextProvider(props: Props) {
type: "SET_DATA",
dataAddress: currentAddress.toKey(),
balanceState,
accountsBalances,
nodesUp,
nodesDown,
availableLegalOfficers,
Expand Down
1 change: 1 addition & 0 deletions src/common/__mocks__/CommonContextMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export enum ExpectNewTransactionStatus {
export function useCommonContext() {
const commonContext:Partial<CommonContext> = {
balanceState,
accountsBalances: {},
colorTheme: COLOR_THEME,
setColorTheme,
refresh,
Expand Down

0 comments on commit dece0dc

Please sign in to comment.