chore: move market expiry to react helpers

This commit is contained in:
Matthew Russell 2024-03-09 11:32:35 +00:00
parent 9e2e6b7636
commit a90c5c3b0c
No known key found for this signature in database
14 changed files with 58 additions and 64 deletions

View File

@ -2,12 +2,11 @@ import { useAssetDetailsDialogStore } from '@vegaprotocol/assets';
import { DocsLinks, useEnvironment } from '@vegaprotocol/environment'; import { DocsLinks, useEnvironment } from '@vegaprotocol/environment';
import { ButtonLink, ExternalLink, Link } from '@vegaprotocol/ui-toolkit'; import { ButtonLink, ExternalLink, Link } from '@vegaprotocol/ui-toolkit';
import type { Market } from '@vegaprotocol/markets'; import type { Market } from '@vegaprotocol/markets';
import { addDecimalsFormatNumber, fromNanoSeconds } from '@vegaprotocol/utils';
import { import {
addDecimalsFormatNumber, useMarketExpiryDate,
fromNanoSeconds,
getExpiryDate,
getMarketExpiryDate, getMarketExpiryDate,
} from '@vegaprotocol/utils'; } from '@vegaprotocol/react-helpers';
import { import {
Last24hPriceChange, Last24hPriceChange,
Last24hVolume, Last24hVolume,
@ -264,12 +263,13 @@ export const FundingCountdown = ({ marketId }: { marketId: string }) => {
}; };
const ExpiryLabel = ({ market }: ExpiryLabelProps) => { const ExpiryLabel = ({ market }: ExpiryLabelProps) => {
const expiryDate = useMarketExpiryDate(
market.tradableInstrument.instrument.metadata.tags,
market.marketTimestamps.close,
market.state
);
const content = market.tradableInstrument.instrument.metadata.tags const content = market.tradableInstrument.instrument.metadata.tags
? getExpiryDate( ? expiryDate
market.tradableInstrument.instrument.metadata.tags,
market.marketTimestamps.close,
market.state
)
: '-'; : '-';
return <div data-testid="trading-expiry">{content}</div>; return <div data-testid="trading-expiry">{content}</div>;
}; };

View File

@ -11,10 +11,8 @@ import { useMemo } from 'react';
import type { Asset } from '@vegaprotocol/types'; import type { Asset } from '@vegaprotocol/types';
import type { ProductType } from '@vegaprotocol/types'; import type { ProductType } from '@vegaprotocol/types';
import { MarketState, MarketStateMapping } from '@vegaprotocol/types'; import { MarketState, MarketStateMapping } from '@vegaprotocol/types';
import { import { addDecimalsFormatNumber } from '@vegaprotocol/utils';
addDecimalsFormatNumber, import { getMarketExpiryDate } from '@vegaprotocol/react-helpers';
getMarketExpiryDate,
} from '@vegaprotocol/utils';
import { closedMarketsWithDataProvider, getAsset } from '@vegaprotocol/markets'; import { closedMarketsWithDataProvider, getAsset } from '@vegaprotocol/markets';
import type { DataSourceFilterFragment } from '@vegaprotocol/markets'; import type { DataSourceFilterFragment } from '@vegaprotocol/markets';
import { useAssetDetailsDialogStore } from '@vegaprotocol/assets'; import { useAssetDetailsDialogStore } from '@vegaprotocol/assets';

View File

@ -7,11 +7,8 @@ import {
useSuccessorMarket, useSuccessorMarket,
type Market, type Market,
} from '@vegaprotocol/markets'; } from '@vegaprotocol/markets';
import { import { addDecimalsFormatNumber, isNumeric } from '@vegaprotocol/utils';
addDecimalsFormatNumber, import { getMarketExpiryDate } from '@vegaprotocol/react-helpers';
getMarketExpiryDate,
isNumeric,
} from '@vegaprotocol/utils';
import { useT, ns } from '../../lib/use-t'; import { useT, ns } from '../../lib/use-t';
import { Links } from '../../lib/links'; import { Links } from '../../lib/links';

View File

@ -9,7 +9,7 @@ import {
import { useProfileDialogStore } from '../../stores/profile-dialog-store'; import { useProfileDialogStore } from '../../stores/profile-dialog-store';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { useT } from '../../lib/use-t'; import { useT } from '../../lib/use-t';
import { useRequired } from '@vegaprotocol/utils'; import { useRequired } from '@vegaprotocol/react-helpers';
import { import {
useSimpleTransaction, useSimpleTransaction,
type Status, type Status,

View File

@ -16,6 +16,7 @@ import en_markets from './locales/en/markets.json';
import en_web3 from './locales/en/web3.json'; import en_web3 from './locales/en/web3.json';
import en_proposals from './locales/en/proposals.json'; import en_proposals from './locales/en/proposals.json';
import en_positions from './locales/en/positions.json'; import en_positions from './locales/en/positions.json';
import en_react_helpers from './locales/en/react-helpers.json';
import en_trades from './locales/en/trading.json'; import en_trades from './locales/en/trading.json';
import en_ui_toolkit from './locales/en/ui-toolkit.json'; import en_ui_toolkit from './locales/en/ui-toolkit.json';
import en_wallet from './locales/en/wallet.json'; import en_wallet from './locales/en/wallet.json';
@ -39,6 +40,7 @@ export const locales = {
web3: en_web3, web3: en_web3,
positions: en_positions, positions: en_positions,
proposals: en_proposals, proposals: en_proposals,
react_helpers: en_react_helpers,
trades: en_trades, trades: en_trades,
'ui-toolkit': en_ui_toolkit, 'ui-toolkit': en_ui_toolkit,
wallet: en_wallet, wallet: en_wallet,

View File

@ -27,8 +27,8 @@ import {
formatNumber, formatNumber,
formatNumberPercentage, formatNumberPercentage,
getDateTimeFormat, getDateTimeFormat,
getMarketExpiryDateFormatted,
} from '@vegaprotocol/utils'; } from '@vegaprotocol/utils';
import { getMarketExpiryDateFormatted } from '@vegaprotocol/react-helpers';
import type { Get } from 'type-fest'; import type { Get } from 'type-fest';
import { MarketInfoTable } from './info-key-value-table'; import { MarketInfoTable } from './info-key-value-table';
import type { import type {

View File

@ -13,6 +13,7 @@ export * from './use-storybook-theme-observer';
export * from './use-yesterday'; export * from './use-yesterday';
export * from './use-previous'; export * from './use-previous';
export * from './use-validate'; export * from './use-validate';
export * from './use-market-expiry-date';
export { useScript } from './use-script'; export { useScript } from './use-script';
export { useUserAgent } from './use-user-agent'; export { useUserAgent } from './use-user-agent';
export { useFormatTrigger } from './use-format-trigger'; export { useFormatTrigger } from './use-format-trigger';

View File

@ -1,9 +1,7 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import { type StopOrder, StopOrderTriggerDirection } from '@vegaprotocol/types'; import { type StopOrder, StopOrderTriggerDirection } from '@vegaprotocol/types';
import { addDecimalsFormatNumber } from '@vegaprotocol/utils'; import { addDecimalsFormatNumber } from '@vegaprotocol/utils';
import { useT } from './use-t';
// TODO: add real useT func
const useT = () => (str: string) => str;
export const useFormatTrigger = () => { export const useFormatTrigger = () => {
const t = useT(); const t = useT();

View File

@ -1,42 +1,10 @@
import { MarketState } from '@vegaprotocol/types'; import { MarketState } from '@vegaprotocol/types';
import { getDateTimeFormat } from '@vegaprotocol/utils';
import { isValid, parseISO } from 'date-fns'; import { isValid, parseISO } from 'date-fns';
import { getDateTimeFormat } from './format';
import { useT } from './use-t'; import { useT } from './use-t';
export const getMarketExpiryDate = ( export const useMarketExpiryDate = (
tags?: ReadonlyArray<string> | null tags: ReadonlyArray<string> | null | undefined,
): Date | null => {
if (tags) {
const dateFound = tags.reduce<Date | null>((agg, tag) => {
const parsed = parseISO(
(tag.match(/^settlement.*:/) &&
tag
.split(':')
.filter((item, i) => i)
.join(':')) as string
);
if (isValid(parsed)) {
agg = parsed;
}
return agg;
}, null);
return dateFound;
}
return null;
};
export const getMarketExpiryDateFormatted = (
tags?: ReadonlyArray<string> | null
): string | null => {
if (tags) {
const dateFound = getMarketExpiryDate(tags);
return dateFound ? getDateTimeFormat().format(dateFound) : null;
}
return null;
};
export const getExpiryDate = (
tags: ReadonlyArray<string> | null,
close: string | null, close: string | null,
state: MarketState state: MarketState
): string => { ): string => {
@ -67,3 +35,35 @@ export const getExpiryDate = (
} }
return content; return content;
}; };
export const getMarketExpiryDateFormatted = (
tags?: ReadonlyArray<string> | null
): string | null => {
if (tags) {
const dateFound = getMarketExpiryDate(tags);
return dateFound ? getDateTimeFormat().format(dateFound) : null;
}
return null;
};
export const getMarketExpiryDate = (
tags?: ReadonlyArray<string> | null
): Date | null => {
if (tags) {
const dateFound = tags.reduce<Date | null>((agg, tag) => {
const parsed = parseISO(
(tag.match(/^settlement.*:/) &&
tag
.split(':')
.filter((item, i) => i)
.join(':')) as string
);
if (isValid(parsed)) {
agg = parsed;
}
return agg;
}, null);
return dateFound;
}
return null;
};

View File

@ -1,3 +1,3 @@
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
export const ns = 'utils'; export const ns = 'react-helpers';
export const useT = () => useTranslation(ns).t; export const useT = () => useTranslation(ns).t;

View File

@ -1,9 +1,7 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import * as utils from '@vegaprotocol/utils'; import * as utils from '@vegaprotocol/utils';
import { useT } from './use-t';
// TODO: add i18n to react helpers
const useT = () => (str: string) => str;
export const useRequired = () => { export const useRequired = () => {
const t = useT(); const t = useT();

View File

@ -17,7 +17,8 @@ import {
type SymbolQuery, type SymbolQuery,
type SymbolQueryVariables, type SymbolQueryVariables,
} from './__generated__/Symbol'; } from './__generated__/Symbol';
import { getMarketExpiryDate, toBigNum } from '@vegaprotocol/utils'; import { toBigNum } from '@vegaprotocol/utils';
import { getMarketExpiryDate } from '@vegaprotocol/react-helpers';
import { import {
type IBasicDataFeed, type IBasicDataFeed,
type DatafeedConfiguration, type DatafeedConfiguration,

View File

@ -7,7 +7,6 @@ export * from './lib/helpers';
export * from './lib/is-asset-erc20'; export * from './lib/is-asset-erc20';
export * from './lib/is-valid-url'; export * from './lib/is-valid-url';
export * from './lib/local-storage'; export * from './lib/local-storage';
export * from './lib/markets';
export * from './lib/price-change'; export * from './lib/price-change';
export * from './lib/remove-0x'; export * from './lib/remove-0x';
export * from './lib/remove-pagination-wrapper'; export * from './lib/remove-pagination-wrapper';