* refactor: move Booker hooks from packages/features to apps/web/modules Migrate the following Booker hooks: - useOverlayCalendar - useSkipConfirmStep - useInitializeWeekStart - useIsQuickAvailabilityCheckFeatureEnabled - useDecoyBooking These hooks are only imported by apps/web files, making them safe to move without creating circular dependencies. Part of the tRPC-driven UI migration from packages/features to apps/web/modules. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix: revert useIsQuickAvailabilityCheckFeatureEnabled move and fix import paths - Reverted useIsQuickAvailabilityCheckFeatureEnabled.ts back to packages/features since it's imported by useSlots.ts in packages/features (would create circular dependency) - Fixed import paths in useOverlayCalendar.ts to use @calcom/features paths - Fixed import paths in useSkipConfirmStep.ts to use @calcom/features paths - Updated Booker.tsx to import useIsQuickAvailabilityCheckFeatureEnabled from original location Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix: use @calcom/web alias for hook imports to fix vitest resolution Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * refactor * migrate more hooks * fix * mv types * fix * fix * fix * fix * revert: remove formatting-only changes to make PR easier to review Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix * fix * fix * revert: restore original import ordering in moved hooks Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix imports * fix * fix * wip * wip * wip * wip * wip * wip * fix: add isBrowser guard to useSlotsViewOnSmallScreen hook Address Cubic AI review feedback (confidence 9/10) by adding the isBrowser guard to the useSlotsViewOnSmallScreen hook to follow the file's client-only contract and avoid executing during SSR/prerendering. Co-Authored-By: unknown <> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
167 lines
6.2 KiB
TypeScript
167 lines
6.2 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
import { shallow } from "zustand/shallow";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider";
|
|
import { useSlotReservationId } from "@calcom/features/bookings/Booker/useSlotReservationId";
|
|
import { isBookingDryRun } from "@calcom/features/bookings/Booker/utils/isBookingDryRun";
|
|
import {
|
|
MINUTES_TO_BOOK,
|
|
PUBLIC_QUERY_RESERVATION_INTERVAL_SECONDS,
|
|
PUBLIC_QUERY_RESERVATION_STALE_TIME_SECONDS,
|
|
} from "@calcom/lib/constants";
|
|
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
|
import { trpc } from "@calcom/trpc/react";
|
|
import type { QuickAvailabilityCheck } from "@calcom/features/bookings/Booker/types";
|
|
import { useIsQuickAvailabilityCheckFeatureEnabled } from "./useIsQuickAvailabilityCheckFeatureEnabled";
|
|
|
|
const useQuickAvailabilityChecks = ({
|
|
eventTypeId,
|
|
eventDuration,
|
|
timeslotsAsISOString,
|
|
slotReservationId,
|
|
}: {
|
|
eventTypeId: number | undefined;
|
|
eventDuration: number;
|
|
timeslotsAsISOString: string[];
|
|
slotReservationId: string | undefined | null;
|
|
}) => {
|
|
// Maintain a cache to ensure previous state is maintained as the request is fetched
|
|
// It is important because tentatively selecting a new timeslot will cause a new request which is uncached.
|
|
const cachedQuickAvailabilityChecksRef = useRef<QuickAvailabilityCheck[]>([]);
|
|
const isQuickAvailabilityCheckFeatureEnabled = useIsQuickAvailabilityCheckFeatureEnabled();
|
|
|
|
if (!isQuickAvailabilityCheckFeatureEnabled) {
|
|
return cachedQuickAvailabilityChecksRef.current;
|
|
}
|
|
|
|
// Create array of slots with their UTC start and end dates
|
|
const slotsToCheck = timeslotsAsISOString.map((slot) => {
|
|
const slotDayjs = dayjs(slot);
|
|
return {
|
|
utcStartIso: slotDayjs.utc().toISOString(),
|
|
utcEndIso: slotDayjs.add(eventDuration, "minutes").utc().toISOString(),
|
|
};
|
|
});
|
|
|
|
const isAvailableQuery = trpc.viewer.slots.isAvailable.useQuery(
|
|
{
|
|
slots: slotsToCheck,
|
|
// enabled flag can't be true if eventTypeId is nullish
|
|
|
|
eventTypeId: eventTypeId!,
|
|
},
|
|
{
|
|
refetchInterval: PUBLIC_QUERY_RESERVATION_INTERVAL_SECONDS * 1000,
|
|
refetchOnWindowFocus: true,
|
|
// We must have slotReservationId because it is possible that slotReservationId is set right after isAvailable request is made and we accidentally could consider the slot as unavailable.
|
|
// isAvailable request also prevents querying reserved slots if uid is not set. We are safe from both sides.
|
|
// TODO: We should move to creating uuid client side for reservation and not waiting for server to set uid cookie.
|
|
enabled: !!(eventTypeId && timeslotsAsISOString.length > 0 && slotReservationId),
|
|
staleTime: PUBLIC_QUERY_RESERVATION_STALE_TIME_SECONDS * 1000,
|
|
}
|
|
);
|
|
|
|
const quickAvailabilityChecks = isAvailableQuery.data?.slots;
|
|
|
|
// Only valid slots response should override the cache. In worst case, we let the actual booking flow(book/event call) decide whether to go ahead with the booking or not.
|
|
if (quickAvailabilityChecks && quickAvailabilityChecks.length > 0) {
|
|
cachedQuickAvailabilityChecksRef.current = quickAvailabilityChecks;
|
|
}
|
|
|
|
return quickAvailabilityChecks || cachedQuickAvailabilityChecksRef.current;
|
|
};
|
|
|
|
export type UseSlotsReturnType = ReturnType<typeof useSlots>;
|
|
|
|
export const useSlots = (event: { id: number; length: number } | null) => {
|
|
const selectedDuration = useBookerStoreContext((state) => state.selectedDuration);
|
|
const searchParams = useCompatSearchParams();
|
|
const [selectedTimeslot, setSelectedTimeslot, tentativeSelectedTimeslots, setTentativeSelectedTimeslots] =
|
|
useBookerStoreContext(
|
|
(state) => [
|
|
state.selectedTimeslot,
|
|
state.setSelectedTimeslot,
|
|
state.tentativeSelectedTimeslots,
|
|
state.setTentativeSelectedTimeslots,
|
|
],
|
|
shallow
|
|
);
|
|
const [slotReservationId, setSlotReservationId] = useSlotReservationId();
|
|
const reserveSlotMutation = trpc.viewer.slots.reserveSlot.useMutation({
|
|
trpc: {
|
|
context: {
|
|
skipBatch: true,
|
|
},
|
|
},
|
|
onSuccess: (data) => {
|
|
setSlotReservationId(data.uid);
|
|
},
|
|
});
|
|
const removeSelectedSlot = trpc.viewer.slots.removeSelectedSlotMark.useMutation({
|
|
trpc: { context: { skipBatch: true } },
|
|
});
|
|
|
|
const handleRemoveSlot = () => {
|
|
if (event?.id && slotReservationId) {
|
|
removeSelectedSlot.mutate({ uid: slotReservationId });
|
|
}
|
|
};
|
|
|
|
const eventTypeId = event?.id;
|
|
const eventDuration = selectedDuration || event?.length || 0;
|
|
const allSelectedTimeslots = [...tentativeSelectedTimeslots, selectedTimeslot].filter(
|
|
(slot): slot is string => slot !== null
|
|
);
|
|
|
|
const allUniqueSelectedTimeslots = Array.from(new Set(allSelectedTimeslots));
|
|
|
|
const quickAvailabilityChecks = useQuickAvailabilityChecks({
|
|
eventTypeId,
|
|
eventDuration,
|
|
timeslotsAsISOString: allUniqueSelectedTimeslots,
|
|
slotReservationId,
|
|
});
|
|
|
|
// In case of skipConfirm flow selectedTimeslot would never be set and instead we could have multiple tentatively selected timeslots, so we pick the latest one from it.
|
|
const timeSlotToBeBooked = selectedTimeslot ?? allSelectedTimeslots.at(-1);
|
|
|
|
const handleReserveSlot = () => {
|
|
if (eventTypeId && timeSlotToBeBooked && eventDuration) {
|
|
reserveSlotMutation.mutate({
|
|
slotUtcStartDate: dayjs(timeSlotToBeBooked).utc().toISOString(),
|
|
eventTypeId,
|
|
slotUtcEndDate: dayjs(timeSlotToBeBooked).utc().add(eventDuration, "minutes").toISOString(),
|
|
_isDryRun: isBookingDryRun(searchParams),
|
|
});
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
handleReserveSlot();
|
|
|
|
const interval = setInterval(() => {
|
|
handleReserveSlot();
|
|
}, parseInt(MINUTES_TO_BOOK) * 60 * 1000 - 2000);
|
|
|
|
return () => {
|
|
handleRemoveSlot();
|
|
clearInterval(interval);
|
|
};
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [event?.id, timeSlotToBeBooked]);
|
|
|
|
return {
|
|
setSelectedTimeslot,
|
|
setTentativeSelectedTimeslots,
|
|
selectedTimeslot,
|
|
tentativeSelectedTimeslots,
|
|
slotReservationId,
|
|
allSelectedTimeslots,
|
|
/**
|
|
* Faster but not that accurate as getSchedule, but doesn't give false positive, so it is usable
|
|
*/
|
|
quickAvailabilityChecks,
|
|
};
|
|
};
|