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-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-06-15 09:59:58 +00:00
|
|
|
const { timeFormat } = useTimePreferences();
|
2022-12-14 13:36:10 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Set the container scroll position based on the current time.
|
2023-06-15 09:59:58 +00:00
|
|
|
const currentHour = new Date().getHours();
|
2022-12-14 13:36:10 +00:00
|
|
|
let currentMinute = new Date().getHours() * 60;
|
|
|
|
currentMinute = currentMinute + new Date().getMinutes();
|
|
|
|
|
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
|
|
|
|
|
|
|
const minutesFromStart = currentMinute - startHour * 60;
|
|
|
|
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-06-15 09:59:58 +00:00
|
|
|
}, [startHour, endHour, scrolledIntoView]);
|
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,
|
|
|
|
}}>
|
|
|
|
<div className="w-14 pr-2 text-right">{dayjs().format(timeFormat)}</div>
|
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|