import { useEffect, useState } from "react"; import { deleteEventAsync, putEventAsync } from "../Calendars/CalendarSlice"; import { useAppDispatch, useAppSelector } from "../../app/hooks"; import { Popover, Button, Box, Typography, ButtonGroup, Card, CardContent, Divider, IconButton, Avatar, Badge, PopoverPosition, } from "@mui/material"; import EditIcon from "@mui/icons-material/Edit"; import DeleteIcon from "@mui/icons-material/Delete"; import VisibilityIcon from "@mui/icons-material/Visibility"; import CloseIcon from "@mui/icons-material/Close"; import CalendarTodayIcon from "@mui/icons-material/CalendarToday"; import LocationOnIcon from "@mui/icons-material/LocationOn"; import VideocamIcon from "@mui/icons-material/Videocam"; import ErrorOutlineIcon from "@mui/icons-material/ErrorOutline"; import CircleIcon from "@mui/icons-material/Circle"; import CancelIcon from "@mui/icons-material/Cancel"; import CheckCircleIcon from "@mui/icons-material/CheckCircle"; import { userAttendee } from "../User/userDataTypes"; import EventDisplayModal, { InfoRow, renderAttendeeBadge, stringAvatar, } from "./EventDisplay"; export default function EventPreviewModal({ eventId, calId, anchorPosition, open, onClose, }: { eventId: string; calId: string; anchorPosition: PopoverPosition | null; open: boolean; onClose: (event: {}, reason: "backdropClick" | "escapeKeyDown") => void; }) { const dispatch = useAppDispatch(); const calendar = useAppSelector((state) => state.calendars.list[calId]); const event = useAppSelector( (state) => state.calendars.list[calId]?.events[eventId] ); const user = useAppSelector((state) => state.user); const [showAllAttendees, setShowAllAttendees] = useState(false); const [openFullDisplay, setOpenFullDisplay] = useState(false); useEffect(() => { if (!event || !calendar) { onClose({}, "backdropClick"); } }, [event, calendar, onClose]); if (!event || !calendar) return null; const attendeeDisplayLimit = 3; const attendees = event.attendee?.filter( (a) => a.cal_address !== event.organizer?.cal_address ) || []; const visibleAttendees = showAllAttendees ? attendees : attendees.slice(0, attendeeDisplayLimit); const currentUserAttendee = event.attendee?.find( (person) => person.cal_address === user.userData.email ); const organizer = event.attendee?.find( (a) => a.cal_address === event.organizer?.cal_address ); function handleRSVP(rsvp: string) { const newEvent = { ...event, attendee: event.attendee?.map((a) => a.cal_address === user.userData.email ? { ...a, partstat: rsvp } : a ), }; dispatch(putEventAsync({ cal: calendar, newEvent })); onClose({}, "backdropClick"); } return ( <> {/* Top-right buttons */} {user.userData.email !== event.organizer?.cal_address && ( { setOpenFullDisplay(!openFullDisplay); }} > )} {user.userData.email === event.organizer?.cal_address && ( <> { setOpenFullDisplay(!openFullDisplay); }} > { onClose({}, "backdropClick"); dispatch( deleteEventAsync({ calId, eventId, eventURL: event.URL }) ); }} > )} onClose({}, "backdropClick")} > {event.title && ( {event.title} )} {/* Time info*/} {formatDate(event.start, event.allday)} {event.end && formatEnd(event.start, event.end, event.allday) && ` – ${formatEnd(event.start, event.end, event.allday)}`} {/* Location */} {event.location && ( } text={event.location} /> )} {/* Video */} {event.x_openpass_videoconference && ( } text="Video conference available" data={event.x_openpass_videoconference} /> )} {/* Attendees */} {event.attendee?.length > 0 && ( Attendees: {organizer && renderAttendeeBadge(organizer, "org", true)} {visibleAttendees.map((a, idx) => renderAttendeeBadge(a, idx.toString()) )} {attendees.length > attendeeDisplayLimit && ( setShowAllAttendees(!showAllAttendees)} > {showAllAttendees ? "Show less" : `Show more (${ attendees.length - attendeeDisplayLimit } more)`} )} )} {/* Error */} {event.error && ( } text={event.error} error /> )} {/* Calendar color dot */} {calendar.name} {/* RSVP */} {currentUserAttendee && ( Will you attend? )} {/* Description */} {event.description && ( {event.description} )} setOpenFullDisplay(false)} eventId={eventId} calId={calId} /> ); } function formatDate(date: Date, allday?: boolean) { if (allday) { return new Date(date).toLocaleDateString(undefined, { year: "numeric", month: "long", weekday: "long", day: "numeric", }); } else { return new Date(date).toLocaleString(undefined, { year: "numeric", month: "long", weekday: "long", day: "numeric", hour: "2-digit", minute: "2-digit", }); } } function formatEnd(start: Date, end: Date, allday?: boolean) { const startDate = new Date(start); const endDate = new Date(end); const sameDay = startDate.getFullYear() === endDate.getFullYear() && startDate.getMonth() === endDate.getMonth() && startDate.getDate() === endDate.getDate(); if (allday) { return sameDay ? null : endDate.toLocaleDateString(undefined, { year: "numeric", month: "short", day: "numeric", }); } else { if (sameDay) { return endDate.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit", }); } return endDate.toLocaleString(undefined, { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); } }