import { useEffect, useState, useRef } from "react"; import dayjs from "@calcom/dayjs"; import { useTimePreferences } from "@calcom/features/bookings/lib"; import { useCalendarStore } from "../../state/store"; function calculateMinutesFromStart(startHour: number, currentHour: number, currentMinute: number) { const startMinute = startHour * 60; const currentMinuteOfDay = currentHour * 60 + currentMinute; return currentMinuteOfDay - startMinute; } export function CurrentTime() { const currentTimeRef = useRef(null); const [scrolledIntoView, setScrolledIntoView] = useState(false); const [currentTimePos, setCurrentTimePos] = useState(null); const { startHour, endHour } = useCalendarStore((state) => ({ startHour: state.startHour || 0, endHour: state.endHour || 23, })); const { timeFormat, timezone } = useTimePreferences(); useEffect(() => { // Set the container scroll position based on the current time. const currentDateTime = dayjs().tz(timezone); // Get current date and time in the specified timezone const currentHour = currentDateTime.hour(); const currentMinute = currentDateTime.minute(); if (currentHour > endHour || currentHour < startHour) { setCurrentTimePos(null); } const minutesFromStart = calculateMinutesFromStart(startHour, currentHour, currentMinute); setCurrentTimePos(minutesFromStart); if (!currentTimeRef.current || scrolledIntoView) return; // Within a small timeout so element has time to render. setTimeout(() => { currentTimeRef?.current?.scrollIntoView({ block: "center" }); setScrolledIntoView(true); }, 100); // eslint-disable-next-line react-hooks/exhaustive-deps }, [startHour, endHour, scrolledIntoView, timezone]); return (