mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
* 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: [email protected] <[email protected]> * 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: [email protected] <[email protected]> * fix: use @calcom/web alias for hook imports to fix vitest resolution Co-Authored-By: [email protected] <[email protected]> * 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: [email protected] <[email protected]> * fix * fix * fix * revert: restore original import ordering in moved hooks Co-Authored-By: [email protected] <[email protected]> * 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>
143 lines
6.1 KiB
TypeScript
143 lines
6.1 KiB
TypeScript
import { useSearchParams } from "next/navigation";
|
|
|
|
import { useIsPlatform } from "@calcom/atoms/hooks/useIsPlatform";
|
|
import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider";
|
|
import { useBookerTime } from "@calcom/features/bookings/Booker/hooks/useBookerTime";
|
|
import type { UseBookingFormReturnType } from "@calcom/features/bookings/Booker/hooks/useBookingForm";
|
|
import { mapBookingToMutationInput, mapRecurringBookingToMutationInput } from "@calcom/features/bookings/lib";
|
|
import type { BookingCreateBody } from "@calcom/features/bookings/lib/bookingCreateBodySchema";
|
|
import type { BookerEvent } from "@calcom/features/bookings/types";
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
import type { RoutingFormSearchParams } from "@calcom/platform-types";
|
|
import { showToast } from "@calcom/ui/components/toast";
|
|
|
|
import { getUtmTrackingParameters } from "../../lib/getUtmTrackingParameters";
|
|
import type { UseCreateBookingInput } from "./useCreateBooking";
|
|
|
|
type Callbacks = { onSuccess?: () => void; onError?: (err: unknown) => void };
|
|
type UseHandleBookingProps = {
|
|
bookingForm: UseBookingFormReturnType["bookingForm"];
|
|
event?: {
|
|
data?: Pick<
|
|
BookerEvent,
|
|
"id" | "isDynamic" | "metadata" | "recurringEvent" | "length" | "slug" | "schedulingType"
|
|
> | null;
|
|
};
|
|
metadata: Record<string, string>;
|
|
hashedLink?: string | null;
|
|
handleBooking: (input: UseCreateBookingInput, callbacks?: Callbacks) => void;
|
|
handleInstantBooking: (input: BookingCreateBody, callbacks?: Callbacks) => void;
|
|
handleRecBooking: (input: BookingCreateBody[], callbacks?: Callbacks) => void;
|
|
locationUrl?: string;
|
|
routingFormSearchParams?: RoutingFormSearchParams;
|
|
isBookingDryRun?: boolean;
|
|
rrHostSubsetIds?: number[];
|
|
};
|
|
|
|
export const useHandleBookEvent = ({
|
|
bookingForm,
|
|
event,
|
|
metadata,
|
|
hashedLink,
|
|
handleBooking,
|
|
handleInstantBooking,
|
|
handleRecBooking,
|
|
locationUrl,
|
|
routingFormSearchParams,
|
|
isBookingDryRun,
|
|
rrHostSubsetIds,
|
|
}: UseHandleBookingProps) => {
|
|
const isPlatform = useIsPlatform();
|
|
const setFormValues = useBookerStoreContext((state) => state.setFormValues);
|
|
const storeTimeSlot = useBookerStoreContext((state) => state.selectedTimeslot);
|
|
const duration = useBookerStoreContext((state) => state.selectedDuration);
|
|
const { timezone } = useBookerTime();
|
|
const rescheduleUid = useBookerStoreContext((state) => state.rescheduleUid);
|
|
const rescheduledBy = useBookerStoreContext((state) => state.rescheduledBy);
|
|
const { t, i18n } = useLocale();
|
|
const username = useBookerStoreContext((state) => state.username);
|
|
const recurringEventCount = useBookerStoreContext((state) => state.recurringEventCount);
|
|
const bookingData = useBookerStoreContext((state) => state.bookingData);
|
|
const seatedEventData = useBookerStoreContext((state) => state.seatedEventData);
|
|
const isInstantMeeting = useBookerStoreContext((state) => state.isInstantMeeting);
|
|
const orgSlug = useBookerStoreContext((state) => state.org);
|
|
const teamMemberEmail = useBookerStoreContext((state) => state.teamMemberEmail);
|
|
const crmOwnerRecordType = useBookerStoreContext((state) => state.crmOwnerRecordType);
|
|
const crmAppSlug = useBookerStoreContext((state) => state.crmAppSlug);
|
|
const crmRecordId = useBookerStoreContext((state) => state.crmRecordId);
|
|
const verificationCode = useBookerStoreContext((state) => state.verificationCode);
|
|
const handleError = (err: unknown) => {
|
|
const errorMessage = err instanceof Error ? t(err.message) : t("can_you_try_again");
|
|
showToast(errorMessage, "error");
|
|
};
|
|
const searchParams = useSearchParams();
|
|
|
|
const handleBookEvent = (inputTimeSlot?: string) => {
|
|
const values = bookingForm.getValues();
|
|
const timeslot = inputTimeSlot ?? storeTimeSlot;
|
|
const callbacks = inputTimeSlot && !isPlatform ? { onError: handleError } : undefined;
|
|
if (timeslot) {
|
|
// Clears form values stored in store, so old values won't stick around.
|
|
setFormValues({});
|
|
bookingForm.clearErrors();
|
|
|
|
// It shouldn't be possible that this method is fired without having event data,
|
|
// but since in theory (looking at the types) it is possible, we still handle that case.
|
|
if (!event?.data) {
|
|
bookingForm.setError("globalError", { message: t("error_booking_event") });
|
|
return;
|
|
}
|
|
|
|
// Ensures that duration is an allowed value, if not it defaults to the
|
|
// default event duration.
|
|
const validDuration = event.data.isDynamic
|
|
? duration || event.data.length
|
|
: duration && event.data.metadata?.multipleDuration?.includes(duration)
|
|
? duration
|
|
: event.data.length;
|
|
|
|
const bookingInput = {
|
|
values,
|
|
duration: validDuration,
|
|
event: event.data,
|
|
date: timeslot,
|
|
timeZone: timezone,
|
|
language: i18n.language,
|
|
rescheduleUid: rescheduleUid || undefined,
|
|
rescheduledBy: rescheduledBy || undefined,
|
|
bookingUid: (bookingData && bookingData.uid) || seatedEventData?.bookingUid || undefined,
|
|
username: username || "",
|
|
metadata: metadata,
|
|
hashedLink,
|
|
teamMemberEmail,
|
|
crmOwnerRecordType,
|
|
crmAppSlug,
|
|
crmRecordId,
|
|
orgSlug: orgSlug ? orgSlug : undefined,
|
|
routingFormSearchParams,
|
|
isDryRunProp: isBookingDryRun,
|
|
verificationCode: verificationCode || undefined,
|
|
rrHostSubsetIds,
|
|
};
|
|
|
|
const tracking = getUtmTrackingParameters(searchParams);
|
|
|
|
if (isInstantMeeting) {
|
|
handleInstantBooking(mapBookingToMutationInput(bookingInput), callbacks);
|
|
} else if (event.data?.recurringEvent?.freq != null && recurringEventCount && !rescheduleUid) {
|
|
handleRecBooking(
|
|
mapRecurringBookingToMutationInput(bookingInput, recurringEventCount, tracking),
|
|
callbacks
|
|
);
|
|
} else {
|
|
handleBooking({ ...mapBookingToMutationInput(bookingInput), locationUrl, tracking }, callbacks);
|
|
}
|
|
// Clears form values stored in store, so old values won't stick around.
|
|
setFormValues({});
|
|
bookingForm.clearErrors();
|
|
}
|
|
};
|
|
|
|
return handleBookEvent;
|
|
};
|