|
| 1 | +import { useMemo, useEffect } from "react"; |
| 2 | + |
| 3 | +import dayjs from "@calcom/dayjs"; |
| 4 | +import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider"; |
| 5 | +import { useAvailableTimeSlots } from "@calcom/features/bookings/Booker/components/hooks/useAvailableTimeSlots"; |
| 6 | +import type { BookerEvent } from "@calcom/features/bookings/types"; |
| 7 | +import { Calendar } from "@calcom/features/calendars/weeklyview"; |
| 8 | +import type { CalendarEvent } from "@calcom/features/calendars/weeklyview/types/events"; |
| 9 | +import { localStorage } from "@calcom/lib/webstorage"; |
| 10 | + |
| 11 | +import { useOverlayCalendarStore } from "../bookings/Booker/components/OverlayCalendar/store"; |
| 12 | +import type { useScheduleForEventReturnType } from "../bookings/Booker/utils/event"; |
| 13 | +import { getQueryParam } from "../bookings/Booker/utils/query-param"; |
| 14 | + |
| 15 | +export const LargeCalendar = ({ |
| 16 | + extraDays, |
| 17 | + schedule, |
| 18 | + isLoading, |
| 19 | + event, |
| 20 | +}: { |
| 21 | + extraDays: number; |
| 22 | + schedule?: useScheduleForEventReturnType["data"]; |
| 23 | + isLoading: boolean; |
| 24 | + event: { |
| 25 | + data?: Pick<BookerEvent, "length"> | null; |
| 26 | + }; |
| 27 | +}) => { |
| 28 | + const selectedDate = useBookerStoreContext((state) => state.selectedDate); |
| 29 | + const selectedEventDuration = useBookerStoreContext((state) => state.selectedDuration); |
| 30 | + const overlayEvents = useOverlayCalendarStore((state) => state.overlayBusyDates); |
| 31 | + const displayOverlay = |
| 32 | + getQueryParam("overlayCalendar") === "true" || localStorage?.getItem("overlayCalendarSwitchDefault"); |
| 33 | + |
| 34 | + const eventDuration = selectedEventDuration || event?.data?.length || 30; |
| 35 | + |
| 36 | + const availableSlots = useAvailableTimeSlots({ schedule, eventDuration }); |
| 37 | + |
| 38 | + const startDate = selectedDate ? dayjs(selectedDate).toDate() : dayjs().toDate(); |
| 39 | + const endDate = dayjs(startDate) |
| 40 | + .add(extraDays - 1, "day") |
| 41 | + .toDate(); |
| 42 | + |
| 43 | + // HACK: force rerender when overlay events change |
| 44 | + // Sine we dont use react router here we need to force rerender (ATOM SUPPORT) |
| 45 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 46 | + useEffect(() => {}, [displayOverlay]); |
| 47 | + |
| 48 | + const overlayEventsForDate = useMemo(() => { |
| 49 | + if (!overlayEvents || !displayOverlay) return []; |
| 50 | + return overlayEvents.map((event, id) => { |
| 51 | + return { |
| 52 | + id, |
| 53 | + start: dayjs(event.start).toDate(), |
| 54 | + end: dayjs(event.end).toDate(), |
| 55 | + title: "Busy", |
| 56 | + options: { |
| 57 | + status: "ACCEPTED", |
| 58 | + }, |
| 59 | + } as CalendarEvent; |
| 60 | + }); |
| 61 | + }, [overlayEvents, displayOverlay]); |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="h-full [--calendar-dates-sticky-offset:66px]"> |
| 65 | + <Calendar |
| 66 | + isPending={isLoading} |
| 67 | + availableTimeslots={availableSlots} |
| 68 | + startHour={0} |
| 69 | + endHour={23} |
| 70 | + events={overlayEventsForDate} |
| 71 | + startDate={startDate} |
| 72 | + endDate={endDate} |
| 73 | + gridCellsPerHour={60 / eventDuration} |
| 74 | + hoverEventDuration={eventDuration} |
| 75 | + hideHeader |
| 76 | + /> |
| 77 | + </div> |
| 78 | + ); |
| 79 | +}; |
0 commit comments