|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useMemo, useEffect } from "react"; |
| 4 | + |
| 5 | +import dayjs from "@calcom/dayjs"; |
| 6 | +import { useTimePreferences } from "@calcom/features/bookings/lib"; |
| 7 | +import { Calendar } from "@calcom/features/calendars/weeklyview"; |
| 8 | +import type { CalendarEvent } from "@calcom/features/calendars/weeklyview/types/events"; |
| 9 | +import { useLocale } from "@calcom/lib/hooks/useLocale"; |
| 10 | +import { useGetTheme } from "@calcom/lib/hooks/useTheme"; |
| 11 | +import { Button } from "@calcom/ui/components/button"; |
| 12 | +import { ButtonGroup } from "@calcom/ui/components/buttonGroup"; |
| 13 | +import { Icon } from "@calcom/ui/components/icon"; |
| 14 | + |
| 15 | +import type { BookingOutput } from "../types"; |
| 16 | + |
| 17 | +type BookingsCalendarViewProps = { |
| 18 | + bookings: BookingOutput[]; |
| 19 | + currentWeekStart: dayjs.Dayjs; |
| 20 | + onWeekStartChange: (weekStart: dayjs.Dayjs) => void; |
| 21 | +}; |
| 22 | + |
| 23 | +export function BookingsCalendarView({ |
| 24 | + bookings, |
| 25 | + currentWeekStart, |
| 26 | + onWeekStartChange, |
| 27 | +}: BookingsCalendarViewProps) { |
| 28 | + const { t } = useLocale(); |
| 29 | + const { timezone } = useTimePreferences(); |
| 30 | + const { resolvedTheme, forcedTheme } = useGetTheme(); |
| 31 | + |
| 32 | + const goToPreviousWeek = () => { |
| 33 | + onWeekStartChange(currentWeekStart.subtract(1, "week")); |
| 34 | + }; |
| 35 | + |
| 36 | + const goToNextWeek = () => { |
| 37 | + onWeekStartChange(currentWeekStart.add(1, "week")); |
| 38 | + }; |
| 39 | + |
| 40 | + const goToToday = () => { |
| 41 | + onWeekStartChange(dayjs().startOf("week")); |
| 42 | + }; |
| 43 | + |
| 44 | + const startDate = useMemo(() => currentWeekStart.toDate(), [currentWeekStart]); |
| 45 | + const endDate = useMemo(() => currentWeekStart.add(6, "day").toDate(), [currentWeekStart]); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + onWeekStartChange(currentWeekStart); |
| 49 | + }, []); |
| 50 | + |
| 51 | + const events = useMemo<CalendarEvent[]>(() => { |
| 52 | + const hasDarkTheme = !forcedTheme && resolvedTheme === "dark"; |
| 53 | + |
| 54 | + return bookings |
| 55 | + .filter((booking) => { |
| 56 | + const bookingStart = dayjs(booking.startTime); |
| 57 | + return ( |
| 58 | + (bookingStart.isAfter(currentWeekStart) || bookingStart.isSame(currentWeekStart)) && |
| 59 | + bookingStart.isBefore(currentWeekStart.add(7, "day")) |
| 60 | + ); |
| 61 | + }) |
| 62 | + .sort((a, b) => { |
| 63 | + const startDiff = new Date(a.startTime).getTime() - new Date(b.startTime).getTime(); |
| 64 | + if (startDiff !== 0) return startDiff; |
| 65 | + return new Date(a.endTime).getTime() - new Date(b.endTime).getTime(); |
| 66 | + }) |
| 67 | + .map((booking, idx) => { |
| 68 | + // Parse eventTypeColor and extract the appropriate color based on theme |
| 69 | + const eventTypeColor = |
| 70 | + booking.eventType?.eventTypeColor && |
| 71 | + booking.eventType.eventTypeColor[hasDarkTheme ? "darkEventTypeColor" : "lightEventTypeColor"]; |
| 72 | + |
| 73 | + return { |
| 74 | + id: idx, |
| 75 | + title: booking.title, |
| 76 | + start: new Date(booking.startTime), |
| 77 | + end: new Date(booking.endTime), |
| 78 | + options: { |
| 79 | + status: booking.status, |
| 80 | + ...(eventTypeColor && { color: eventTypeColor }), |
| 81 | + }, |
| 82 | + }; |
| 83 | + }); |
| 84 | + }, [bookings, currentWeekStart, resolvedTheme, forcedTheme]); |
| 85 | + |
| 86 | + const weekStart = currentWeekStart; |
| 87 | + const weekEnd = currentWeekStart.add(6, "day"); |
| 88 | + const startMonth = weekStart.format("MMM"); |
| 89 | + const endMonth = weekEnd.format("MMM"); |
| 90 | + const year = weekEnd.format("YYYY"); |
| 91 | + |
| 92 | + const weekRange = |
| 93 | + startMonth === endMonth ? ( |
| 94 | + <> |
| 95 | + <span className="text-emphasis">{`${startMonth} ${weekStart.format("D")} - ${weekEnd.format( |
| 96 | + "D" |
| 97 | + )}`}</span> |
| 98 | + <span className="text-muted">, {year}</span> |
| 99 | + </> |
| 100 | + ) : ( |
| 101 | + <> |
| 102 | + <span className="text-emphasis">{`${weekStart.format("MMM D")} - ${weekEnd.format("MMM D")}`}</span> |
| 103 | + <span className="text-muted">, {year}</span> |
| 104 | + </> |
| 105 | + ); |
| 106 | + |
| 107 | + return ( |
| 108 | + <div className="border-subtle flex h-[calc(100vh-260px)] min-h-[600px] flex-col rounded-2xl border"> |
| 109 | + <div className="mx-4 mt-4 flex items-center justify-between py-1.5"> |
| 110 | + <div className="flex items-center gap-2"> |
| 111 | + <h2 className="text-xl font-semibold">{weekRange}</h2> |
| 112 | + </div> |
| 113 | + <div className="flex items-center gap-2"> |
| 114 | + <Button color="secondary" onClick={goToToday} className="capitalize leading-4"> |
| 115 | + {t("today")} |
| 116 | + </Button> |
| 117 | + <ButtonGroup combined> |
| 118 | + <Button color="secondary" onClick={goToPreviousWeek}> |
| 119 | + <span className="sr-only">{t("view_previous_week")}</span> |
| 120 | + <Icon name="chevron-left" className="h-4 w-4" /> |
| 121 | + </Button> |
| 122 | + <Button color="secondary" onClick={goToNextWeek}> |
| 123 | + <span className="sr-only">{t("view_next_week")}</span> |
| 124 | + <Icon name="chevron-right" className="h-4 w-4" /> |
| 125 | + </Button> |
| 126 | + </ButtonGroup> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + |
| 130 | + <div className="flex-1 overflow-y-auto overflow-x-hidden rounded-2xl"> |
| 131 | + <Calendar |
| 132 | + timezone={timezone} |
| 133 | + sortEvents |
| 134 | + startHour={0} |
| 135 | + endHour={23} |
| 136 | + events={events} |
| 137 | + startDate={startDate} |
| 138 | + endDate={endDate} |
| 139 | + gridCellsPerHour={4} |
| 140 | + hoverEventDuration={0} |
| 141 | + showBackgroundPattern={false} |
| 142 | + showBorder={false} |
| 143 | + borderColor="subtle" |
| 144 | + onEventClick={(_event) => {}} |
| 145 | + hideHeader |
| 146 | + /> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + ); |
| 150 | +} |
0 commit comments