import { useState } from 'react' import { useQuery } from '@tanstack/react-query' import { sessionListOptions } from '@/api/lessons' import type { LessonSession } from '@/types/lesson' import { Button } from '@/components/ui/button' import { Dialog, DialogContent } from '@/components/ui/dialog' import { SessionDetailPanel } from './session-detail-panel' import { ChevronLeft, ChevronRight } from 'lucide-react' const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] const STATUS_COLORS: Record = { scheduled: 'bg-blue-500/15 border-blue-500/30 text-blue-700 dark:text-blue-300', attended: 'bg-green-500/15 border-green-500/30 text-green-700 dark:text-green-300', missed: 'bg-red-500/15 border-red-500/30 text-red-700 dark:text-red-300', makeup: 'bg-purple-500/15 border-purple-500/30 text-purple-700 dark:text-purple-300', cancelled: 'bg-muted border-border text-muted-foreground', } interface LessonsWeekViewProps { canEdit: boolean instructorId?: string } export function LessonsWeekView({ canEdit, instructorId }: LessonsWeekViewProps) { const [weekOffset, setWeekOffset] = useState(0) const [selectedSessionId, setSelectedSessionId] = useState(null) // Get week start (Monday) and end (Sunday) const now = new Date() const monday = new Date(now) monday.setDate(now.getDate() - ((now.getDay() + 6) % 7) + weekOffset * 7) monday.setHours(0, 0, 0, 0) const sunday = new Date(monday) sunday.setDate(monday.getDate() + 6) const weekStart = monday.toISOString().slice(0, 10) const weekEnd = sunday.toISOString().slice(0, 10) const { data } = useQuery({ ...sessionListOptions({ page: 1, limit: 200, sort: 'scheduled_time', order: 'asc', scheduledDateFrom: weekStart, scheduledDateTo: weekEnd, ...(instructorId ? { instructorId } : {}), }), staleTime: 30_000, }) const sessions = data?.data ?? [] // Group sessions by date const byDate = new Map() for (const s of sessions) { const existing = byDate.get(s.scheduledDate) ?? [] existing.push(s) byDate.set(s.scheduledDate, existing) } // Generate week dates const weekDates = Array.from({ length: 7 }, (_, i) => { const d = new Date(monday) d.setDate(monday.getDate() + i) return d.toISOString().slice(0, 10) }) const todayStr = new Date().toISOString().slice(0, 10) return (
{/* Week navigation */}
{monday.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })} — {sunday.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })}
{weekOffset !== 0 && ( )}
{/* Calendar grid */}
{weekDates.map((dateStr, i) => { const daySessions = byDate.get(dateStr) ?? [] const isToday = dateStr === todayStr const dayDate = new Date(dateStr + 'T00:00:00') return (
{/* Day header */}
{DAYS[dayDate.getDay()]}
{dayDate.getDate()}
{/* Sessions */}
{daySessions.map((session) => ( ))} {daySessions.length === 0 && (
)}
) })}
{/* Session detail dialog */} { if (!open) setSelectedSessionId(null) }}>
) }