* 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>
129 lines
3.7 KiB
TypeScript
129 lines
3.7 KiB
TypeScript
"use client";
|
|
/**
|
|
* All hooks defined in this file must be client side hooks and must not be executed in server side.
|
|
* So, they should start with isBrowser check.
|
|
*/
|
|
import useMediaQuery from "@calcom/lib/hooks/useMediaQuery";
|
|
import { sdkActionManager } from "../sdk-event";
|
|
import { useEmbedUiConfig, useIsEmbed } from "../embed-iframe";
|
|
import { embedStore, getEventHasFired, setEventHasFired, getReloadInitiated, setReloadInitiated } from "./lib/embedStore";
|
|
import { isBrowser, isPrerendering } from "./lib/utils";
|
|
|
|
/**
|
|
* Fires bookerViewed, bookerReopened, or bookerReloaded events
|
|
*/
|
|
const fireBookerViewedEvent = ({
|
|
eventId,
|
|
eventSlug,
|
|
slotsLoaded,
|
|
}: {
|
|
eventId: number | undefined;
|
|
eventSlug: string | undefined;
|
|
slotsLoaded: boolean;
|
|
}) => {
|
|
const isFirstTime = embedStore.viewId === 1;
|
|
const isReload = getReloadInitiated();
|
|
const eventName: "bookerViewed" | "bookerReopened" | "bookerReloaded" = isReload ? "bookerReloaded" : isFirstTime ? "bookerViewed" : "bookerReopened";
|
|
|
|
if (getEventHasFired(eventName)) {
|
|
return;
|
|
}
|
|
|
|
fireEvent(eventName);
|
|
|
|
function fireEvent(eventName: "bookerViewed" | "bookerReopened" | "bookerReloaded") {
|
|
setEventHasFired(eventName, true);
|
|
if (slotsLoaded) {
|
|
if (eventId && eventSlug) {
|
|
sdkActionManager?.fire(eventName, {
|
|
eventId,
|
|
eventSlug,
|
|
slotsLoaded: true,
|
|
});
|
|
} else {
|
|
// This situation shouldn't arise but not throwing error because for a tracking event we don't want to crash booker
|
|
console.error("BookerViewed event not fired because slotsLoaded is true but eventId or eventSlug are falsy");
|
|
}
|
|
} else {
|
|
sdkActionManager?.fire(eventName, {
|
|
eventId: null,
|
|
eventSlug: null,
|
|
slotsLoaded: false,
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Fires bookerReady event when booker view is loaded and slots are ready.
|
|
* Only fires once per link view when slots are successfully loaded.
|
|
*/
|
|
const fireBookerReadyEvent = ({
|
|
eventId,
|
|
eventSlug,
|
|
slotsLoaded,
|
|
}: {
|
|
eventId: number;
|
|
eventSlug: string;
|
|
slotsLoaded: boolean;
|
|
}) => {
|
|
if (!slotsLoaded) {
|
|
return;
|
|
}
|
|
if (!getEventHasFired("bookerReady")) {
|
|
sdkActionManager?.fire("bookerReady", { eventId, eventSlug });
|
|
setEventHasFired("bookerReady", true);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Hook that fires embed events (bookerViewed/bookerReopened/bookerReloaded and bookerReady).
|
|
* Manages event state using embedStore to prevent duplicate events and track modal reopens/reloads.
|
|
*/
|
|
export const useBookerEmbedEvents = ({
|
|
eventId,
|
|
eventSlug,
|
|
schedule,
|
|
}: {
|
|
eventId: number | undefined;
|
|
eventSlug: string | undefined;
|
|
schedule: {
|
|
isSuccess: boolean;
|
|
dataUpdatedAt: number;
|
|
};
|
|
}) => {
|
|
if (!isBrowser) {
|
|
return;
|
|
}
|
|
const viewId = embedStore.viewId;
|
|
|
|
if (isPrerendering() || !viewId) {
|
|
return;
|
|
}
|
|
// first BookerViewed event happens, followed by bookerReady if slots are loaded
|
|
fireBookerViewedEvent({
|
|
eventId,
|
|
eventSlug,
|
|
slotsLoaded: schedule.isSuccess,
|
|
});
|
|
|
|
if (eventId && eventSlug) {
|
|
fireBookerReadyEvent({
|
|
eventId,
|
|
eventSlug,
|
|
slotsLoaded: schedule.isSuccess,
|
|
});
|
|
}
|
|
};
|
|
|
|
export const useSlotsViewOnSmallScreen = () => {
|
|
const isEmbed = useIsEmbed();
|
|
const isMobile = useMediaQuery("(max-width: 768px)");
|
|
|
|
const embedUiConfig = useEmbedUiConfig();
|
|
|
|
if (!isBrowser || !isEmbed || !isMobile) return false;
|
|
|
|
return embedUiConfig.useSlotsViewOnSmallScreen ?? false;
|
|
};
|