2022-10-11 12:30:07 +00:00
|
|
|
import { useCallback, useMemo, useState } from 'react';
|
|
|
|
import { t, useDataProvider } from '@vegaprotocol/react-helpers';
|
|
|
|
import type { DealTicketMarketFragment } from '@vegaprotocol/deal-ticket';
|
|
|
|
import { compileGridData, TradingModeTooltip } from '@vegaprotocol/deal-ticket';
|
|
|
|
import type { Schema as Types } from '@vegaprotocol/types';
|
|
|
|
import {
|
|
|
|
AuctionTriggerMapping,
|
|
|
|
MarketTradingModeMapping,
|
2022-11-15 12:31:25 +00:00
|
|
|
Schema,
|
2022-10-11 12:30:07 +00:00
|
|
|
} from '@vegaprotocol/types';
|
|
|
|
import type {
|
|
|
|
MarketData,
|
|
|
|
MarketDataUpdateFieldsFragment,
|
|
|
|
SingleMarketFieldsFragment,
|
|
|
|
} from '@vegaprotocol/market-list';
|
|
|
|
import { marketDataProvider, marketProvider } from '@vegaprotocol/market-list';
|
|
|
|
import { HeaderStat } from '../header';
|
|
|
|
|
|
|
|
interface Props {
|
2022-11-18 17:08:48 +00:00
|
|
|
marketId?: string;
|
|
|
|
onSelect?: (marketId: string) => void;
|
2022-10-11 12:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TradingModeMarket = Omit<DealTicketMarketFragment, 'depth'>;
|
|
|
|
|
|
|
|
export const MarketTradingModeComponent = ({ marketId, onSelect }: Props) => {
|
|
|
|
const [tradingMode, setTradingMode] =
|
|
|
|
useState<Types.MarketTradingMode | null>(null);
|
|
|
|
const [trigger, setTrigger] = useState<Types.AuctionTrigger | null>(null);
|
|
|
|
const [market, setMarket] = useState<TradingModeMarket | null>(null);
|
|
|
|
const variables = useMemo(
|
|
|
|
() => ({
|
|
|
|
marketId: marketId,
|
|
|
|
}),
|
|
|
|
[marketId]
|
|
|
|
);
|
|
|
|
|
|
|
|
const { data } = useDataProvider<SingleMarketFieldsFragment, never>({
|
|
|
|
dataProvider: marketProvider,
|
|
|
|
variables,
|
|
|
|
skip: !marketId,
|
|
|
|
});
|
|
|
|
|
|
|
|
const update = useCallback(
|
2022-11-10 19:08:13 +00:00
|
|
|
({ data: marketData }: { data: MarketData | null }) => {
|
|
|
|
if (marketData) {
|
|
|
|
setTradingMode(marketData.marketTradingMode);
|
|
|
|
setTrigger(marketData.trigger);
|
|
|
|
setMarket({
|
|
|
|
...data,
|
|
|
|
data: marketData,
|
|
|
|
} as TradingModeMarket);
|
|
|
|
}
|
2022-10-11 12:30:07 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[data]
|
|
|
|
);
|
|
|
|
|
|
|
|
useDataProvider<MarketData, MarketDataUpdateFieldsFragment>({
|
|
|
|
dataProvider: marketDataProvider,
|
|
|
|
update,
|
|
|
|
variables,
|
|
|
|
skip: !marketId || !data,
|
|
|
|
});
|
|
|
|
|
2022-11-18 17:08:48 +00:00
|
|
|
const content =
|
|
|
|
tradingMode === Schema.MarketTradingMode.TRADING_MODE_MONITORING_AUCTION &&
|
|
|
|
trigger &&
|
|
|
|
trigger !== Schema.AuctionTrigger.AUCTION_TRIGGER_UNSPECIFIED
|
|
|
|
? `${MarketTradingModeMapping[tradingMode]} - ${AuctionTriggerMapping[trigger]}`
|
|
|
|
: MarketTradingModeMapping[tradingMode as Types.MarketTradingMode];
|
|
|
|
|
2022-10-11 12:30:07 +00:00
|
|
|
return (
|
|
|
|
<HeaderStat
|
|
|
|
heading={t('Trading mode')}
|
|
|
|
description={
|
|
|
|
market && (
|
|
|
|
<TradingModeTooltip
|
|
|
|
tradingMode={tradingMode}
|
|
|
|
trigger={trigger}
|
|
|
|
compiledGrid={compileGridData(market, onSelect)}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2022-10-14 08:15:34 +00:00
|
|
|
testId="market-trading-mode"
|
2022-10-11 12:30:07 +00:00
|
|
|
>
|
2022-11-18 17:08:48 +00:00
|
|
|
<div>{content || '-'}</div>
|
2022-10-11 12:30:07 +00:00
|
|
|
</HeaderStat>
|
|
|
|
);
|
|
|
|
};
|