From 414b8ced13c5394bd8fb7e0e9bde073685626fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20G=C5=82ownia?= Date: Wed, 28 Sep 2022 00:02:48 +0200 Subject: [PATCH] chore: fix positions data provider restarts (#1509) --- .../portfolio/positions/positions-asset.tsx | 6 +-- .../portfolio/positions/positions.tsx | 4 +- .../src/lib/components/select-market.tsx | 2 +- libs/positions/src/lib/positions-manager.tsx | 5 +-- libs/positions/src/lib/positions.tsx | 6 +-- .../positions/src/lib/use-positions-assets.ts | 6 +-- libs/positions/src/lib/use-positions-data.tsx | 43 +++++++++---------- 7 files changed, 30 insertions(+), 42 deletions(-) diff --git a/apps/console-lite/src/app/components/portfolio/positions/positions-asset.tsx b/apps/console-lite/src/app/components/portfolio/positions/positions-asset.tsx index 4ada5a6c6..d50e10b14 100644 --- a/apps/console-lite/src/app/components/portfolio/positions/positions-asset.tsx +++ b/apps/console-lite/src/app/components/portfolio/positions/positions-asset.tsx @@ -17,11 +17,11 @@ const getRowId = ({ data }: { data: Position }) => data.marketId; const PositionsAsset = ({ partyId, assetSymbol }: Props) => { const gridRef = useRef(null); - const { data, error, loading, getRows } = usePositionsData({ + const { data, error, loading, getRows } = usePositionsData( partyId, - assetSymbol, gridRef, - }); + assetSymbol + ); const { columnDefs, defaultColDef } = useColumnDefinitions(); return ( diff --git a/apps/console-lite/src/app/components/portfolio/positions/positions.tsx b/apps/console-lite/src/app/components/portfolio/positions/positions.tsx index 71b592b80..da9de0939 100644 --- a/apps/console-lite/src/app/components/portfolio/positions/positions.tsx +++ b/apps/console-lite/src/app/components/portfolio/positions/positions.tsx @@ -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 ( {assetSymbols && assetSymbols.length > 0 && ( diff --git a/libs/market-list/src/lib/components/select-market.tsx b/libs/market-list/src/lib/components/select-market.tsx index b4812b082..4e854bbae 100644 --- a/libs/market-list/src/lib/components/select-market.tsx +++ b/libs/market-list/src/lib/components/select-market.tsx @@ -160,7 +160,7 @@ export const SelectMarketPopover = ({ PositionsSubscription_positions[] >({ dataProvider: positionsDataProvider, - update: () => false, + noUpdate: true, variables, skip: !keypair, }); diff --git a/libs/positions/src/lib/positions-manager.tsx b/libs/positions/src/lib/positions-manager.tsx index f3ae3a58d..0542c24e0 100644 --- a/libs/positions/src/lib/positions-manager.tsx +++ b/libs/positions/src/lib/positions-manager.tsx @@ -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 ( <> diff --git a/libs/positions/src/lib/positions.tsx b/libs/positions/src/lib/positions.tsx index 3d0dfe8b5..fce0e71bb 100644 --- a/libs/positions/src/lib/positions.tsx +++ b/libs/positions/src/lib/positions.tsx @@ -16,11 +16,11 @@ interface PositionsProps { export const Positions = memo( ({ partyId, assetSymbol, onClose }: PositionsProps) => { const gridRef = useRef(null); - const { data, error, loading, getRows } = usePositionsData({ + const { data, error, loading, getRows } = usePositionsData( partyId, - assetSymbol, gridRef, - }); + assetSymbol + ); return ( diff --git a/libs/positions/src/lib/use-positions-assets.ts b/libs/positions/src/lib/use-positions-assets.ts index f360d2558..91106143f 100644 --- a/libs/positions/src/lib/use-positions-assets.ts +++ b/libs/positions/src/lib/use-positions-assets.ts @@ -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(); const update = useCallback(({ data }: { data: Position[] | null }) => { diff --git a/libs/positions/src/lib/use-positions-data.tsx b/libs/positions/src/lib/use-positions-data.tsx index be72113ed..8e6ce3747 100644 --- a/libs/positions/src/lib/use-positions-data.tsx +++ b/libs/positions/src/lib/use-positions-data.tsx @@ -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; -} - 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, + assetSymbol?: string +) => { const variables = useMemo(() => ({ partyId }), [partyId]); const dataRef = useRef(null); const update = useCallback( @@ -61,22 +59,21 @@ export const usePositionsData = ({ partyId, assetSymbol, gridRef }: Props) => { variables, }); dataRef.current = assetSymbol ? filter(data, { assetSymbol }) : data; - 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); - if (gridRef.current?.api) { - gridRef.current.api.setPinnedBottomRowData([ - getSummaryRow(rowsThisBlock), - ]); - } - }; + const getRows = useCallback( + async ({ successCallback, startRow, endRow }: GetRowsParams) => { + const rowsThisBlock = dataRef.current + ? dataRef.current.slice(startRow, endRow) + : []; + const lastRow = dataRef.current?.length ?? -1; + successCallback(rowsThisBlock, lastRow); + if (gridRef.current?.api) { + gridRef.current.api.setPinnedBottomRowData([ + getSummaryRow(rowsThisBlock), + ]); + } + }, + [gridRef] + ); return { data, error,