"use client"; import { useState, useEffect } from "react"; import type { ReactElement } from "react"; import { Calendar } from "@/components/calendar/Calendar"; import { fetchEvents } from "@/lib/api/events"; import { MosaicSpinner } from "@/components/ui/MosaicSpinner"; import { useWorkspaceId } from "@/lib/hooks"; import type { Event } from "@mosaic/shared"; export default function CalendarPage(): ReactElement { const workspaceId = useWorkspaceId(); const [events, setEvents] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!workspaceId) { setIsLoading(false); return; } const wsId = workspaceId; let cancelled = false; setError(null); setIsLoading(true); async function loadEvents(): Promise { try { const data = await fetchEvents(wsId); if (!cancelled) { setEvents(data); } } catch (err: unknown) { console.error("[Calendar] Failed to fetch events:", err); if (!cancelled) { setError( err instanceof Error ? err.message : "We had trouble loading your calendar. Please try again when you're ready." ); } } finally { if (!cancelled) { setIsLoading(false); } } } void loadEvents(); return (): void => { cancelled = true; }; }, [workspaceId]); function handleRetry(): void { if (!workspaceId) return; const wsId = workspaceId; setError(null); setIsLoading(true); fetchEvents(wsId) .then((data) => { setEvents(data); }) .catch((err: unknown) => { console.error("[Calendar] Retry failed:", err); setError( err instanceof Error ? err.message : "We had trouble loading your calendar. Please try again when you're ready." ); }) .finally(() => { setIsLoading(false); }); } if (isLoading) { return (

Calendar

View your schedule at a glance

); } if (error !== null) { return (

Calendar

View your schedule at a glance

{error}

); } return (

Calendar

View your schedule at a glance

{events.length === 0 ? (

No events scheduled

Your calendar is clear

) : ( )}
); }