mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-05 11:52:03 +00:00
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
|
|
import type {
|
||
|
|
WorkOrderDayGroup,
|
||
|
|
WorkOrderTableRow,
|
||
|
|
} from "@/domain/work-orders/types/work-order-table-row";
|
||
|
|
import { isLiveWeek } from "@/domain/work-orders/utils/list-week-utils";
|
||
|
|
import { isDateInWeek } from "@/domain/work-orders/utils/table-day-utils";
|
||
|
|
import { addDaysIso, 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;
|
||
|
|
}
|
||
|
|
|
||
|
|
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">,
|
||
|
|
selectedDayIso: string,
|
||
|
|
): boolean {
|
||
|
|
return Boolean(row.scheduledOn) && row.scheduledOn.slice(0, 10) === selectedDayIso;
|
||
|
|
}
|
||
|
|
|
||
|
|
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)),
|
||
|
|
};
|
||
|
|
}
|