wealth/packages/island/src/organisms/prediction-card/components/PredictionBody.tsx

120 lines
3.4 KiB
TypeScript

import { Typography } from '../../../atoms/typography';
import type { PredictionBodyProps } from '../types';
import {
nowTime,
predictionMath,
resolveOutcome,
resolveStatus,
toTime,
} from '../utils';
import { ClosedHero } from './ClosedHero';
import { HorizonProgress } from './HorizonProgress';
import { OpenKpis } from './OpenKpis';
import { PredictionBadges } from './PredictionBadges';
import { PredictionHead } from './PredictionHead';
import { PredictionLadder } from './PredictionLadder';
import { PredictionLevels } from './PredictionLevels';
import { PredictionMeta } from './PredictionMeta';
import { PendingCloseRow } from './PendingCloseRow';
import { PredictionRange } from './PredictionRange';
/** Header, hero, range bar, then the levels (compact) or the ladder, progress and caption (expanded). */
export function PredictionBody({
prediction: data,
pendingClose,
href,
expanded,
premium,
caption,
publishedAt,
now,
labels,
}: PredictionBodyProps) {
const math = predictionMath(data);
const isOpen = resolveStatus(data) === 'open';
const prediction = { ...data, outcome: resolveOutcome(data, math.roi) };
const { symbol, market, expiresAt, closedAt, outcome } = prediction;
const nowMs = nowTime(now);
const secondsLeft =
isOpen && expiresAt != null ? (toTime(expiresAt) - nowMs) / 1000 : 0;
const part = { prediction, math, labels };
return (
<>
<PredictionHead
href={href}
title={
<>
<Typography
tag="bdi"
dir="ltr"
variant={expanded ? 'heading-md' : 'heading-sm'}
className="font-bold tracking-[0.01em]"
>
{symbol}
</Typography>
{market != null && (
<Typography tag="span" variant="caption" color="secondary">
{market}
</Typography>
)}
</>
}
meta={
<PredictionMeta
prediction={prediction}
secondsLeft={secondsLeft > 0 ? secondsLeft : null}
labels={labels}
/>
}
badges={
<PredictionBadges
status={isOpen ? 'open' : 'closed'}
outcome={outcome}
premium={premium}
labels={labels}
/>
}
/>
{isOpen && pendingClose != null && labels.pendingClose != null && (
<PendingCloseRow title={labels.pendingClose}>
{pendingClose}
</PendingCloseRow>
)}
{!isOpen && math.roi != null ? (
<ClosedHero
{...part}
expanded={expanded}
heldMs={
closedAt != null ? toTime(closedAt) - toTime(publishedAt) : null
}
/>
) : (
<OpenKpis {...part} />
)}
<PredictionRange {...part} expanded={expanded} />
{expanded ? (
<PredictionLadder {...part} />
) : (
<PredictionLevels {...part} />
)}
{expanded && isOpen && expiresAt != null && (
<HorizonProgress
publishedAt={publishedAt}
expiresAt={expiresAt}
nowMs={nowMs}
labels={labels}
/>
)}
{expanded && caption != null && (
<Typography
tag="div"
className="rounded-md bg-surface-sunken px-4 py-3 break-words whitespace-pre-wrap"
>
{caption}
</Typography>
)}
</>
);
}