"use client"; import { useState, useRef, useEffect } from "react"; import { useTranslations } from "next-intl"; import { Button } from "@/components/ui/button"; import { ChevronLeft, ChevronRight, Plus, Upload, CalendarDays, Globe, ChevronDown, ArrowLeft, Menu } from "lucide-react"; import { startOfWeek } from "date-fns"; import { cn } from "@/lib/utils"; import type { CalendarViewMode } from "@/stores/calendar-store"; import type { Calendar } from "@/lib/jmap/types"; import { useCalendarLocale } from "@/hooks/use-calendar-locale"; interface CalendarToolbarProps { selectedDate: Date; viewMode: CalendarViewMode; onPrev: () => void; onNext: () => void; onToday: () => void; onViewModeChange: (mode: CalendarViewMode) => void; onCreateEvent: () => void; onImport?: () => void; onSubscribe?: () => void; isMobile?: boolean; firstDayOfWeek?: number; onNavigateBack?: () => void; calendars?: Calendar[]; selectedCalendarIds?: string[]; onToggleVisibility?: (id: string) => void; enableCalendarTasks?: boolean; /** Show a burger button at the start that opens the (overlay) sidebar. */ onMenuClick?: () => void; } export function CalendarToolbar({ selectedDate, viewMode, onPrev, onNext, onToday, onViewModeChange, onCreateEvent, onImport, onSubscribe, isMobile, firstDayOfWeek = 1, onNavigateBack, calendars, selectedCalendarIds, onToggleVisibility, enableCalendarTasks, onMenuClick, }: CalendarToolbarProps) { const t = useTranslations("calendar"); const { weekStartsOn, formatMonthYear, formatMonthYearShort, formatWeekRange, formatWeekRangeShort, formatFullDate, } = useCalendarLocale(); const views: CalendarViewMode[] = enableCalendarTasks ? ["month", "week", "day", "agenda", "tasks"] : ["month", "week", "day", "agenda"]; const [showCalendarDropdown, setShowCalendarDropdown] = useState(false); const dropdownRef = useRef(null); useEffect(() => { if (!showCalendarDropdown) return; function handleClickOutside(e: MouseEvent) { if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) { setShowCalendarDropdown(false); } } document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, [showCalendarDropdown]); const getDateLabel = (): string => { switch (viewMode) { case "month": return isMobile ? formatMonthYearShort(selectedDate) : formatMonthYear(selectedDate); case "week": { const ws = startOfWeek(selectedDate, { weekStartsOn }); return isMobile ? formatWeekRangeShort(ws) : formatWeekRange(ws); } case "day": return isMobile ? formatFullDate(selectedDate) : formatFullDate(selectedDate); case "agenda": return isMobile ? formatMonthYearShort(selectedDate) : formatMonthYear(selectedDate); case "tasks": return t("views.tasks"); } }; const [showImportDropdown, setShowImportDropdown] = useState(false); const importDropdownRef = useRef(null); useEffect(() => { if (!showImportDropdown) return; function handleClickOutside(e: MouseEvent) { if (importDropdownRef.current && !importDropdownRef.current.contains(e.target as Node)) { setShowImportDropdown(false); } } document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, [showImportDropdown]); return (
{/* Burger menu (rendered in pages that use a narrow overlay sidebar) */} {onMenuClick && !isMobile && ( )} {/* ── MOBILE TOOLBAR ── */} {isMobile && (
{/* Row 1: Back / Date nav / Today */}
{onMenuClick && ( )} {onNavigateBack && ( )} {getDateLabel()}
{/* Row 2: View switcher pills + calendar toggle */}
{views.map((v) => ( ))}
{calendars && selectedCalendarIds && onToggleVisibility && (
{showCalendarDropdown && (

{t("my_calendars")}

{calendars.filter(c => !c.isShared).map((cal) => { const isVisible = selectedCalendarIds.includes(cal.id); const color = cal.color || "#3b82f6"; return ( ); })}
{(() => { const shared = calendars.filter(c => c.isShared); const groups = new Map(); for (const c of shared) { const key = c.accountId || c.accountName || c.id; if (!groups.has(key)) groups.set(key, { accountName: c.accountName || key, cals: [] }); groups.get(key)!.cals.push(c); } return Array.from(groups.values()).map((group) => (

{group.accountName}

{group.cals.map((cal) => { const isVisible = selectedCalendarIds.includes(cal.id); const color = cal.color || "#3b82f6"; return ( ); })}
)); })()}
)}
)}
)} {/* ── DESKTOP TOOLBAR ── */} {!isMobile && (
{getDateLabel()}
)}
{!isMobile && (
{views.map((v) => ( ))}
)} {(onImport || onSubscribe) && !isMobile && (
{showImportDropdown && (
{onImport && ( )} {onSubscribe && ( )}
)}
)} {!isMobile && ( )}
); }