2022-03-30 00:01:34 +00:00
|
|
|
import { useRef, useCallback, useMemo } from 'react';
|
|
|
|
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import { useDataProvider } from '@vegaprotocol/react-helpers';
|
2022-08-16 07:18:55 +00:00
|
|
|
import type { PositionsMetricsSubscription_positions } from './__generated__/PositionsMetricsSubscription';
|
2022-03-30 00:01:34 +00:00
|
|
|
import type { AgGridReact } from 'ag-grid-react';
|
2022-08-16 07:18:55 +00:00
|
|
|
import PositionsTable from './positions-table';
|
|
|
|
import type { GetRowsParams } from './positions-table';
|
|
|
|
import { positionsMetricsDataProvider as dataProvider } from './positions-metrics-data-provider';
|
|
|
|
import type { Data, Position } from './positions-metrics-data-provider';
|
2022-03-30 00:01:34 +00:00
|
|
|
|
|
|
|
interface PositionsManagerProps {
|
|
|
|
partyId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const PositionsManager = ({ partyId }: PositionsManagerProps) => {
|
|
|
|
const gridRef = useRef<AgGridReact | null>(null);
|
|
|
|
const variables = useMemo(() => ({ partyId }), [partyId]);
|
2022-08-16 07:18:55 +00:00
|
|
|
const dataRef = useRef<Position[] | null>(null);
|
|
|
|
const update = useCallback(({ data }: { data: Data }) => {
|
|
|
|
if (!gridRef.current?.api) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dataRef.current = data.positions;
|
|
|
|
gridRef.current.api.refreshInfiniteCache();
|
|
|
|
return true;
|
|
|
|
}, []);
|
2022-03-30 00:01:34 +00:00
|
|
|
const { data, error, loading } = useDataProvider<
|
2022-08-16 07:18:55 +00:00
|
|
|
Data,
|
|
|
|
PositionsMetricsSubscription_positions
|
|
|
|
>({ dataProvider, update, variables });
|
|
|
|
dataRef.current = data?.positions || null;
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2022-03-30 00:01:34 +00:00
|
|
|
return (
|
|
|
|
<AsyncRenderer loading={loading} error={error} data={data}>
|
2022-08-16 07:18:55 +00:00
|
|
|
<PositionsTable
|
|
|
|
rowModelType={data?.positions?.length ? 'infinite' : 'clientSide'}
|
|
|
|
rowData={data?.positions?.length ? undefined : []}
|
|
|
|
datasource={{ getRows }}
|
|
|
|
/>
|
2022-03-30 00:01:34 +00:00
|
|
|
</AsyncRenderer>
|
|
|
|
);
|
|
|
|
};
|