Files
calendar/apps/web/modules/bookings/hooks/useSlots.ts
T
Eunjae LeeGitHubClaude Opus 4.5Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
81e4241f85 refactor: apply biome formatting to apps/web (#27692)
* refactor: apply biome formatting to apps/web (batch 1)

Formats apps/web non-modules directories:
- apps/web/app
- apps/web/lib
- apps/web/pages
- apps/web/styles
- apps/web/server
- apps/web/test
- Root-level .ts and .mjs files

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: apply biome formatting to apps/web/modules (batch 2)

Formats smaller apps/web/modules directories:
- onboarding, data-table, users, apps, auth
- integration-attribute-sync, shell, availability
- feature-flags, team, webhooks, getting-started
- feature-opt-in, troubleshooter, schedules, notifications
- signup-view.tsx

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: apply biome formatting to apps/web/modules (batch 3)

Formats medium apps/web/modules directories:
- bookings
- event-types
- insights
- embed

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: apply biome formatting to apps/web/modules/ee (batch 4)

Formats apps/web/modules/ee directory.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: apply biome formatting to apps/web (batch 5)

Formats remaining apps/web directories:
- apps/web/modules/settings
- apps/web/modules/booking-audit
- apps/web/playwright

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: apply biome formatting to apps/web/components (batch 6)

Formats remaining apps/web/components files.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: apply biome formatting to new apps/web files from main

Formats newly added/modified files from main merge:
- WorkflowStepContainer.tsx (resolved merge conflicts)
- Newly moved files (blocklist, form-builder, schedules, etc.)
- Other files modified in main

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: apply biome formatting to new apps/web files from main

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-10 23:25:24 +05:30

170 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,
};
};