shoc-frontend-new/src/domain/work-orders/utils/table-day-utils.ts

46 lines
1.3 KiB
TypeScript

import { addDaysIso, pad2 } from "@/domain/work-orders/utils/wizard-date-utils";
export const WEEKDAY_LABELS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"] as const;
export function deriveDayFields(rawDate: string): {
dayGroup: string;
dayLabel: string;
} {
const dayGroup = rawDate ? rawDate.replace(/^\d{4}-(\d{2})-(\d{2})$/, "$1/$2") : "";
const dayLabel = rawDate
? (() => {
const d = new Date(`${rawDate}T12:00:00`);
return `${WEEKDAY_LABELS[d.getDay()]} · ${dayGroup}`;
})()
: "";
return { dayGroup, dayLabel };
}
export function buildWeekSkeleton(weekMonday: string): {
key: string;
label: string;
date: string;
items: never[];
}[] {
const base = new Date(`${weekMonday}T12:00:00`);
return Array.from({ length: 5 }, (_, i) => {
const d = new Date(base);
d.setDate(base.getDate() + i);
const key = `${pad2(d.getMonth() + 1)}/${pad2(d.getDate())}`;
const iso = `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`;
return {
key,
label: `${WEEKDAY_LABELS[d.getDay()]} · ${key}`,
date: iso,
items: [],
};
});
}
export function isDateInWeek(iso: string, weekMonday: string): boolean {
if (!iso || !/^\d{4}-\d{2}-\d{2}$/.test(iso)) {
return false;
}
const friday = addDaysIso(weekMonday, 4);
return iso >= weekMonday && iso <= friday;
}