2023-04-03 13:17:23 +00:00
|
|
|
import { useCallback, useRef, useState } from 'react';
|
2023-01-19 12:10:52 +00:00
|
|
|
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
|
2023-03-09 23:52:38 +00:00
|
|
|
import type { Position } from '../';
|
2023-01-19 12:10:52 +00:00
|
|
|
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';
|
2023-02-28 18:56:29 +00:00
|
|
|
import { t } from '@vegaprotocol/i18n';
|
2023-03-09 23:52:38 +00:00
|
|
|
import { useBottomPlaceholder } 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;
|
2023-03-09 23:52:38 +00:00
|
|
|
noBottomPlaceholder?: 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-03-09 23:52:38 +00:00
|
|
|
noBottomPlaceholder,
|
2023-01-19 21:53:10 +00:00
|
|
|
}: PositionsManagerProps) => {
|
2022-10-19 23:59:36 +00:00
|
|
|
const gridRef = useRef<AgGridReact | null>(null);
|
2023-03-27 10:30:55 +00:00
|
|
|
const { data, error, loading, reload } = usePositionsData(partyId, gridRef);
|
2023-04-03 13:17:23 +00:00
|
|
|
const [dataCount, setDataCount] = useState(data?.length ?? 0);
|
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,
|
2023-03-29 06:50:11 +00:00
|
|
|
timeInForce: Schema.OrderTimeInForce.TIME_IN_FORCE_IOC as const,
|
2023-01-31 16:04:52 +00:00
|
|
|
side: openVolume.startsWith('-')
|
|
|
|
? Schema.Side.SIDE_BUY
|
|
|
|
: Schema.Side.SIDE_SELL,
|
|
|
|
size: openVolume.replace('-', ''),
|
2023-03-29 06:50:11 +00:00
|
|
|
reduceOnly: true,
|
2023-01-31 16:04:52 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
2023-03-09 23:52:38 +00:00
|
|
|
|
2023-05-04 10:09:55 +00:00
|
|
|
const setId = useCallback((data: Position, id: string) => {
|
2023-03-09 23:52:38 +00:00
|
|
|
return {
|
|
|
|
...data,
|
2023-05-04 10:09:55 +00:00
|
|
|
marketId: id,
|
2023-03-09 23:52:38 +00:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
const bottomPlaceholderProps = useBottomPlaceholder<Position>({
|
|
|
|
gridRef,
|
|
|
|
setId,
|
2023-03-17 09:50:43 +00:00
|
|
|
disabled: noBottomPlaceholder,
|
2023-03-09 23:52:38 +00:00
|
|
|
});
|
2023-04-03 13:17:23 +00:00
|
|
|
const updateRowCount = useCallback(() => {
|
2023-03-15 15:08:48 +00:00
|
|
|
setDataCount(gridRef.current?.api?.getModel().getRowCount() ?? 0);
|
|
|
|
}, []);
|
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-03-15 15:08:48 +00:00
|
|
|
suppressLoadingOverlay
|
|
|
|
suppressNoRowsOverlay
|
2023-01-31 16:04:52 +00:00
|
|
|
isReadOnly={isReadOnly}
|
2023-04-03 13:17:23 +00:00
|
|
|
onFilterChanged={updateRowCount}
|
|
|
|
onRowDataUpdated={updateRowCount}
|
2023-03-17 09:50:43 +00:00
|
|
|
{...bottomPlaceholderProps}
|
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')}
|
2023-03-15 15:08:48 +00:00
|
|
|
noDataCondition={(data) => !dataCount}
|
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
|
|
|
);
|
|
|
|
};
|