Files
armonia-dashboard/src/utils.ts
T
anneux 6cbc8b53c1 feat(ui): add accessible toasts, inline micro-modals, agenda date filter, and sticky mobile nav
- Replace native alert/prompt with accessible Toast and MicroModal systems
- Add green toast with folio reference on payment acknowledgment
- Add amber confirmation dialog and badge update for delayed payment authorization
- Add reassuring destructive confirmation modal for slot release
- Add day-picker and Hoy/Mañana/Esta Semana filter bar to Daily Agenda view
- Refine buffer collision alerts with explicit visual time markers
- Add sticky mobile navigation bar and enforce >= 44x44px touch targets across all controls
- Pass Biome check, vp check, and vitest unit tests
2026-10-19 13:48:06 -05:00

176 lines
4.1 KiB
TypeScript

export function formatDateTime(isoStr: string): string {
try {
const dt = new Date(isoStr);
if (Number.isNaN(dt.getTime())) return isoStr;
return new Intl.DateTimeFormat("es-MX", {
weekday: "short",
day: "numeric",
month: "short",
hour: "numeric",
minute: "2-digit",
hour12: true,
}).format(dt);
} catch {
return isoStr;
}
}
export function formatTimeOnly(isoStr: string): string {
try {
const dt = new Date(isoStr);
if (Number.isNaN(dt.getTime())) return isoStr;
return new Intl.DateTimeFormat("es-MX", {
hour: "numeric",
minute: "2-digit",
hour12: true,
}).format(dt);
} catch {
return isoStr;
}
}
export function formatCurrency(amount: number): string {
return `$${amount.toLocaleString("es-MX")} MXN`;
}
export function calculateInterval(
startIso: string,
durationMinutes: number,
bufferMinutes: number,
): {
start: Date;
end: Date;
bufferEnd: Date;
startStr: string;
endStr: string;
bufferEndStr: string;
} {
const start = new Date(startIso);
const end = new Date(start.getTime() + durationMinutes * 60 * 1000);
const bufferEnd = new Date(end.getTime() + bufferMinutes * 60 * 1000);
return {
start,
end,
bufferEnd,
startStr: formatTimeOnly(start.toISOString()),
endStr: formatTimeOnly(end.toISOString()),
bufferEndStr: formatTimeOnly(bufferEnd.toISOString()),
};
}
export function getHoursRemainingBadge(hours: number): {
text: string;
urgencyClass: string;
} {
if (hours <= 0) {
return { text: "Sesión vencida / Hoy", urgencyClass: "badge-danger" };
}
if (hours <= 24) {
return {
text: `${Math.round(hours)}h restantes (Urgente)`,
urgencyClass: "badge-danger",
};
}
if (hours <= 48) {
return {
text: `${Math.round(hours)}h restantes`,
urgencyClass: "badge-warning",
};
}
return {
text: `En ${Math.round(hours / 24)} días`,
urgencyClass: "badge-info",
};
}
export function isSameDay(d1: Date, d2: Date): boolean {
return (
d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate()
);
}
export function isToday(date: Date, refDate: Date = new Date()): boolean {
return isSameDay(date, refDate);
}
export function isTomorrow(date: Date, refDate: Date = new Date()): boolean {
const tomorrow = new Date(
refDate.getFullYear(),
refDate.getMonth(),
refDate.getDate() + 1,
);
return isSameDay(date, tomorrow);
}
export function isThisWeek(date: Date, refDate: Date = new Date()): boolean {
const day = refDate.getDay();
// Week starts on Monday: (day + 6) % 7 gives 0 for Monday, 6 for Sunday
const diffToMonday = (day + 6) % 7;
const startOfWeek = new Date(
refDate.getFullYear(),
refDate.getMonth(),
refDate.getDate() - diffToMonday,
0,
0,
0,
0,
);
const endOfWeek = new Date(
startOfWeek.getTime() + 7 * 24 * 60 * 60 * 1000 - 1,
);
return date >= startOfWeek && date <= endOfWeek;
}
export function formatDateInput(date: Date): string {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
return `${y}-${m}-${d}`;
}
export interface CollisionResult {
hasCollision: boolean;
isDirectCollision: boolean;
isBufferCollision: boolean;
deficitMinutes: number;
}
export function checkBufferCollision(
currentStartIso: string,
currentDurationMinutes: number,
currentBufferMinutes: number,
nextStartIso: string,
): CollisionResult {
const currentStart = new Date(currentStartIso);
const currentEnd = new Date(
currentStart.getTime() + currentDurationMinutes * 60 * 1000,
);
const currentBufferEnd = new Date(
currentEnd.getTime() + currentBufferMinutes * 60 * 1000,
);
const nextStart = new Date(nextStartIso);
if (nextStart >= currentBufferEnd) {
return {
hasCollision: false,
isDirectCollision: false,
isBufferCollision: false,
deficitMinutes: 0,
};
}
const isDirectCollision = nextStart < currentEnd;
const deficitMs = currentBufferEnd.getTime() - nextStart.getTime();
const deficitMinutes = Math.round(deficitMs / (60 * 1000));
return {
hasCollision: true,
isDirectCollision,
isBufferCollision: !isDirectCollision,
deficitMinutes,
};
}