2023-03-15 15:08:48 +00:00
|
|
|
import { useCallback, useEffect, 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 '../';
|
2023-03-15 15:08:48 +00:00
|
|
|
import type { FilterChangedEvent } from 'ag-grid-community';
|
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-15 15:08:48 +00:00
|
|
|
const [dataCount, setDataCount] = useState(0);
|
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('-', ''),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
2023-03-09 23:52:38 +00:00
|
|
|
|
|
|
|
const setId = useCallback((data: Position) => {
|
|
|
|
return {
|
|
|
|
...data,
|
|
|
|
marketId: `${data.marketId}-1`,
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
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-03-15 15:08:48 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setDataCount(gridRef.current?.api?.getModel().getRowCount() ?? 0);
|
2023-03-17 09:50:43 +00:00
|
|
|
}, [data?.length]);
|
2023-03-15 15:08:48 +00:00
|
|
|
const onFilterChanged = useCallback((event: FilterChangedEvent) => {
|
|
|
|
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-03-15 15:08:48 +00:00
|
|
|
onFilterChanged={onFilterChanged}
|
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
|
|
|
);
|
|
|
|
};
|