2022-03-27 09:40:27 +00:00
|
|
|
import { useRef, useCallback, useMemo } from 'react';
|
2022-03-25 12:40:08 +00:00
|
|
|
import { produce } from 'immer';
|
2022-03-29 11:55:44 +00:00
|
|
|
import merge from 'lodash/merge';
|
2022-03-25 12:40:08 +00:00
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import { AsyncRenderer } from '../../components/async-renderer';
|
|
|
|
import { PositionsTable, getRowNodeId } from '@vegaprotocol/positions';
|
|
|
|
import { useDataProvider } from '@vegaprotocol/react-helpers';
|
|
|
|
import {
|
2022-03-29 16:52:35 +00:00
|
|
|
Positions_party_positions,
|
|
|
|
PositionSubscribe_positions,
|
2022-03-25 12:40:08 +00:00
|
|
|
positionsDataProvider,
|
|
|
|
} from '@vegaprotocol/graphql';
|
2022-03-27 09:40:27 +00:00
|
|
|
import { useVegaWallet } from '@vegaprotocol/wallet';
|
2022-03-25 12:40:08 +00:00
|
|
|
|
|
|
|
import type { AgGridReact } from 'ag-grid-react';
|
|
|
|
|
|
|
|
export const Positions = () => {
|
|
|
|
const { pathname, push } = useRouter();
|
2022-03-29 17:31:00 +00:00
|
|
|
const gridRef = useRef<AgGridReact | null>(null);
|
2022-03-27 09:40:27 +00:00
|
|
|
const { keypair } = useVegaWallet();
|
2022-03-29 17:31:00 +00:00
|
|
|
const variables = useMemo(() => ({ partyId: keypair?.pub }), [keypair]);
|
2022-03-25 12:40:08 +00:00
|
|
|
const update = useCallback(
|
2022-03-29 16:52:35 +00:00
|
|
|
(delta: PositionSubscribe_positions) => {
|
|
|
|
const update: Positions_party_positions[] = [];
|
|
|
|
const add: Positions_party_positions[] = [];
|
2022-03-25 12:40:08 +00:00
|
|
|
if (!gridRef.current) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const rowNode = gridRef.current.api.getRowNode(getRowNodeId(delta));
|
|
|
|
if (rowNode) {
|
2022-03-29 16:52:35 +00:00
|
|
|
const updatedData = produce<Positions_party_positions>(
|
2022-03-25 12:40:08 +00:00
|
|
|
rowNode.data,
|
2022-03-29 16:52:35 +00:00
|
|
|
(draft: Positions_party_positions) => {
|
2022-03-29 11:55:44 +00:00
|
|
|
merge(draft, delta);
|
2022-03-27 09:40:27 +00:00
|
|
|
}
|
2022-03-25 12:40:08 +00:00
|
|
|
);
|
|
|
|
if (updatedData !== rowNode.data) {
|
2022-03-27 09:40:27 +00:00
|
|
|
update.push(updatedData);
|
2022-03-25 12:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
add.push(delta);
|
|
|
|
}
|
|
|
|
if (update.length || add.length) {
|
|
|
|
gridRef.current.api.applyTransactionAsync({
|
|
|
|
update,
|
|
|
|
add,
|
|
|
|
addIndex: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[gridRef]
|
|
|
|
);
|
|
|
|
const { data, error, loading } = useDataProvider<
|
2022-03-29 16:52:35 +00:00
|
|
|
Positions_party_positions,
|
|
|
|
PositionSubscribe_positions
|
2022-03-27 09:40:27 +00:00
|
|
|
>(positionsDataProvider, update, variables);
|
2022-03-25 12:40:08 +00:00
|
|
|
return (
|
|
|
|
<AsyncRenderer loading={loading} error={error} data={data}>
|
|
|
|
{(data) => (
|
|
|
|
<PositionsTable
|
|
|
|
ref={gridRef}
|
|
|
|
data={data}
|
|
|
|
onRowClicked={(id) =>
|
|
|
|
push(`${pathname}/${id}?portfolio=orders&trade=orderbook`)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</AsyncRenderer>
|
|
|
|
);
|
|
|
|
};
|