"use client"; import ShiftCard from "@/components/molecules/cards/ShiftCard"; import { DEPARTMENTS } from "@/lib/constants"; import { useShift } from "@/lib/shift-context"; import { useMemo, useState } from "react"; import type { WeeklyScheduleProps } from "./ShiftSchedule.types"; const MONTH_NAMES = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ]; const DAY_LABELS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; function formatDateShort(d: Date): string { return `${d.getDate().toString().padStart(2, "0")}/${(d.getMonth() + 1).toString().padStart(2, "0")}`; } function isToday(d: Date): boolean { const today = new Date(Date.now()); return ( d.getDate() === today.getDate() && d.getMonth() === today.getMonth() && d.getFullYear() === today.getFullYear() ); } export default function WeeklySchedule({ onShiftClick, onCreateShift, mobileCalendarHeader = false, }: WeeklyScheduleProps) { const { currentDate, getWeekDates, getShiftsForDate, goToNextWeek, goToPrevWeek, } = useShift(); const weekDates = getWeekDates(); const [selectedDate, setSelectedDate] = useState( weekDates[0] ?? currentDate, ); const selectedDateRef = useMemo(() => { return ( weekDates.find((d) => d === selectedDate) ?? weekDates[0] ?? currentDate ); }, [selectedDate, weekDates, currentDate]); // ── Mobile calendar header view ──────────────────────────────────────────── if (mobileCalendarHeader) { const dayShifts = getShiftsForDate(new Date(selectedDateRef)); return (
{/* Week strip */}

{MONTH_NAMES[currentDate.getMonth()]} {currentDate.getFullYear()}

{weekDates.map((date, i) => { const active = date === selectedDateRef; const today = isToday(new Date(date)); const shifts = getShiftsForDate(new Date(date)); return ( ); })}
{/* Day shifts */}

{DAY_LABELS[(new Date(selectedDateRef).getDay() + 6) % 7]},{" "} {formatDateShort(new Date(selectedDateRef))}

{dayShifts.length} shifts
{dayShifts.length === 0 && !onCreateShift ? (

No shifts today

) : (
{dayShifts.map((shift) => ( ))} {onCreateShift && ( )}
)}
); } // ── Desktop table view ────────────────────────────────────────────────────── return (
{weekDates.map((date, i) => { const today = isToday(new Date(date)); return ( ); })} {DEPARTMENTS.map((dept, deptIdx) => ( {weekDates.map((date, i) => { const today = isToday(new Date(date)); const shifts = getShiftsForDate(new Date(date)); return ( ); })} ))}
Department {DAY_LABELS[i]} {date.getDate()} {(date.getMonth() + 1).toString().padStart(2, "0")}/ {date.getFullYear()}
{dept.name}
{shifts.map((shift) => ( ))} {onCreateShift && ( )}
); }