vega-frontend-monorepo/libs/positions/src/lib/positions-manager.tsx

78 lines
2.1 KiB
TypeScript
Raw Normal View History

Feat/522 close position (#1762) * feat: use close position hook and dialog setup * chore: update wallet tx interface for batch market instruction * feat: add usage of data provider to show relevant order information * feat: render correctly formatted values in close position dialog * feat: make vega tx dialog more flexibly by allowing custom ui for every state of the tx * feat: adjust text alignment and spacing between active orders and order to close * feat: add unit tests * chore: remove stray log * chore: fix lint * chore: ignore ts error for formatter function of vesting chart * feat: split components up, memozie variables * feat: add shared loading state to prevent content popping in * feat: add time in force label * feat: move transaction result hook to wallet lib * feat: prevent being able to close vega tx dialog, must reject tx * chore: add test for useTransactionResult hook * chore: fix positiosn test after hook relocation * Revert "feat: prevent being able to close vega tx dialog, must reject tx" This reverts commit d1ecda69c3c55822bb042320f82b2e1c3833b99a. * chore: add check for order edge to be defined * chore: remove close callback * feat: use tx result state to determine dialog state * chore: update close position hook to check for transaction result * fix: readd types tif selection persistance * feat: convert order event func to be async, use it in close position for more result context * fix: rename utils * chore: adjust error language Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
2022-10-24 18:16:15 +00:00
import { useRef } from 'react';
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
import { usePositionsData, PositionsTable } from '../';
import type { AgGridReact } from 'ag-grid-react';
import * as Schema from '@vegaprotocol/types';
import { useVegaTransactionStore } from '@vegaprotocol/wallet';
Feat/522 close position (#1762) * feat: use close position hook and dialog setup * chore: update wallet tx interface for batch market instruction * feat: add usage of data provider to show relevant order information * feat: render correctly formatted values in close position dialog * feat: make vega tx dialog more flexibly by allowing custom ui for every state of the tx * feat: adjust text alignment and spacing between active orders and order to close * feat: add unit tests * chore: remove stray log * chore: fix lint * chore: ignore ts error for formatter function of vesting chart * feat: split components up, memozie variables * feat: add shared loading state to prevent content popping in * feat: add time in force label * feat: move transaction result hook to wallet lib * feat: prevent being able to close vega tx dialog, must reject tx * chore: add test for useTransactionResult hook * chore: fix positiosn test after hook relocation * Revert "feat: prevent being able to close vega tx dialog, must reject tx" This reverts commit d1ecda69c3c55822bb042320f82b2e1c3833b99a. * chore: add check for order edge to be defined * chore: remove close callback * feat: use tx result state to determine dialog state * chore: update close position hook to check for transaction result * fix: readd types tif selection persistance * feat: convert order event func to be async, use it in close position for more result context * fix: rename utils * chore: adjust error language Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
2022-10-24 18:16:15 +00:00
import { t } from '@vegaprotocol/react-helpers';
interface PositionsManagerProps {
partyId: string;
onMarketClick?: (marketId: string) => void;
isReadOnly: boolean;
}
export const PositionsManager = ({
partyId,
onMarketClick,
isReadOnly,
}: PositionsManagerProps) => {
const gridRef = useRef<AgGridReact | null>(null);
const { data, error, loading, reload } = usePositionsData(
partyId,
gridRef,
true
);
const create = useVegaTransactionStore((store) => store.create);
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('-', ''),
},
],
},
});
return (
<div className="h-full relative">
<PositionsTable
rowData={error ? [] : data}
ref={gridRef}
onMarketClick={onMarketClick}
onClose={onClose}
noRowsOverlayComponent={() => null}
isReadOnly={isReadOnly}
/>
<div className="pointer-events-none absolute inset-0">
<AsyncRenderer
loading={loading}
error={error}
data={data}
noDataMessage={t('No positions')}
noDataCondition={(data) => !(data && data.length)}
reload={reload}
/>
</div>
</div>
);
};