shoc-frontend-new/src/domain/work-orders/utils/week-day-nav-utils.ts
Arthur Bassi 426889427c feat(work-orders): show carry-over pill and map day-tab for prior-week WOs
Keep source week visible and map weekday tabs without rewriting schedule.
2026-09-01 14:16:52 -03:00

96 lines
3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type {
WorkOrderDayGroup,
WorkOrderTableRow,
} from "@/domain/work-orders/types/work-order-table-row";
import { rowMatchesCarryOverDay } from "@/domain/work-orders/utils/carry-over-display";
import { isLiveWeek } from "@/domain/work-orders/utils/list-week-utils";
import { isDateInWeek } from "@/domain/work-orders/utils/table-day-utils";
import { addDaysIso, mondayOf, todayIso } from "@/domain/work-orders/utils/wizard-date-utils";
const DAY_TAB_WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] as const;
export function weekdaysMonFri(weekMonday: string): string[] {
return Array.from({ length: 5 }, (_, index) => addDaysIso(weekMonday, index));
}
export function firstWeekday(weekMonday: string): string {
return weekMonday;
}
/** Maps a scheduled date onto the Mon–Fri tab that can show it (weekends fall back to Monday). */
export function scheduledDayTab(dateIso: string): { weekMonday: string; selectedDayIso: string } {
const day = dateIso.slice(0, 10);
const weekMonday = mondayOf(day);
const days = weekdaysMonFri(weekMonday);
return {
weekMonday,
selectedDayIso: days.includes(day) ? day : firstWeekday(weekMonday),
};
}
export function isUnassignedQueueReady(
unassignedLoading: boolean,
unassignedError: Error | null,
): boolean {
return !unassignedLoading && unassignedError == null;
}
export function isDayScopedSearchEmpty(args: {
searchActive: boolean;
unassignedCount: number;
dayItemCount: number;
unassignedQueueReady: boolean;
}): boolean {
return (
args.searchActive &&
args.unassignedQueueReady &&
args.unassignedCount === 0 &&
args.dayItemCount === 0
);
}
export function initialSelectedDay(weekMonday: string, today = todayIso()): string {
if (isLiveWeek(weekMonday) && isDateInWeek(today, weekMonday)) {
return today;
}
return firstWeekday(weekMonday);
}
export function formatDayTabLabel(iso: string): string {
if (!/^\d{4}-\d{2}-\d{2}$/.test(iso)) {
return "";
}
const day = new Date(`${iso}T12:00:00`);
const [, month, date] = iso.split("-");
return `${DAY_TAB_WEEKDAYS[day.getDay()]} · ${month}/${date}`;
}
export function rowMatchesSelectedDay(
row: Pick<WorkOrderTableRow, "scheduledOn" | "status" | "carriedOver">,
selectedDayIso: string,
weekMonday?: string,
): boolean {
if (Boolean(row.scheduledOn) && row.scheduledOn.slice(0, 10) === selectedDayIso) {
return true;
}
if (!weekMonday) {
return false;
}
return rowMatchesCarryOverDay(row, selectedDayIso, weekMonday);
}
export function buildSelectedDayGroup(
weekMonday: string,
selectedDayIso: string,
scheduled: WorkOrderTableRow[],
): WorkOrderDayGroup {
const days = weekdaysMonFri(weekMonday);
const date = days.includes(selectedDayIso) ? selectedDayIso : firstWeekday(weekMonday);
const [, month, day] = date.split("-");
return {
key: `${month}/${day}`,
label: formatDayTabLabel(date),
date,
items: scheduled.filter((row) => rowMatchesSelectedDay(row, date, weekMonday)),
};
}