import { TimeSlot, Participant } from '@/types/calendar'; import { cn } from '@/lib/utils'; import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover'; import { Button } from '@/components/ui/button'; import { useState, useMemo, useRef, useEffect } from 'react'; import { Check, X, Loader2, ChevronLeft, ChevronRight, ChevronsRight, Clock, Calendar as CalendarIcon, Sun, Moon, Users } from 'lucide-react'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; const DEFAULT_TIMEZONE = 'America/Toronto'; const dayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']; const SLOT_INTERVAL_MINUTES = 15; const SLOTS_PER_HOUR = 60 / SLOT_INTERVAL_MINUTES; const WORKING_HOUR_START = 8; // 8 AM const WORKING_HOUR_END = 18; // 6 PM // Generate time slots with hour and minute const ALL_TIME_SLOTS = Array.from({ length: 24 * SLOTS_PER_HOUR }, (_, i) => ({ hour: Math.floor(i / SLOTS_PER_HOUR), minute: (i % SLOTS_PER_HOUR) * SLOT_INTERVAL_MINUTES, })); const WORKING_TIME_SLOTS = ALL_TIME_SLOTS.filter( slot => slot.hour >= WORKING_HOUR_START && slot.hour < WORKING_HOUR_END ); // Helper to check if a slot is in the past or too close (2h buffer) const isSlotTooSoon = (slotDate: number) => { const now = Date.now(); const twoHoursFromNow = now + 2 * 60 * 60 * 1000; return slotDate < twoHoursFromNow; }; // Reuse previous timezone helpers or simplify const getWeekDates = (timezone: string, weekOffset: number = 0): Date[] => { const now = new Date(); const formatter = new Intl.DateTimeFormat('en-CA', { timeZone: timezone, year: 'numeric', month: '2-digit', day: '2-digit', }); const todayStr = formatter.format(now); const [year, month, day] = todayStr.split('-').map(Number); const todayDate = new Date(year, month - 1, day); const dayOfWeek = todayDate.getDay(); // If Sunday (0), go back 6 days to Monday. If Mon (1), go back 0. If Sat (6), go back 5. // Actually standard logic: Mon=1...Sun=7. // Let's assume standard ISO week start (Mon) const daysToMonday = dayOfWeek === 0 ? -6 : 1 - dayOfWeek; const mondayDate = new Date(year, month - 1, day + daysToMonday + weekOffset * 7); return Array.from({ length: 5 }, (_, i) => { const d = new Date(mondayDate); d.setDate(mondayDate.getDate() + i); return d; }); }; const formatTimezoneDisplay = (timezone: string): string => { try { const parts = timezone.split('/'); const city = parts[parts.length - 1].replace(/_/g, ' '); return city; } catch { return timezone; } }; interface AvailabilityHeatmapV2Props { slots: TimeSlot[]; selectedParticipants: Participant[]; onSlotSelect: (slot: TimeSlot) => void; showPartialAvailability?: boolean; isLoading?: boolean; weekOffset?: number; onWeekOffsetChange?: (offset: number) => void; displayTimezone?: string; showSecondaryTimezone?: boolean; secondaryTimezone?: string; } export const AvailabilityHeatmapV2 = ({ slots, selectedParticipants, onSlotSelect, showPartialAvailability = false, isLoading = false, weekOffset = 0, onWeekOffsetChange, displayTimezone = DEFAULT_TIMEZONE, showSecondaryTimezone = false, secondaryTimezone = DEFAULT_TIMEZONE, }: AvailabilityHeatmapV2Props) => { const [showFullDay, setShowFullDay] = useState(false); const activeSlots = showFullDay ? ALL_TIME_SLOTS : WORKING_TIME_SLOTS; const weekDates = useMemo(() => getWeekDates(displayTimezone, weekOffset), [displayTimezone, weekOffset]); // Pre-compute slots lookup map for O(1) access // Key: YYYY-MM-DD:Hour:Minute const slotsMap = useMemo(() => { const map = new Map(); slots.forEach(slot => { const d = new Date(slot.start_time); // Format to DisplayTZ to find coordinate const formatter = new Intl.DateTimeFormat('en-CA', { timeZone: displayTimezone, year: 'numeric', month: '2-digit', day: '2-digit', hour: 'numeric', minute: 'numeric', hour12: false }); const parts = formatter.formatToParts(d); const year = parts.find(p => p.type === 'year')?.value; const month = parts.find(p => p.type === 'month')?.value; const day = parts.find(p => p.type === 'day')?.value; const hour = parts.find(p => p.type === 'hour')?.value; const minute = parts.find(p => p.type === 'minute')?.value; if (year && month && day && hour && minute) { let h = parseInt(hour, 10); let m = parseInt(minute, 10); if (h === 24) h = 0; // Just in case const key = `${year}-${month}-${day}:${h}:${m}`; map.set(key, slot); } }); return map; }, [slots, displayTimezone]); const formatDateKey = (date: Date) => { const y = date.getFullYear(); const m = String(date.getMonth() + 1).padStart(2, '0'); const d = String(date.getDate()).padStart(2, '0'); return `${y}-${m}-${d}`; }; const formatDisplayDate = (date: Date) => { return date.toLocaleDateString('en-US', { weekday: 'short', month: 'short', day: 'numeric' }); }; const getSlotForCell = (date: Date, hour: number, minute: number) => { const key = `${formatDateKey(date)}:${hour}:${minute}`; return slotsMap.get(key); }; const formatTime = (hour: number, minute: number) => { return new Date(0, 0, 0, hour, minute).toLocaleTimeString('en-US', { hour: 'numeric', minute: '2-digit', hour12: true, }); }; const formatHourOnly = (hour: number) => { return new Date(0, 0, 0, hour).toLocaleTimeString('en-US', { hour: 'numeric', hour12: true, }); }; // Move hooks to top level to avoid conditional hook execution error const tzOffsetDiff = useMemo(() => { try { const now = new Date(); const p = parseInt(new Intl.DateTimeFormat('en-US', { timeZone: displayTimezone, hour: 'numeric', hour12: false }).format(now)); const s = parseInt(new Intl.DateTimeFormat('en-US', { timeZone: secondaryTimezone, hour: 'numeric', hour12: false }).format(now)); let diff = s - p; if (diff > 12) diff -= 24; if (diff < -12) diff += 24; return diff; } catch { return 0; } }, [displayTimezone, secondaryTimezone]); const timeColWidth = showSecondaryTimezone ? "120px" : "80px"; if (selectedParticipants.length === 0) { return (

No participants selected

Select team members from the list above to compare calendars and find the perfect meeting time.

); } return (
{/* Controls Bar */}
{weekDates[0]?.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })} {' - '} {weekDates[4]?.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })} {weekOffset === 0 ? "This Week" : weekOffset === 1 ? "Next Week" : `${weekOffset} weeks out`}
{formatTimezoneDisplay(displayTimezone)}
{/* Main Grid Card */}
{isLoading && (
)}
{/* Grid Header */}
{formatTimezoneDisplay(displayTimezone)} {showSecondaryTimezone && ( {formatTimezoneDisplay(secondaryTimezone)} )}
{weekDates.map(date => { const isToday = new Date().toDateString() === date.toDateString(); return (
{date.toLocaleDateString('en-US', { weekday: 'short' })}
{date.getDate()}
); })}
{/* Grid Body */}
{activeSlots.map(({ hour, minute }) => { const isNight = hour < 8 || hour >= 18; // Calculate secondary time let secondaryHour = hour + tzOffsetDiff; if (secondaryHour >= 24) secondaryHour -= 24; if (secondaryHour < 0) secondaryHour += 24; // Calculate end time for display const endMinute = minute + SLOT_INTERVAL_MINUTES; const endHour = hour + Math.floor(endMinute / 60); const endMinuteNormalized = endMinute % 60; return (
{/* Time Label - Sticky Left */}
{minute === 0 ? (
{isNight ? ( ) : ( )} {formatTime(hour, minute)}
) : ( :{minute.toString().padStart(2, '0')} )}
{showSecondaryTimezone && ( {formatTime(secondaryHour, minute)} )}
{/* Days */} {weekDates.map(date => { const slot = getSlotForCell(date, hour, minute); const tooSoon = slot ? isSlotTooSoon(new Date(slot.start_time).getTime()) : true; // Availability logic const availability = slot?.availability || 'none'; const isNone = availability === 'none'; const isPartial = availability === 'partial' && showPartialAvailability; const isFull = availability === 'full'; const isPartialHidden = availability === 'partial' && !showPartialAvailability; // Styling let bgClass = ""; // Default transparent if (!slot) { bgClass = "bg-muted/10 pattern-diagonal-lines opacity-50"; } else if (tooSoon) { bgClass = "bg-muted/20 pattern-diagonal-lines cursor-not-allowed border border-border/10"; } else if (isFull) { bgClass = "bg-emerald-500/90 hover:bg-emerald-600 shadow-sm"; } else if (isPartial) { bgClass = "bg-amber-400/80 hover:bg-amber-500 shadow-sm"; } else if (isNone || isPartialHidden) { bgClass = "bg-muted/50 hover:bg-muted/70 border border-border/20"; } return (
{slot ? (
{/* Mini Indicators for color-blind accessibility or density */} {isFull && }
= 12 ? "end" : "start"}>

{formatDisplayDate(date)}

{formatTime(hour, minute)} - {formatTime(endHour, endMinuteNormalized)}
{selectedParticipants.map(participant => { const isAvailable = slot.availableParticipants.includes(participant.name); return (
{participant.name}
{isAvailable ? ( ) : ( Busy )}
); })}
{(!isNone && !tooSoon) && ( )}
) : ( // Placeholder for missing slots
)}
); })}
); })}
{/* Legend */}
All Available
{showPartialAvailability && (
Partial Match
)}
Busy / No Match
Past / Too Soon
); }; function UsersPlaceholder() { return (
); } // Importing Users icon since it was missing in imports