EMAILOUT / frontend /src /lib /waitStepUtils.js
Seth
update
2c28af1
Raw
History Blame Contribute Delete
1.71 kB
export const WAIT_UNITS = [
{ value: 'days', label: 'Days', singular: 'day', min: 1, max: 365 },
{ value: 'hours', label: 'Hours', singular: 'hour', min: 1, max: 720 },
{ value: 'minutes', label: 'Minutes', singular: 'minute', min: 1, max: 10080 },
];
export function waitStepAmount(step) {
if (!step) return 1;
if (step.amount != null) return Number(step.amount) || 1;
if (step.days != null) return Number(step.days) || 1;
return 1;
}
export function waitStepUnit(step) {
const u = (step?.unit || 'days').toLowerCase();
if (u === 'hours' || u === 'hour') return 'hours';
if (u === 'minutes' || u === 'minute') return 'minutes';
return 'days';
}
export function waitUnitMeta(unit) {
return WAIT_UNITS.find((u) => u.value === unit) || WAIT_UNITS[0];
}
export function clampWaitAmount(amount, unit) {
const meta = waitUnitMeta(unit);
const n = Number(amount);
if (!Number.isFinite(n)) return meta.min;
return Math.max(meta.min, Math.min(meta.max, Math.round(n)));
}
export function formatWaitStep(step, { short = false } = {}) {
const amount = waitStepAmount(step);
const unit = waitStepUnit(step);
const meta = waitUnitMeta(unit);
if (short) {
if (unit === 'days') return `${amount}d`;
if (unit === 'hours') return `${amount}h`;
return `${amount}m`;
}
const word = amount === 1 ? meta.singular : meta.label.toLowerCase();
return `${amount} ${word}`;
}
export function normalizeWaitStep(step) {
if (!step || step.type !== 'wait') return step;
const unit = waitStepUnit(step);
const amount = clampWaitAmount(waitStepAmount(step), unit);
return { ...step, amount, unit };
}