| // src/components/genui/CalendarComponent.tsx | |
| import { useState, useMemo } from "react"; | |
| import { ChevronLeft, ChevronRight, Clock, MapPin } from "lucide-react"; | |
| interface CalendarEvent { | |
| id: string; | |
| title: string; | |
| start: string; | |
| end?: string; | |
| color?: string; | |
| description?: string; | |
| location?: string; | |
| } | |
| interface CalendarComponentProps { | |
| title: string; | |
| events: CalendarEvent[]; | |
| defaultView?: "month" | "week" | "day" | "agenda"; | |
| height?: number; | |
| isStreaming?: boolean; | |
| onAction?: (action: string, data: any) => void; | |
| } | |
| const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; | |
| const MONTHS = ["January","February","March","April","May","June","July","August","September","October","November","December"]; | |
| export function CalendarComponent({ title, events, defaultView = "month", height = 400, onAction }: CalendarComponentProps) { | |
| const [view, setView] = useState(defaultView); | |
| const [currentDate, setCurrentDate] = useState(new Date()); | |
| const year = currentDate.getFullYear(); | |
| const month = currentDate.getMonth(); | |
| const daysInMonth = new Date(year, month + 1, 0).getDate(); | |
| const firstDay = new Date(year, month, 0).getDay(); | |
| const days = useMemo(() => { | |
| const result: (number | null)[] = []; | |
| for (let i = 0; i < firstDay; i++) result.push(null); | |
| for (let i = 1; i <= daysInMonth; i++) result.push(i); | |
| return result; | |
| }, [firstDay, daysInMonth]); | |
| const getEventsForDay = (day: number) => | |
| events.filter(e => { | |
| const d = new Date(e.start); | |
| return d.getFullYear() === year && d.getMonth() === month && d.getDate() === day; | |
| }); | |
| const today = new Date(); | |
| const prevMonth = () => setCurrentDate(new Date(year, month - 1, 1)); | |
| const nextMonth = () => setCurrentDate(new Date(year, month + 1, 1)); | |
| const goToToday = () => setCurrentDate(new Date()); | |
| if (view === "agenda") { | |
| const upcoming = events | |
| .filter(e => new Date(e.start) >= new Date()) | |
| .sort((a, b) => new Date(a.start).getTime() - new Date(b.start).getTime()); | |
| return ( | |
| <div className="space-y-2" style={{ maxHeight: height, overflowY: "auto" }}> | |
| <div className="flex items-center justify-between"> | |
| <h3 className="font-semibold text-sm">{title}</h3> | |
| <button onClick={() => setView("month")} className="text-xs text-primary hover:underline">Month view</button> | |
| </div> | |
| {upcoming.length === 0 ? ( | |
| <p className="text-sm text-gray-400 py-8 text-center">No upcoming events</p> | |
| ) : ( | |
| <div className="space-y-2"> | |
| {upcoming.slice(0, 20).map(event => ( | |
| <div key={event.id} className="flex items-start gap-3 p-2.5 rounded-lg border border-gray-100 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700/50"> | |
| <div className="text-center shrink-0 w-12"> | |
| <p className="text-lg font-bold text-primary">{new Date(event.start).getDate()}</p> | |
| <p className="text-[10px] text-gray-400">{MONTHS[new Date(event.start).getMonth()].slice(0,3)}</p> | |
| </div> | |
| <div className="flex-1 min-w-0"> | |
| <p className="text-sm font-semibold">{event.title}</p> | |
| <div className="flex items-center gap-2 text-xs text-gray-400 mt-0.5"> | |
| <Clock className="w-3 h-3" /> | |
| {new Date(event.start).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })} | |
| {event.location && <><MapPin className="w-3 h-3"/>{event.location}</>} | |
| </div> | |
| </div> | |
| {event.color && <div className="w-1.5 h-full rounded-full shrink-0" style={{ backgroundColor: event.color }} />} | |
| </div> | |
| ))} | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |
| return ( | |
| <div className="space-y-2"> | |
| {/* Header */} | |
| <div className="flex items-center justify-between"> | |
| <h3 className="font-semibold text-sm">{title}</h3> | |
| <div className="flex items-center gap-1"> | |
| <button onClick={goToToday} className="text-[10px] px-2 py-0.5 bg-gray-100 dark:bg-gray-700 rounded-full hover:bg-gray-200">Today</button> | |
| <button onClick={prevMonth} className="p-0.5 hover:bg-gray-100 dark:hover:bg-gray-700 rounded"><ChevronLeft className="w-3.5 h-3.5" /></button> | |
| <span className="text-xs font-medium min-w-25 text-center">{MONTHS[month]} {year}</span> | |
| <button onClick={nextMonth} className="p-0.5 hover:bg-gray-100 dark:hover:bg-gray-700 rounded"><ChevronRight className="w-3.5 h-3.5" /></button> | |
| <button onClick={() => setView("agenda")} className="text-[10px] text-primary hover:underline ml-1">Agenda</button> | |
| </div> | |
| </div> | |
| {/* Grid */} | |
| <div className="grid grid-cols-7 gap-px bg-gray-200 dark:bg-gray-700 rounded-lg overflow-hidden text-center"> | |
| {WEEKDAYS.map(d => ( | |
| <div key={d} className="bg-gray-50 dark:bg-gray-800 py-1.5 text-[10px] font-semibold text-gray-500 uppercase">{d}</div> | |
| ))} | |
| {days.map((day, i) => ( | |
| <div | |
| key={i} | |
| className={`bg-white dark:bg-gray-900 min-h-[60px] p-1 text-left ${ | |
| !day ? "opacity-30" : "" | |
| } ${ | |
| day && today.getFullYear() === year && today.getMonth() === month && today.getDate() === day | |
| ? "bg-primary/5 ring-1 ring-primary" | |
| : "" | |
| }`} | |
| > | |
| {day && ( | |
| <> | |
| <span className="text-[10px] text-gray-500">{day}</span> | |
| <div className="space-y-0.5 mt-0.5"> | |
| {getEventsForDay(day).slice(0, 2).map(e => ( | |
| <div | |
| key={e.id} | |
| className="text-[8px] px-1 py-0.5 rounded truncate text-white" | |
| style={{ backgroundColor: e.color ?? "#0d9488" }} | |
| title={e.title} | |
| > | |
| {e.title} | |
| </div> | |
| ))} | |
| {getEventsForDay(day).length > 2 && ( | |
| <span className="text-[8px] text-gray-400">+{getEventsForDay(day).length - 2} more</span> | |
| )} | |
| </div> | |
| </> | |
| )} | |
| </div> | |
| ))} | |
| </div> | |
| </div> | |
| ); | |
| } |
Xet Storage Details
- Size:
- 6.37 kB
- Xet hash:
- 00cca1b89304a4d0ed88ca5a2b03875a80deedac27373fb124b69f474b8e9fbd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.