2023-06-15 09:59:58 +00:00
|
|
|
import { useMemo } from "react";
|
2023-06-09 09:38:18 +00:00
|
|
|
import { shallow } from "zustand/shallow";
|
|
|
|
|
2023-06-15 09:59:58 +00:00
|
|
|
import type { Dayjs } from "@calcom/dayjs";
|
2023-06-09 09:38:18 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-06-13 11:08:00 +00:00
|
|
|
import { useTimePreferences } from "@calcom/features/bookings/lib";
|
2023-06-09 09:38:18 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
2022-12-14 13:36:10 +00:00
|
|
|
|
|
|
|
import { useCalendarStore } from "../../state/store";
|
2023-06-09 09:38:18 +00:00
|
|
|
import type { CalendarAvailableTimeslots } from "../../types/state";
|
2023-04-05 18:14:46 +00:00
|
|
|
import type { GridCellToDateProps } from "../../utils";
|
|
|
|
import { gridCellToDateTime } from "../../utils";
|
2022-12-14 13:36:10 +00:00
|
|
|
|
2023-06-09 09:38:18 +00:00
|
|
|
type EmptyCellProps = GridCellToDateProps & {
|
2023-06-15 09:59:58 +00:00
|
|
|
isDisabled?: boolean;
|
|
|
|
topOffsetMinutes?: number;
|
2023-06-09 09:38:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function EmptyCell(props: EmptyCellProps) {
|
2022-12-14 13:36:10 +00:00
|
|
|
const cellToDate = gridCellToDateTime({
|
|
|
|
day: props.day,
|
|
|
|
gridCellIdx: props.gridCellIdx,
|
|
|
|
totalGridCells: props.totalGridCells,
|
|
|
|
selectionLength: props.selectionLength,
|
|
|
|
startHour: props.startHour,
|
|
|
|
});
|
|
|
|
|
2023-06-15 09:59:58 +00:00
|
|
|
const minuesFromStart =
|
|
|
|
(cellToDate.toDate().getHours() - props.startHour) * 60 + cellToDate.toDate().getMinutes();
|
|
|
|
|
|
|
|
return <Cell topOffsetMinutes={minuesFromStart} timeSlot={cellToDate} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
type AvailableCellProps = {
|
|
|
|
availableSlots: CalendarAvailableTimeslots;
|
|
|
|
day: GridCellToDateProps["day"];
|
|
|
|
startHour: GridCellToDateProps["startHour"];
|
|
|
|
};
|
|
|
|
|
|
|
|
export function AvailableCellsForDay({ availableSlots, day, startHour }: AvailableCellProps) {
|
|
|
|
const slotsForToday = availableSlots && availableSlots[dayjs(day).format("YYYY-MM-DD")];
|
|
|
|
|
|
|
|
const slots = useMemo(
|
|
|
|
() =>
|
|
|
|
slotsForToday?.map((slot) => ({
|
|
|
|
slot,
|
|
|
|
topOffsetMinutes: (slot.start.getHours() - startHour) * 60 + slot.start.getMinutes(),
|
|
|
|
})),
|
|
|
|
[slotsForToday, startHour]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!availableSlots) return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{slots?.map((slot) => {
|
|
|
|
return (
|
|
|
|
<Cell
|
|
|
|
key={slot.slot.start.toISOString()}
|
|
|
|
timeSlot={dayjs(slot.slot.start)}
|
|
|
|
topOffsetMinutes={slot.topOffsetMinutes}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type CellProps = {
|
|
|
|
isDisabled?: boolean;
|
|
|
|
topOffsetMinutes?: number;
|
|
|
|
timeSlot: Dayjs;
|
|
|
|
};
|
|
|
|
|
|
|
|
function Cell({ isDisabled, topOffsetMinutes, timeSlot }: CellProps) {
|
|
|
|
const timeFormat = useTimePreferences((state) => state.timeFormat);
|
|
|
|
const { onEmptyCellClick, hoverEventDuration } = useCalendarStore(
|
|
|
|
(state) => ({
|
|
|
|
onEmptyCellClick: state.onEmptyCellClick,
|
|
|
|
hoverEventDuration: state.hoverEventDuration,
|
|
|
|
}),
|
|
|
|
shallow
|
|
|
|
);
|
2023-06-09 09:38:18 +00:00
|
|
|
|
2022-12-14 13:36:10 +00:00
|
|
|
return (
|
|
|
|
<div
|
2023-06-09 09:38:18 +00:00
|
|
|
className={classNames(
|
2023-06-15 09:59:58 +00:00
|
|
|
"group flex w-[calc(100%-1px)] items-center justify-center",
|
2023-06-09 09:38:18 +00:00
|
|
|
isDisabled && "pointer-events-none",
|
2023-06-15 09:59:58 +00:00
|
|
|
!isDisabled && "bg-default dark:bg-muted",
|
|
|
|
topOffsetMinutes && "absolute"
|
2023-06-09 09:38:18 +00:00
|
|
|
)}
|
|
|
|
data-disabled={isDisabled}
|
2023-06-15 09:59:58 +00:00
|
|
|
data-slot={timeSlot.toISOString()}
|
|
|
|
style={{
|
|
|
|
height: `calc(${hoverEventDuration}*var(--one-minute-height))`,
|
|
|
|
overflow: "visible",
|
|
|
|
top: topOffsetMinutes ? `calc(${topOffsetMinutes}*var(--one-minute-height))` : undefined,
|
|
|
|
}}
|
|
|
|
onClick={() => onEmptyCellClick && onEmptyCellClick(timeSlot.toDate())}>
|
2023-06-09 09:38:18 +00:00
|
|
|
{!isDisabled && hoverEventDuration !== 0 && (
|
2022-12-14 13:36:10 +00:00
|
|
|
<div
|
2023-06-15 09:59:58 +00:00
|
|
|
className={classNames(
|
2023-07-19 22:32:25 +00:00
|
|
|
"opacity-4 bg-brand-default hover:bg-brand-default text-brand dark:border-emphasis absolute hidden rounded-[4px] border-[1px] border-gray-900 px-[6px] py-1 text-xs font-semibold leading-5 group-hover:flex group-hover:cursor-pointer",
|
2023-06-15 09:59:58 +00:00
|
|
|
hoverEventDuration && hoverEventDuration > 15 && "items-start pt-3",
|
|
|
|
hoverEventDuration && hoverEventDuration < 15 && "items-center"
|
|
|
|
)}
|
2022-12-14 13:36:10 +00:00
|
|
|
style={{
|
2023-06-13 11:08:00 +00:00
|
|
|
height: `calc(${hoverEventDuration}*var(--one-minute-height) - 2px)`,
|
2023-06-15 09:59:58 +00:00
|
|
|
zIndex: 80,
|
2023-06-09 09:38:18 +00:00
|
|
|
// @TODO: This used to be 90% as per Sean's work. I think this was needed when
|
|
|
|
// multiple events are stacked next to each other. We might need to add this back later.
|
2023-06-13 11:08:00 +00:00
|
|
|
width: "calc(100% - 2px)",
|
2022-12-14 13:36:10 +00:00
|
|
|
}}>
|
2023-06-15 09:59:58 +00:00
|
|
|
<div className="overflow-ellipsis leading-[0]">{timeSlot.format(timeFormat)}</div>
|
2022-12-14 13:36:10 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|