mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
100 lines
3.4 KiB
TypeScript
100 lines
3.4 KiB
TypeScript
import { useQueryClient } from '@tanstack/react-query';
|
|
import React from 'react';
|
|
|
|
import type { SocketMessage } from 'lib/socket/types';
|
|
import type { Address } from 'types/api/address';
|
|
|
|
import { getResourceKey } from 'lib/api/useApiQuery';
|
|
import useSocketChannel from 'lib/socket/useSocketChannel';
|
|
import useSocketMessage from 'lib/socket/useSocketMessage';
|
|
import { currencyUnits } from 'lib/units';
|
|
import { Skeleton } from '@luxfi/ui/skeleton';
|
|
import * as DetailedInfo from 'ui/shared/DetailedInfo/DetailedInfo';
|
|
import NativeTokenIcon from 'ui/shared/NativeTokenIcon';
|
|
import NativeCoinValue from 'ui/shared/value/NativeCoinValue';
|
|
|
|
interface Props {
|
|
data: Pick<Address, 'block_number_balance_updated_at' | 'coin_balance' | 'hash' | 'exchange_rate'>;
|
|
isLoading?: boolean;
|
|
}
|
|
|
|
const AddressBalance = ({ data, isLoading }: Props) => {
|
|
const [ lastBlockNumber, setLastBlockNumber ] = React.useState<number>(data.block_number_balance_updated_at || 0);
|
|
const queryClient = useQueryClient();
|
|
|
|
const updateData = React.useCallback((balance: string, exchangeRate: string, blockNumber: number) => {
|
|
if (blockNumber < lastBlockNumber) {
|
|
return;
|
|
}
|
|
|
|
setLastBlockNumber(blockNumber);
|
|
const queryKey = getResourceKey('general:address', { pathParams: { hash: data.hash } });
|
|
queryClient.setQueryData(queryKey, (prevData: Address | undefined) => {
|
|
if (!prevData) {
|
|
return;
|
|
}
|
|
return {
|
|
...prevData,
|
|
coin_balance: balance,
|
|
exchange_rate: exchangeRate,
|
|
block_number_balance_updated_at: blockNumber,
|
|
};
|
|
});
|
|
}, [ data.hash, lastBlockNumber, queryClient ]);
|
|
|
|
const handleNewBalanceMessage: SocketMessage.AddressBalance['handler'] = React.useCallback((payload) => {
|
|
updateData(payload.balance, payload.exchange_rate, payload.block_number);
|
|
}, [ updateData ]);
|
|
|
|
const handleNewCoinBalanceMessage: SocketMessage.AddressCurrentCoinBalance['handler'] = React.useCallback((payload) => {
|
|
updateData(payload.coin_balance, payload.exchange_rate, payload.block_number);
|
|
}, [ updateData ]);
|
|
|
|
const channel = useSocketChannel({
|
|
topic: `addresses:${ data.hash.toLowerCase() }`,
|
|
isDisabled: !data.coin_balance,
|
|
});
|
|
useSocketMessage({
|
|
channel,
|
|
event: 'balance',
|
|
handler: handleNewBalanceMessage,
|
|
});
|
|
useSocketMessage({
|
|
channel,
|
|
event: 'current_coin_balance',
|
|
handler: handleNewCoinBalanceMessage,
|
|
});
|
|
|
|
// On pruned nodes, coin_balance may be null while the address is known to exist.
|
|
// Show "Pending" instead of a misleading "0 LUX".
|
|
const isBalancePending = data.coin_balance === null;
|
|
|
|
return (
|
|
<>
|
|
<DetailedInfo.ItemLabel
|
|
hint={ `${ currencyUnits.ether } balance` }
|
|
isLoading={ isLoading }
|
|
>
|
|
Balance
|
|
</DetailedInfo.ItemLabel>
|
|
<DetailedInfo.ItemValue multiRow>
|
|
{ isBalancePending ? (
|
|
<Skeleton loading={ isLoading } className="inline-flex items-center">
|
|
<NativeTokenIcon className="w-5 h-5 mr-2"/>
|
|
<span className="text-[var(--color-text-secondary)]">Pending</span>
|
|
</Skeleton>
|
|
) : (
|
|
<NativeCoinValue
|
|
amount={ data.coin_balance || '0' }
|
|
exchangeRate={ data.exchange_rate }
|
|
startElement={ <NativeTokenIcon className="w-5 h-5 mr-2" isLoading={ isLoading }/> }
|
|
loading={ isLoading }
|
|
/>
|
|
) }
|
|
</DetailedInfo.ItemValue>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default React.memo(AddressBalance);
|