chore: fix positions data provider restarts (#1509)

This commit is contained in:
Bartłomiej Głownia 2022-09-28 00:02:48 +02:00 committed by GitHub
parent e310f04034
commit 414b8ced13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 42 deletions

View File

@ -17,11 +17,11 @@ const getRowId = ({ data }: { data: Position }) => data.marketId;
const PositionsAsset = ({ partyId, assetSymbol }: Props) => {
const gridRef = useRef<AgGridReact | null>(null);
const { data, error, loading, getRows } = usePositionsData({
const { data, error, loading, getRows } = usePositionsData(
partyId,
assetSymbol,
gridRef,
});
assetSymbol
);
const { columnDefs, defaultColDef } = useColumnDefinitions();
return (
<AsyncRenderer loading={loading} error={error} data={data}>

View File

@ -6,9 +6,7 @@ import PositionsAsset from './positions-asset';
const Positions = () => {
const { partyId } = useOutletContext<{ partyId: string }>();
const { data, error, loading, assetSymbols } = usePositionsAssets({
partyId,
});
const { data, error, loading, assetSymbols } = usePositionsAssets(partyId);
return (
<AsyncRenderer loading={loading} error={error} data={data}>
{assetSymbols && assetSymbols.length > 0 && (

View File

@ -160,7 +160,7 @@ export const SelectMarketPopover = ({
PositionsSubscription_positions[]
>({
dataProvider: positionsDataProvider,
update: () => false,
noUpdate: true,
variables,
skip: !keypair,
});

View File

@ -17,10 +17,7 @@ export const PositionsManager = ({ partyId }: PositionsManagerProps) => {
[submit]
);
const { data, error, loading, assetSymbols } = usePositionsAssets({
partyId,
});
const { data, error, loading, assetSymbols } = usePositionsAssets(partyId);
return (
<>
<AsyncRenderer loading={loading} error={error} data={data}>

View File

@ -16,11 +16,11 @@ interface PositionsProps {
export const Positions = memo(
({ partyId, assetSymbol, onClose }: PositionsProps) => {
const gridRef = useRef<AgGridReact | null>(null);
const { data, error, loading, getRows } = usePositionsData({
const { data, error, loading, getRows } = usePositionsData(
partyId,
assetSymbol,
gridRef,
});
assetSymbol
);
return (
<AsyncRenderer loading={loading} error={error} data={data}>

View File

@ -3,14 +3,10 @@ import { useDataProvider } from '@vegaprotocol/react-helpers';
import type { Position } from './positions-data-providers';
import { positionsMetricsDataProvider as dataProvider } from './positions-data-providers';
interface Props {
partyId: string;
}
const getSymbols = (positions: Position[]) =>
Array.from(new Set(positions.map((position) => position.assetSymbol))).sort();
export const usePositionsAssets = ({ partyId }: Props) => {
export const usePositionsAssets = (partyId: string) => {
const variables = useMemo(() => ({ partyId }), [partyId]);
const assetSymbols = useRef<string[] | undefined>();
const update = useCallback(({ data }: { data: Position[] | null }) => {

View File

@ -8,12 +8,6 @@ import { positionsMetricsDataProvider as dataProvider } from './positions-data-p
import filter from 'lodash/filter';
import { t, toBigNum, useDataProvider } from '@vegaprotocol/react-helpers';
interface Props {
partyId: string;
assetSymbol?: string;
gridRef: RefObject<AgGridReact>;
}
const getSummaryRow = (positions: Position[]) => {
const summaryRow = {
notional: new BigNumber(0),
@ -41,7 +35,11 @@ const getSummaryRow = (positions: Position[]) => {
};
};
export const usePositionsData = ({ partyId, assetSymbol, gridRef }: Props) => {
export const usePositionsData = (
partyId: string,
gridRef: RefObject<AgGridReact>,
assetSymbol?: string
) => {
const variables = useMemo(() => ({ partyId }), [partyId]);
const dataRef = useRef<Position[] | null>(null);
const update = useCallback(
@ -61,11 +59,8 @@ export const usePositionsData = ({ partyId, assetSymbol, gridRef }: Props) => {
variables,
});
dataRef.current = assetSymbol ? filter(data, { assetSymbol }) : data;
const getRows = async ({
successCallback,
startRow,
endRow,
}: GetRowsParams) => {
const getRows = useCallback(
async ({ successCallback, startRow, endRow }: GetRowsParams) => {
const rowsThisBlock = dataRef.current
? dataRef.current.slice(startRow, endRow)
: [];
@ -76,7 +71,9 @@ export const usePositionsData = ({ partyId, assetSymbol, gridRef }: Props) => {
getSummaryRow(rowsThisBlock),
]);
}
};
},
[gridRef]
);
return {
data,
error,