fix(positions): make positions table smoothly updated (again) (#3266)

This commit is contained in:
Maciek 2023-03-27 12:30:55 +02:00 committed by GitHub
parent 621ba497e8
commit a2b112d898
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 10 deletions

View File

@ -24,11 +24,7 @@ export const PositionsManager = ({
}: PositionsManagerProps) => { }: PositionsManagerProps) => {
const gridRef = useRef<AgGridReact | null>(null); const gridRef = useRef<AgGridReact | null>(null);
const [dataCount, setDataCount] = useState(0); const [dataCount, setDataCount] = useState(0);
const { data, error, loading, reload } = usePositionsData( const { data, error, loading, reload } = usePositionsData(partyId, gridRef);
partyId,
gridRef,
true
);
const create = useVegaTransactionStore((store) => store.create); const create = useVegaTransactionStore((store) => store.create);
const onClose = ({ const onClose = ({
marketId, marketId,
@ -72,7 +68,7 @@ export const PositionsManager = ({
}); });
useEffect(() => { useEffect(() => {
setDataCount(gridRef.current?.api?.getModel().getRowCount() ?? 0); setDataCount(gridRef.current?.api?.getModel().getRowCount() ?? 0);
}, [data?.length]); }, [data]);
const onFilterChanged = useCallback((event: FilterChangedEvent) => { const onFilterChanged = useCallback((event: FilterChangedEvent) => {
setDataCount(gridRef.current?.api?.getModel().getRowCount() ?? 0); setDataCount(gridRef.current?.api?.getModel().getRowCount() ?? 0);
}, []); }, []);

View File

@ -6,13 +6,13 @@ import { positionsMetricsProvider } from './positions-data-providers';
import type { PositionsQueryVariables } from './__generated__/Positions'; import type { PositionsQueryVariables } from './__generated__/Positions';
import { useDataProvider, updateGridData } from '@vegaprotocol/react-helpers'; import { useDataProvider, updateGridData } from '@vegaprotocol/react-helpers';
import type { GetRowsParams } from '@vegaprotocol/datagrid'; import type { GetRowsParams } from '@vegaprotocol/datagrid';
import isEqual from 'lodash/isEqual';
export const getRowId = ({ data }: { data: Position }) => data.marketId; export const getRowId = ({ data }: { data: Position }) => data.marketId;
export const usePositionsData = ( export const usePositionsData = (
partyId: string, partyId: string,
gridRef: RefObject<AgGridReact>, gridRef: RefObject<AgGridReact>
clientSideModel?: boolean
) => { ) => {
const variables = useMemo<PositionsQueryVariables>( const variables = useMemo<PositionsQueryVariables>(
() => ({ partyId }), () => ({ partyId }),
@ -21,9 +21,30 @@ export const usePositionsData = (
const dataRef = useRef<Position[] | null>(null); const dataRef = useRef<Position[] | null>(null);
const update = useCallback( const update = useCallback(
({ data }: { data: Position[] | null }) => { ({ data }: { data: Position[] | null }) => {
return clientSideModel ? false : updateGridData(dataRef, data, gridRef); if (gridRef.current?.api?.getModel().getType() === 'infinite') {
return updateGridData(dataRef, data, gridRef);
}
const update: Position[] = [];
const add: Position[] = [];
data?.forEach((d) => {
const rowNode = gridRef.current?.api?.getRowNode(d.marketId);
if (rowNode) {
if (!isEqual(rowNode.data, d)) {
update.push(d);
}
} else {
add.push(d);
}
});
gridRef.current?.api?.applyTransaction({
update,
add,
addIndex: 0,
});
return true;
}, },
[gridRef, clientSideModel] [gridRef]
); );
const { data, error, loading, reload } = useDataProvider({ const { data, error, loading, reload } = useDataProvider({
dataProvider: positionsMetricsProvider, dataProvider: positionsMetricsProvider,