2023-05-02 17:41:21 +00:00
|
|
|
import { DApp, EXPLORER_ORACLE, useLinks } from '@vegaprotocol/environment';
|
|
|
|
import { MarketState } from '@vegaprotocol/types';
|
|
|
|
import { Link } from '@vegaprotocol/ui-toolkit';
|
|
|
|
import { getDateTimeFormat } from '@vegaprotocol/utils';
|
2023-11-16 03:10:39 +00:00
|
|
|
import { useT } from '../../lib/use-t';
|
2023-05-02 17:41:21 +00:00
|
|
|
import { formatDistanceToNowStrict, isAfter } from 'date-fns';
|
|
|
|
|
|
|
|
export interface SettlementDataCellProps {
|
|
|
|
oracleSpecId: string;
|
|
|
|
metaDate: Date | null;
|
|
|
|
closeTimestamp: string | null;
|
|
|
|
marketState: MarketState;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const SettlementDateCell = ({
|
|
|
|
oracleSpecId,
|
|
|
|
metaDate,
|
|
|
|
closeTimestamp,
|
|
|
|
marketState,
|
|
|
|
}: SettlementDataCellProps) => {
|
2023-11-16 03:10:39 +00:00
|
|
|
const t = useT();
|
2023-05-02 17:41:21 +00:00
|
|
|
const linkCreator = useLinks(DApp.Explorer);
|
|
|
|
const date = closeTimestamp ? new Date(closeTimestamp) : metaDate;
|
|
|
|
|
|
|
|
let text = '';
|
|
|
|
if (!date) {
|
|
|
|
text = t('Unknown');
|
|
|
|
} else {
|
|
|
|
// pass Date.now() to date constructor for easier mocking
|
|
|
|
const expiryHasPassed = isAfter(new Date(Date.now()), date);
|
|
|
|
const distance = formatDistanceToNowStrict(date); // X days/mins ago
|
|
|
|
|
|
|
|
if (expiryHasPassed) {
|
|
|
|
if (marketState !== MarketState.STATE_SETTLED) {
|
2023-11-16 03:10:39 +00:00
|
|
|
text = t('Expected {{distance}} ago', { distance });
|
2023-05-02 17:41:21 +00:00
|
|
|
} else {
|
2023-11-16 03:10:39 +00:00
|
|
|
text = t('{{distance}} ago', { distance });
|
2023-05-02 17:41:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-16 03:10:39 +00:00
|
|
|
text = t('Expected in {{distance}}', { distance });
|
2023-05-02 17:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Link
|
|
|
|
href={linkCreator(EXPLORER_ORACLE.replace(':id', oracleSpecId))}
|
|
|
|
className="underline"
|
|
|
|
target="_blank"
|
|
|
|
title={
|
|
|
|
date ? getDateTimeFormat().format(date) : t('Unknown settlement date')
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{text}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
};
|