import { useRef, memo, useState, useCallback } from 'react'; import { addDecimalsFormatNumber } from '@vegaprotocol/utils'; import { t } from '@vegaprotocol/i18n'; import { useDataProvider } from '@vegaprotocol/data-provider'; import type { AgGridReact } from 'ag-grid-react'; import { aggregatedAccountsDataProvider, aggregatedAccountDataProvider, } from './accounts-data-provider'; import type { PinnedAsset } from './accounts-table'; import { AccountTable } from './accounts-table'; import { Dialog } from '@vegaprotocol/ui-toolkit'; import BreakdownTable from './breakdown-table'; import type { useDataGridEvents } from '@vegaprotocol/datagrid'; const AccountBreakdown = ({ assetId, partyId, onMarketClick, }: { assetId: string; partyId: string; onMarketClick?: (marketId: string, metaKey?: boolean) => void; }) => { const gridRef = useRef(null); const { data } = useDataProvider({ dataProvider: aggregatedAccountDataProvider, variables: { partyId, assetId }, update: ({ data }) => { if (gridRef.current?.api && data?.breakdown) { gridRef.current?.api.setRowData(data?.breakdown); return true; } return false; }, }); return (

{data?.asset?.symbol} {t('usage breakdown')}

{data && (

{t('You have %s %s in total.', [ addDecimalsFormatNumber(data.total, data.asset.decimals), data.asset.symbol, ])}

)}
); }; export const AccountBreakdownDialog = memo( ({ assetId, partyId, onClose, onMarketClick, }: { assetId?: string; partyId: string; onClose: () => void; onMarketClick?: (marketId: string, metaKey?: boolean) => void; }) => { return ( { if (!isOpen) { onClose(); } }} > {assetId && ( )} ); } ); interface AccountManagerProps { partyId: string; onClickAsset: (assetId: string) => void; onClickWithdraw?: (assetId?: string) => void; onClickDeposit?: (assetId?: string) => void; onClickTransfer?: (assetId?: string) => void; onMarketClick?: (marketId: string, metaKey?: boolean) => void; isReadOnly: boolean; pinnedAsset?: PinnedAsset; gridProps?: ReturnType; } export const AccountManager = ({ onClickAsset, onClickWithdraw, onClickDeposit, onClickTransfer, partyId, isReadOnly, pinnedAsset, onMarketClick, gridProps, }: AccountManagerProps) => { const [breakdownAssetId, setBreakdownAssetId] = useState(); const { data, error } = useDataProvider({ dataProvider: aggregatedAccountsDataProvider, variables: { partyId }, }); const onMarketClickInternal = useCallback( (...args: Parameters>) => { setBreakdownAssetId(undefined); if (onMarketClick) { onMarketClick(...args); } }, [onMarketClick] ); return (
setBreakdownAssetId(undefined), [])} onMarketClick={onMarketClick ? onMarketClickInternal : undefined} />
); }; export default memo(AccountManager);