2023-01-17 15:01:24 +00:00
|
|
|
import {
|
|
|
|
t,
|
|
|
|
useDataProvider,
|
|
|
|
updateGridData,
|
|
|
|
} 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-11-23 23:42:22 +00:00
|
|
|
onClickAsset: (assetId: string) => void;
|
2022-09-30 00:40:44 +00:00
|
|
|
onClickWithdraw?: (assetId?: string) => void;
|
|
|
|
onClickDeposit?: (assetId?: string) => void;
|
2023-01-31 16:04:52 +00:00
|
|
|
isReadOnly: boolean;
|
2022-04-06 17:48:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 12:32:54 +00:00
|
|
|
export const AccountManager = ({
|
|
|
|
onClickAsset,
|
|
|
|
onClickWithdraw,
|
|
|
|
onClickDeposit,
|
|
|
|
partyId,
|
2023-01-31 16:04:52 +00:00
|
|
|
isReadOnly,
|
2022-12-02 12:32:54 +00:00
|
|
|
}: 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 }) => {
|
2023-01-17 15:01:24 +00:00
|
|
|
return updateGridData(dataRef, data, gridRef);
|
2022-12-02 12:32:54 +00:00
|
|
|
},
|
|
|
|
[gridRef]
|
|
|
|
);
|
2022-10-18 10:40:26 +00:00
|
|
|
|
2022-12-02 12:32:54 +00:00
|
|
|
const { data, loading, error } = useDataProvider<AccountFields[], never>({
|
|
|
|
dataProvider: aggregatedAccountsDataProvider,
|
|
|
|
update,
|
|
|
|
variables,
|
|
|
|
});
|
|
|
|
const getRows = useCallback(
|
|
|
|
async ({ successCallback, startRow, endRow }: GetRowsParams) => {
|
2022-11-07 12:14:21 +00:00
|
|
|
const rowsThisBlock = dataRef.current
|
|
|
|
? dataRef.current.slice(startRow, endRow)
|
|
|
|
: [];
|
2023-01-11 14:43:52 +00:00
|
|
|
const lastRow = dataRef.current ? dataRef.current.length : 0;
|
2022-11-07 12:14:21 +00:00
|
|
|
successCallback(rowsThisBlock, lastRow);
|
2022-12-02 12:32:54 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<div className="relative h-full">
|
|
|
|
<AccountTable
|
|
|
|
rowModelType="infinite"
|
|
|
|
ref={gridRef}
|
|
|
|
datasource={{ getRows }}
|
|
|
|
onClickAsset={onClickAsset}
|
|
|
|
onClickDeposit={onClickDeposit}
|
|
|
|
onClickWithdraw={onClickWithdraw}
|
2023-01-31 16:04:52 +00:00
|
|
|
isReadOnly={isReadOnly}
|
2022-12-02 12:32:54 +00:00
|
|
|
/>
|
2023-01-11 14:43:52 +00:00
|
|
|
<div className="pointer-events-none absolute inset-0">
|
2022-12-02 12:32:54 +00:00
|
|
|
<AsyncRenderer
|
2023-01-11 14:43:52 +00:00
|
|
|
data={data}
|
|
|
|
noDataCondition={(data) => !(data && data.length)}
|
2022-12-02 12:32:54 +00:00
|
|
|
error={error}
|
|
|
|
loading={loading}
|
|
|
|
noDataMessage={t('No accounts')}
|
2022-11-07 12:14:21 +00:00
|
|
|
/>
|
2022-12-02 12:32:54 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2022-09-30 00:40:44 +00:00
|
|
|
|
2022-11-07 12:14:21 +00:00
|
|
|
export default memo(AccountManager);
|