2023-06-15 09:59:58 +00:00
|
|
|
import { useEffect, useState, useRef } from "react";
|
2022-12-14 13:36:10 +00:00
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-06-15 09:59:58 +00:00
|
|
|
import { useTimePreferences } from "@calcom/features/bookings/lib";
|
2022-12-14 13:36:10 +00:00
|
|
|
|
|
|
|
import { useCalendarStore } from "../../state/store";
|
|
|
|
|
2023-08-09 10:19:01 +00:00
|
|
|
function calculateMinutesFromStart(startHour: number, currentHour: number, currentMinute: number) {
|
|
|
|
const startMinute = startHour * 60;
|
|
|
|
const currentMinuteOfDay = currentHour * 60 + currentMinute;
|
|
|
|
return currentMinuteOfDay - startMinute;
|
|
|
|
}
|
|
|
|
|
2023-06-15 09:59:58 +00:00
|
|
|
export function CurrentTime() {
|
|
|
|
const currentTimeRef = useRef<HTMLDivElement>(null);
|
|
|
|
const [scrolledIntoView, setScrolledIntoView] = useState(false);
|
|
|
|
const [currentTimePos, setCurrentTimePos] = useState<number | null>(null);
|
2022-12-14 13:36:10 +00:00
|
|
|
const { startHour, endHour } = useCalendarStore((state) => ({
|
|
|
|
startHour: state.startHour || 0,
|
|
|
|
endHour: state.endHour || 23,
|
|
|
|
}));
|
2023-08-09 10:19:01 +00:00
|
|
|
const { timeFormat, timezone } = useTimePreferences();
|
2022-12-14 13:36:10 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Set the container scroll position based on the current time.
|
2023-08-09 10:19:01 +00:00
|
|
|
|
|
|
|
const currentDateTime = dayjs().tz(timezone); // Get current date and time in the specified timezone
|
|
|
|
|
|
|
|
const currentHour = currentDateTime.hour();
|
|
|
|
const currentMinute = currentDateTime.minute();
|
2022-12-14 13:36:10 +00:00
|
|
|
|
2023-06-15 09:59:58 +00:00
|
|
|
if (currentHour > endHour || currentHour < startHour) {
|
|
|
|
setCurrentTimePos(null);
|
2022-12-14 13:36:10 +00:00
|
|
|
}
|
2023-06-15 09:59:58 +00:00
|
|
|
|
2023-08-09 10:19:01 +00:00
|
|
|
const minutesFromStart = calculateMinutesFromStart(startHour, currentHour, currentMinute);
|
2023-06-15 09:59:58 +00:00
|
|
|
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);
|
2022-12-14 13:36:10 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-08-09 10:19:01 +00:00
|
|
|
}, [startHour, endHour, scrolledIntoView, timezone]);
|
2022-12-14 13:36:10 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2023-06-15 09:59:58 +00:00
|
|
|
ref={currentTimeRef}
|
|
|
|
className="absolute top-0 z-40 flex h-px items-center justify-center text-xs"
|
2022-12-14 13:36:10 +00:00
|
|
|
aria-hidden="true"
|
2023-06-15 09:59:58 +00:00
|
|
|
style={{
|
|
|
|
top: `calc(${currentTimePos}*var(--one-minute-height) + var(--calendar-offset-top))`,
|
|
|
|
zIndex: 70,
|
|
|
|
}}>
|
2023-08-09 10:19:01 +00:00
|
|
|
<div className="w-14 pr-2 text-right">{dayjs().tz(timezone).format(timeFormat)}</div>
|
2023-06-15 09:59:58 +00:00
|
|
|
<div className="bg-inverted h-3 w-px" />
|
|
|
|
<div className="bg-inverted h-px w-screen" />
|
2022-12-14 13:36:10 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|