From c0942d56a16d434e0fff3ab81fc1e49927596e10 Mon Sep 17 00:00:00 2001 From: Art Date: Tue, 12 Mar 2024 14:48:44 +0100 Subject: [PATCH] fix(trading): crashing tooltip, enactment date from batch proposal (#5979) --- .../trading-mode-tooltip.tsx | 49 ++++++++++++------- .../src/lib/proposals-hooks/Proposal.graphql | 8 +++ .../proposals-hooks/__generated__/Proposal.ts | 10 +++- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/libs/deal-ticket/src/components/trading-mode-tooltip/trading-mode-tooltip.tsx b/libs/deal-ticket/src/components/trading-mode-tooltip/trading-mode-tooltip.tsx index 56b6421bb..79a30fd3f 100644 --- a/libs/deal-ticket/src/components/trading-mode-tooltip/trading-mode-tooltip.tsx +++ b/libs/deal-ticket/src/components/trading-mode-tooltip/trading-mode-tooltip.tsx @@ -1,11 +1,7 @@ import { useMemo } from 'react'; import { parseISO, isValid, isAfter } from 'date-fns'; import classNames from 'classnames'; -import { - useProposalOfMarketQuery, - type ProposalOfMarketQuery, - type SingleProposal, -} from '@vegaprotocol/proposals'; +import { useProposalOfMarketQuery } from '@vegaprotocol/proposals'; import { DocsLinks } from '@vegaprotocol/environment'; import { getDateTimeFormat } from '@vegaprotocol/utils'; import * as Schema from '@vegaprotocol/types'; @@ -21,6 +17,32 @@ type TradingModeTooltipProps = { skipGrid?: boolean; }; +const useEnactmentDatetime = (marketId?: string, skip?: boolean) => { + const variables = useMemo(() => ({ marketId: marketId || '' }), [marketId]); + const { data: proposalData } = useProposalOfMarketQuery({ + variables, + skip, + }); + + let v: string = ''; + if (proposalData?.proposal) { + if (proposalData?.proposal?.__typename === 'Proposal') { + v = proposalData.proposal.terms.enactmentDatetime; + } + if ( + proposalData?.proposal?.__typename === 'BatchProposal' && + proposalData.proposal.subProposals + ) { + const sub = proposalData.proposal.subProposals.find( + (p) => p?.id === variables.marketId + ); + v = sub?.terms?.enactmentDatetime; + } + } + + return parseISO(v); +}; + export const TradingModeTooltip = ({ marketId, onSelect, @@ -31,24 +53,15 @@ export const TradingModeTooltip = ({ const { data: market } = useMarket(marketId); const { data: marketData } = useStaticMarketData(marketId, skip); const { marketTradingMode, trigger } = marketData || {}; - const variables = useMemo(() => ({ marketId: marketId || '' }), [marketId]); - const { data: proposalData } = useProposalOfMarketQuery({ - variables, - skip: - !marketTradingMode || - Schema.MarketTradingMode.TRADING_MODE_OPENING_AUCTION !== - marketTradingMode, - }); - // We only fetch Proposals (and not BatchProposals) - const proposal = proposalData?.proposal as SingleProposal< - ProposalOfMarketQuery['proposal'] - >; + const skipEnactmentDate = + !marketTradingMode || + Schema.MarketTradingMode.TRADING_MODE_OPENING_AUCTION !== marketTradingMode; + const enactmentDate = useEnactmentDatetime(marketId, skipEnactmentDate); if (!market || !marketData) { return null; } - const enactmentDate = parseISO(proposal?.terms.enactmentDatetime); const compiledGrid = !skipGrid && compileGridData(t, market, marketData, onSelect); diff --git a/libs/proposals/src/lib/proposals-hooks/Proposal.graphql b/libs/proposals/src/lib/proposals-hooks/Proposal.graphql index 009fa78dd..81d5fa647 100644 --- a/libs/proposals/src/lib/proposals-hooks/Proposal.graphql +++ b/libs/proposals/src/lib/proposals-hooks/Proposal.graphql @@ -45,6 +45,14 @@ query ProposalOfMarket($marketId: ID!) { enactmentDatetime } } + ... on BatchProposal { + subProposals { + id + terms { + enactmentDatetime + } + } + } } } diff --git a/libs/proposals/src/lib/proposals-hooks/__generated__/Proposal.ts b/libs/proposals/src/lib/proposals-hooks/__generated__/Proposal.ts index d4e1a5cd4..40fadb768 100644 --- a/libs/proposals/src/lib/proposals-hooks/__generated__/Proposal.ts +++ b/libs/proposals/src/lib/proposals-hooks/__generated__/Proposal.ts @@ -25,7 +25,7 @@ export type ProposalOfMarketQueryVariables = Types.Exact<{ }>; -export type ProposalOfMarketQuery = { __typename?: 'Query', proposal?: { __typename?: 'BatchProposal' } | { __typename?: 'Proposal', id?: string | null, terms: { __typename?: 'ProposalTerms', enactmentDatetime?: any | null } } | null }; +export type ProposalOfMarketQuery = { __typename?: 'Query', proposal?: { __typename?: 'BatchProposal', subProposals?: Array<{ __typename?: 'ProposalDetail', id?: string | null, terms?: { __typename?: 'ProposalTerms', enactmentDatetime?: any | null } | null } | null> | null } | { __typename?: 'Proposal', id?: string | null, terms: { __typename?: 'ProposalTerms', enactmentDatetime?: any | null } } | null }; export type SuccessorMarketProposalDetailsQueryVariables = Types.Exact<{ proposalId: Types.Scalars['ID']; @@ -152,6 +152,14 @@ export const ProposalOfMarketDocument = gql` enactmentDatetime } } + ... on BatchProposal { + subProposals { + id + terms { + enactmentDatetime + } + } + } } } `;