2022-10-24 18:16:15 +00:00
|
|
|
import { useRef } from 'react';
|
2023-01-19 12:10:52 +00:00
|
|
|
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import { usePositionsData, PositionsTable } from '../';
|
2022-10-19 23:59:36 +00:00
|
|
|
import type { AgGridReact } from 'ag-grid-react';
|
2023-01-19 12:10:52 +00:00
|
|
|
import * as Schema from '@vegaprotocol/types';
|
|
|
|
import { useVegaTransactionStore } from '@vegaprotocol/wallet';
|
2022-10-24 18:16:15 +00:00
|
|
|
import { t } from '@vegaprotocol/react-helpers';
|
2022-03-30 00:01:34 +00:00
|
|
|
|
|
|
|
interface PositionsManagerProps {
|
|
|
|
partyId: string;
|
2023-01-19 21:53:10 +00:00
|
|
|
onMarketClick?: (marketId: string) => void;
|
2023-01-31 16:04:52 +00:00
|
|
|
isReadOnly: boolean;
|
2022-03-30 00:01:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 21:53:10 +00:00
|
|
|
export const PositionsManager = ({
|
|
|
|
partyId,
|
|
|
|
onMarketClick,
|
2023-01-31 16:04:52 +00:00
|
|
|
isReadOnly,
|
2023-01-19 21:53:10 +00:00
|
|
|
}: PositionsManagerProps) => {
|
2022-10-19 23:59:36 +00:00
|
|
|
const gridRef = useRef<AgGridReact | null>(null);
|
2023-02-16 16:30:32 +00:00
|
|
|
const { data, error, loading, reload } = usePositionsData(
|
|
|
|
partyId,
|
|
|
|
gridRef,
|
|
|
|
true
|
|
|
|
);
|
2023-01-19 12:10:52 +00:00
|
|
|
const create = useVegaTransactionStore((store) => store.create);
|
2023-01-31 16:04:52 +00:00
|
|
|
const onClose = ({
|
|
|
|
marketId,
|
|
|
|
openVolume,
|
|
|
|
}: {
|
|
|
|
marketId: string;
|
|
|
|
openVolume: string;
|
|
|
|
}) =>
|
|
|
|
create({
|
|
|
|
batchMarketInstructions: {
|
|
|
|
cancellations: [
|
|
|
|
{
|
|
|
|
marketId,
|
|
|
|
orderId: '', // omit order id to cancel all active orders
|
|
|
|
},
|
|
|
|
],
|
|
|
|
submissions: [
|
|
|
|
{
|
|
|
|
marketId: marketId,
|
|
|
|
type: Schema.OrderType.TYPE_MARKET as const,
|
|
|
|
timeInForce: Schema.OrderTimeInForce.TIME_IN_FORCE_FOK as const,
|
|
|
|
side: openVolume.startsWith('-')
|
|
|
|
? Schema.Side.SIDE_BUY
|
|
|
|
: Schema.Side.SIDE_SELL,
|
|
|
|
size: openVolume.replace('-', ''),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
2022-03-30 00:01:34 +00:00
|
|
|
return (
|
2023-01-11 14:43:52 +00:00
|
|
|
<div className="h-full relative">
|
|
|
|
<PositionsTable
|
2023-02-16 16:30:32 +00:00
|
|
|
rowData={error ? [] : data}
|
2023-01-11 14:43:52 +00:00
|
|
|
ref={gridRef}
|
2023-01-19 21:53:10 +00:00
|
|
|
onMarketClick={onMarketClick}
|
2023-01-31 16:04:52 +00:00
|
|
|
onClose={onClose}
|
2023-01-11 14:43:52 +00:00
|
|
|
noRowsOverlayComponent={() => null}
|
2023-01-31 16:04:52 +00:00
|
|
|
isReadOnly={isReadOnly}
|
2023-01-11 14:43:52 +00:00
|
|
|
/>
|
|
|
|
<div className="pointer-events-none absolute inset-0">
|
|
|
|
<AsyncRenderer
|
|
|
|
loading={loading}
|
|
|
|
error={error}
|
|
|
|
data={data}
|
|
|
|
noDataMessage={t('No positions')}
|
|
|
|
noDataCondition={(data) => !(data && data.length)}
|
2023-02-16 16:30:32 +00:00
|
|
|
reload={reload}
|
2022-10-19 23:59:36 +00:00
|
|
|
/>
|
2023-01-11 14:43:52 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-03-30 00:01:34 +00:00
|
|
|
);
|
|
|
|
};
|