2022-10-05 10:42:22 +00:00
|
|
|
import type { Asset } from '@vegaprotocol/assets';
|
2022-09-30 00:40:44 +00:00
|
|
|
import { useDataProvider } from '@vegaprotocol/react-helpers';
|
2022-04-06 17:48:05 +00:00
|
|
|
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import type { AgGridReact } from 'ag-grid-react';
|
2022-11-07 12:14:21 +00:00
|
|
|
import { useRef, useMemo, useCallback, memo } from 'react';
|
2022-09-30 00:40:44 +00:00
|
|
|
import type { AccountFields } from './accounts-data-provider';
|
|
|
|
import { aggregatedAccountsDataProvider } from './accounts-data-provider';
|
|
|
|
import type { GetRowsParams } from './accounts-table';
|
|
|
|
import { AccountTable } from './accounts-table';
|
2022-04-06 17:48:05 +00:00
|
|
|
|
2022-09-30 00:40:44 +00:00
|
|
|
interface AccountManagerProps {
|
2022-04-06 17:48:05 +00:00
|
|
|
partyId: string;
|
2022-09-30 00:40:44 +00:00
|
|
|
onClickAsset: (asset?: string | Asset) => void;
|
|
|
|
onClickWithdraw?: (assetId?: string) => void;
|
|
|
|
onClickDeposit?: (assetId?: string) => void;
|
2022-04-06 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 12:14:21 +00:00
|
|
|
export const AccountManager = memo(
|
|
|
|
({
|
|
|
|
onClickAsset,
|
|
|
|
onClickWithdraw,
|
|
|
|
onClickDeposit,
|
|
|
|
partyId,
|
|
|
|
}: AccountManagerProps) => {
|
|
|
|
const gridRef = useRef<AgGridReact | null>(null);
|
|
|
|
const dataRef = useRef<AccountFields[] | null>(null);
|
|
|
|
const variables = useMemo(() => ({ partyId }), [partyId]);
|
|
|
|
const update = useCallback(
|
|
|
|
({ data }: { data: AccountFields[] | null }) => {
|
|
|
|
dataRef.current = data;
|
|
|
|
gridRef.current?.api?.refreshInfiniteCache();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[gridRef]
|
|
|
|
);
|
2022-10-18 10:40:26 +00:00
|
|
|
|
2022-11-07 12:14:21 +00:00
|
|
|
const { data, loading, error } = useDataProvider<AccountFields[], never>({
|
|
|
|
dataProvider: aggregatedAccountsDataProvider,
|
|
|
|
update,
|
|
|
|
variables,
|
|
|
|
});
|
|
|
|
const getRows = async ({
|
|
|
|
successCallback,
|
|
|
|
startRow,
|
|
|
|
endRow,
|
|
|
|
}: GetRowsParams) => {
|
|
|
|
const rowsThisBlock = dataRef.current
|
|
|
|
? dataRef.current.slice(startRow, endRow)
|
|
|
|
: [];
|
|
|
|
const lastRow = dataRef.current?.length ?? -1;
|
|
|
|
successCallback(rowsThisBlock, lastRow);
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<AsyncRenderer data={data || []} error={error} loading={loading}>
|
|
|
|
<AccountTable
|
|
|
|
rowModelType={data?.length ? 'infinite' : 'clientSide'}
|
|
|
|
rowData={data?.length ? undefined : []}
|
|
|
|
ref={gridRef}
|
|
|
|
datasource={{ getRows }}
|
|
|
|
onClickAsset={onClickAsset}
|
|
|
|
onClickDeposit={onClickDeposit}
|
|
|
|
onClickWithdraw={onClickWithdraw}
|
|
|
|
/>
|
|
|
|
</AsyncRenderer>
|
|
|
|
);
|
2022-10-18 10:40:26 +00:00
|
|
|
}
|
2022-11-07 12:14:21 +00:00
|
|
|
);
|
2022-09-30 00:40:44 +00:00
|
|
|
|
2022-11-07 12:14:21 +00:00
|
|
|
export default memo(AccountManager);
|