import { useContext } from "react"; import { useStore } from "zustand"; import type { Dayjs } from "@calcom/dayjs"; import dayjs from "@calcom/dayjs"; import { classNames } from "@calcom/lib"; import type { DateRange } from "@calcom/lib/date-ranges"; import { DAY_CELL_WIDTH } from "../constants"; import { TBContext } from "../store"; interface TimeDialProps { timezone: string; dateRanges?: DateRange[]; } function isMidnight(h: number) { return h <= 5 || h >= 22; } function isCurrentHourInRange({ dateRanges, cellDate, offset, }: { dateRanges?: DateRange[]; cellDate: Dayjs; offset: number; }): { rangeOverlap?: number; isInRange: boolean; } { if (!dateRanges) return { isInRange: false, }; const currentHour = cellDate.hour(); let rangeOverlap = 0; // yes or no answer whether it's in range. const isFullyInRange = dateRanges.some((time) => { if (!time) null; const startHour = dayjs(time.start).subtract(offset, "hour"); const endHour = dayjs(time.end).subtract(offset, "hour"); // If not same day number then we don't care if (startHour.day() !== cellDate.day()) { return false; } // this is a weird way of doing this const newDate = dayjs(time.start).set("hour", currentHour); const diffStart = newDate.diff(startHour, "minutes"); if (Math.abs(diffStart) < 60 && diffStart != 0) { rangeOverlap = diffStart < 0 ? -(Math.floor(startHour.minute() / 15) * 25) : Math.floor(startHour.minute() / 15) * 25; } const diffEnd = newDate.diff(endHour, "minutes"); if (Math.abs(diffEnd) < 60 && diffEnd != 0) { rangeOverlap = diffEnd < 0 ? -(Math.floor(endHour.minute() / 15) * 25) : Math.floor(endHour.minute() / 15) * 25; } return newDate.isBetween(startHour, endHour, undefined, "[)"); // smiley faces or something }); // most common situation, bail early. if (isFullyInRange) { return { isInRange: isFullyInRange, }; } return { isInRange: !!rangeOverlap, rangeOverlap, // value from -75 to 75 to indicate range overlap }; } export function TimeDial({ timezone, dateRanges }: TimeDialProps) { const store = useContext(TBContext); if (!store) throw new Error("Missing TBContext.Provider in the tree"); const browsingDate = useStore(store, (s) => s.browsingDate); const usersTimezoneDate = dayjs(browsingDate).tz(timezone); const nowDate = dayjs(browsingDate); const offset = (nowDate.utcOffset() - usersTimezoneDate.utcOffset()) / 60; const hours = Array.from({ length: 24 }, (_, i) => i - offset + 1); const days = [ hours.filter((i) => i < 0).map((i) => (i + 24) % 24), hours.filter((i) => i >= 0 && i < 24), hours.filter((i) => i >= 24).map((i) => i % 24), ]; let minuteOffsetApplied = false; return ( <>