113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
import { clock, date } from '@lumina/tools/format';
|
|
import type { ReactNode } from 'react';
|
|
|
|
import { Element } from '../../primitives/element';
|
|
import type { IconName } from '../icon';
|
|
import {
|
|
DAY_SECONDS,
|
|
HOUR_SECONDS,
|
|
PROMOTION_URGENT_HOURS,
|
|
REMAINING_WARNING_MIN_DAYS,
|
|
} from './constants';
|
|
import { countdownTone } from './tone';
|
|
import type { CountdownLabels, CountdownProps, CountdownTone } from './types';
|
|
|
|
export type CountdownState = {
|
|
tone: CountdownTone;
|
|
icon?: IconName;
|
|
text: ReactNode;
|
|
/** The OTP clock is still running: `role="timer"`. */
|
|
isTimer: boolean;
|
|
};
|
|
|
|
type StateInput = Pick<CountdownProps, 'kind' | 'prefix'> & {
|
|
labels: CountdownLabels;
|
|
/** Seconds left; `null` while a live clock doesn't know the time yet. */
|
|
left: number | null;
|
|
/** The end as epoch ms, for the promotion date. */
|
|
end: number | null;
|
|
};
|
|
|
|
/** The prefix stays RTL, outside the clock's LTR isolate. */
|
|
function otpClock(prefix: ReactNode, seconds: number): ReactNode {
|
|
return (
|
|
<>
|
|
{prefix}
|
|
<Element tag="bdi" dir="ltr">
|
|
{clock(seconds)}
|
|
</Element>
|
|
</>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The brief's wording and escalation for a given time left, shared by the server render and the
|
|
* live clock. Returns `null` when nothing should show (a promotion that ended, a missing label).
|
|
*/
|
|
export function countdownState({
|
|
kind,
|
|
prefix,
|
|
labels,
|
|
left,
|
|
end,
|
|
}: StateInput): CountdownState | null {
|
|
if (kind === 'otp') {
|
|
// Unknown time: the clock's own width, so nothing moves when it fills in.
|
|
if (left == null)
|
|
return {
|
|
tone: 'calm',
|
|
icon: 'clock',
|
|
text: otpClock(prefix, 0),
|
|
isTimer: false,
|
|
};
|
|
if (left === 0)
|
|
return labels.expired == null
|
|
? null
|
|
: { tone: 'danger', text: labels.expired, isTimer: false };
|
|
return {
|
|
tone: countdownTone(kind, left),
|
|
icon: 'clock',
|
|
text: otpClock(prefix, left),
|
|
isTimer: true,
|
|
};
|
|
}
|
|
if (left == null) return null;
|
|
|
|
let state: Omit<CountdownState, 'isTimer'>;
|
|
if (kind === 'promotion') {
|
|
const hours = left / HOUR_SECONDS;
|
|
if (end == null || hours <= 0) return null;
|
|
state =
|
|
hours > PROMOTION_URGENT_HOURS
|
|
? { tone: 'calm', text: labels.until?.(date(end, { time: false })) }
|
|
: {
|
|
tone: 'warning',
|
|
icon: 'clock',
|
|
text:
|
|
hours < 1
|
|
? labels.underAnHour
|
|
: labels.hoursLeft?.(Math.floor(hours)),
|
|
};
|
|
} else {
|
|
const days = left / DAY_SECONDS;
|
|
const tone = countdownTone(kind, left);
|
|
state =
|
|
tone === 'calm'
|
|
? { tone, text: labels.daysLeft?.(Math.floor(days)) }
|
|
: tone === 'warning'
|
|
? {
|
|
tone,
|
|
text: labels.daysLeft?.(
|
|
Math.max(REMAINING_WARNING_MIN_DAYS, Math.floor(days)),
|
|
),
|
|
}
|
|
: // Partial days round up: 2.1 days reads «کمتر از ۳ روز مانده»; past the end, 1.
|
|
{
|
|
tone,
|
|
icon: 'alert',
|
|
text: labels.underDays?.(Math.max(1, Math.ceil(days))),
|
|
};
|
|
}
|
|
return state.text == null ? null : { ...state, isTimer: false };
|
|
}
|