chore: improve typings for product type

This commit is contained in:
Matthew Russell
2023-08-17 09:14:29 +01:00
parent e926a014d2
commit 16ac68ede8
7 changed files with 42 additions and 25 deletions
@@ -2,28 +2,22 @@ import type { MouseEvent } from 'react';
import { useCallback } from 'react';
import get from 'lodash/get';
import { Pill } from '@vegaprotocol/ui-toolkit';
import type { Market } from '@vegaprotocol/types';
const productTypeMap = {
Future: 'Futr',
FutureProduct: 'Futr',
Spot: 'Spot',
SpotProduct: 'Spot',
Perpetual: 'Perp',
PerpetualProduct: 'Perp',
} as const;
export type ProductType = keyof typeof productTypeMap | undefined;
import {
ProductTypeShortName,
type Market,
type ProductType,
} from '@vegaprotocol/types';
export const MarketProductPill = ({
productType,
}: {
productType?: ProductType;
productType: ProductType;
}) => {
return productType ? (
return (
<Pill size="xxs" className="uppercase ml-0.5" title={productType}>
{productTypeMap[productType] || productType}
{ProductTypeShortName[productType]}
</Pill>
) : null;
);
};
interface MarketNameCellProps {
@@ -66,7 +60,7 @@ export const MarketNameCell = ({
<span data-testid="market-code" data-market-id={id}>
{value}
</span>
<MarketProductPill productType={productType} />
{productType && <MarketProductPill productType={productType} />}
</>
);
return onMarketClick && id ? (
@@ -26,7 +26,7 @@ import {
PositionsDocument,
PositionsSubscriptionDocument,
} from './__generated__/Positions';
import type { PositionStatus } from '@vegaprotocol/types';
import type { PositionStatus, ProductType } from '@vegaprotocol/types';
export interface Position {
assetId: string;
@@ -51,7 +51,7 @@ export interface Position {
totalBalance: string;
unrealisedPNL: string;
updatedAt: string | null;
productType?: string;
productType: ProductType;
}
export const getMetrics = (
@@ -189,13 +189,8 @@ describe('Positions', () => {
/>
);
});
<<<<<<< HEAD
const cells = screen.getAllByRole('gridcell');
expect(cells[11].textContent).toEqual('');
=======
expect(screen.getByRole('button', { name: 'Close' })).toBeInTheDocument();
>>>>>>> 997dc6380 (test: update tests after tooltip changes)
expect(screen.getByTestId('close-position')).toBeInTheDocument();
});
it('do not display close button if openVolume is zero', async () => {
+16 -1
View File
@@ -21,6 +21,7 @@ import {
ExternalLink,
VegaIcon,
VegaIconNames,
Pill,
} from '@vegaprotocol/ui-toolkit';
import {
volumePrefix,
@@ -122,7 +123,21 @@ export const PositionsTable = ({
}: VegaICellRendererParams<Position, 'marketCode'>) => {
if (!data || !value) return '-';
return (
<StackedCell primary={value} secondary={data?.assetSymbol} />
<StackedCell
primary={value}
secondary={
<>
{data?.assetSymbol}
<Pill
size="xxs"
className="uppercase ml-0.5"
title={productType}
>
{productTypeMap[productType] || productType}
</Pill>
</>
}
/>
);
},
},
+9
View File
@@ -25,6 +25,7 @@ import type {
DispatchMetric,
StopOrderStatus,
} from './__generated__/types';
import type { ProductType } from './product';
export const AccountTypeMapping: {
[T in AccountType]: string;
@@ -513,3 +514,11 @@ export const PeggedReferenceMapping: { [R in PeggedReference]: string } = {
PEGGED_REFERENCE_BEST_BID: 'Bid',
PEGGED_REFERENCE_MID: 'Mid',
};
export const ProductTypeMapping: Record<ProductType, string> = {
Future: 'Future',
};
export const ProductTypeShortName: Record<ProductType, string> = {
Future: 'FUTR',
};
+1
View File
@@ -1,3 +1,4 @@
export * from './__generated__/types';
export * from './candle';
export * from './global-types-mappings';
export * from './product';
+3
View File
@@ -0,0 +1,3 @@
import type { Product } from './__generated__/types';
export type ProductType = NonNullable<Product['__typename']>;