* feat: add embed prerendering support and enhance event handling - Introduced `useIsEmbedPrerendering` hook to determine if the embed is in prerender mode. - Updated `useAvailabilityEvents` to prevent firing events during prerendering. - Added `bookerLoadedEvent` and `availabilityRefreshed` types to `EventDataMap`. - Implemented `useFirebookerLoadedEvent` to manage firing the booker loaded event conditionally. - Refactored event firing logic in `useSchedule` and `BookerWebWrapper` components to utilize new hooks. * feat: add embed prerendering support and enhance event handling - Introduced `useIsEmbedPrerendering` hook to determine if the embed is in prerender mode. - Updated `useAvailabilityEvents` to prevent firing events during prerendering. - Added `bookerLoadedEvent` and `availabilityRefreshed` types to `EventDataMap`. - Implemented `useFirebookerLoadedEvent` to manage firing the booker loaded event conditionally. - Refactored event firing logic in `useSchedule` and `BookerWebWrapper` components to utilize new hooks. * feat: enhance embed event handling and introduce link reopening detection - Added `useEmbedReopened` hook to track when the embed is reopened. - Updated `BookerWebWrapper` to reset event firing state upon embed reopening. - Refactored event firing logic to use `bookerViewed` instead of `bookerLoadedEvent`. - Introduced scheduling for event firing in `useSchedule` to ensure correct order of events during prerendering. * feat: add lifecycle diagrams for inline and modal embeds - Introduced `inline-embed-lifecycle.mermaid` and `modal-embed-lifecycle.mermaid` files to visualize the lifecycle events and states of inline and modal embeds. - Updated `LIFECYCLE.md` to reference the new diagrams and provide a clearer explanation of the embed lifecycle processes. - Added `modal-prerendering-flow.mermaid` to illustrate the prerendering flow for modal embeds. - Enhanced the routing playground with new features and improved event handling for availability and booking events. * refactor: update event handling for booker lifecycle events - Replaced `availabilityLoaded` event with `bookerReady` to better reflect the state when the booker view is fully loaded and ready for interaction. - Updated related documentation and diagrams to reflect changes in event triggers and descriptions. - Adjusted internal state management to track `viewId` instead of `reopenCount` for distinguishing between initial views and reopens. - Added tests for new event handling logic to ensure correct firing of `bookerViewed`, `bookerReopened`, and `bookerReady` events. * refactor: enhance embed event handling and state management - Updated event handling for booker lifecycle events, replacing `resetViewVariables` with `resetPageData` to manage page-specific state. - Introduced new utility functions for managing event firing states and reload initiation. - Refactored `fireBookerViewedEvent` and `fireBookerReadyEvent` to utilize the new state management functions. - Added comprehensive tests for the updated event handling logic and state resets to ensure correct functionality across various scenarios. * refactor: update embed iframe configuration and utility functions - Reduced `slotsStaleTimeMs` from 30 seconds to 10 seconds and `iframeForceReloadThresholdMs` from 100 seconds to 30 seconds for improved responsiveness. - Refactored utility functions to use `isBrowser` for client-side checks instead of `isClientSide`. - Removed unused `isPrerendering` function and updated related documentation for clarity. - Enhanced event handling by exporting `useBookerEmbedEvents` from the appropriate module for better accessibility. * refactor: update embed iframe configuration and utility functions - Reduced `slotsStaleTimeMs` from 30 seconds to 10 seconds and `iframeForceReloadThresholdMs` from 100 seconds to 30 seconds for improved responsiveness. - Refactored utility functions to use `isBrowser` for client-side checks instead of `isClientSide`. - Removed unused `isPrerendering` function and updated related documentation for clarity. - Enhanced event handling by exporting `useBookerEmbedEvents` from the appropriate module for better accessibility. * fix cubic feedback
363 lines
12 KiB
TypeScript
363 lines
12 KiB
TypeScript
type Namespace = string;
|
|
type CustomEventDetail = Record<string, unknown>;
|
|
|
|
function _fireEvent(fullName: string, detail: CustomEventDetail) {
|
|
const event = new window.CustomEvent(fullName, {
|
|
detail: detail,
|
|
});
|
|
|
|
window.dispatchEvent(event);
|
|
}
|
|
|
|
type BaseBookingEventPayload = {
|
|
title: string | undefined;
|
|
startTime: string | undefined;
|
|
endTime: string | undefined;
|
|
eventTypeId: number | null | undefined;
|
|
status: string | undefined;
|
|
paymentRequired: boolean;
|
|
isRecurring: boolean;
|
|
/**
|
|
* This is only used for recurring bookings
|
|
*/
|
|
allBookings?: { startTime: string; endTime: string }[];
|
|
videoCallUrl?: string;
|
|
};
|
|
|
|
export type EventDataMap = {
|
|
/**
|
|
* Fired when an event type is selected in the booker.
|
|
* Purpose: Track user selection of event types.
|
|
*/
|
|
eventTypeSelected: {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
eventType: any;
|
|
};
|
|
/**
|
|
* Fired when the embed page fails to load.
|
|
* Purpose: Signal page load errors to parent.
|
|
* Triggers: On non-200 HTTP status from iframe.
|
|
* Includes: Error code, message, and URL information.
|
|
*/
|
|
linkFailed: {
|
|
code: string;
|
|
msg: string;
|
|
data: {
|
|
url: string;
|
|
};
|
|
};
|
|
/**
|
|
* Fired when iframe content is fully ready for user interaction.
|
|
* Purpose: Signal that embed is ready and loader should be removed.
|
|
* Triggers: After content height is known and slots are loaded (if skeleton loader is used).
|
|
* Actions: Parent removes loader and makes iframe visible.
|
|
*/
|
|
linkReady: Record<string, never>;
|
|
/**
|
|
* Fired when prerendered embed is ready in the background.
|
|
* Purpose: Signal that prerender phase is complete.
|
|
* Triggers: When parentKnowsIframeReady is called during prerendering.
|
|
* Note: Embed stays hidden until connect() is called.
|
|
*/
|
|
linkPrerendered: Record<string, never>;
|
|
/**
|
|
* Fired when booker view is loaded and slots are fully ready for user interaction.
|
|
* Purpose: Signal that booker is ready with slots loaded, allowing users to select a slot.
|
|
* Triggers: When booker view is loaded and slots are successfully loaded.
|
|
* Note: Only fires for booker pages (not booking success view or other non-booker pages).
|
|
* This is different from linkReady which fires for any embed page.
|
|
*/
|
|
bookerReady: {
|
|
eventId: number;
|
|
eventSlug: string;
|
|
};
|
|
/**
|
|
* @deprecated Use `bookerReady` instead. This event is kept for backward compatibility.
|
|
* Fired when availability/slots data has been loaded for the first time.
|
|
* Purpose: Track first successful slots load.
|
|
* Triggers: On first successful slots load.
|
|
* Note: Only fires when slots are successfully loaded.
|
|
*/
|
|
availabilityLoaded: {
|
|
eventId: number;
|
|
eventSlug: string;
|
|
};
|
|
/**
|
|
* Fired when booker has been viewed for the first time on first modal open.
|
|
* Purpose: Track first view of booker in current session.
|
|
* Triggers: On first linkReady event for a page view.
|
|
* Note: Not fired during prerendering. Includes event information and slots loading status.
|
|
*/
|
|
bookerViewed:
|
|
| {
|
|
eventId: number;
|
|
eventSlug: string;
|
|
slotsLoaded: true;
|
|
}
|
|
| {
|
|
eventId: null;
|
|
eventSlug: null;
|
|
slotsLoaded: false;
|
|
};
|
|
/**
|
|
* Fired when booker has been reopened after being closed.
|
|
* Purpose: Track when booker is reopened, distinguishing from first view.
|
|
* Triggers: When modal is reopened after being closed (no reload).
|
|
* Note: Distinguishes between first view (bookerViewed) and reopen (bookerReopened).
|
|
*/
|
|
bookerReopened:
|
|
| {
|
|
eventId: number;
|
|
eventSlug: string;
|
|
slotsLoaded: true;
|
|
}
|
|
| {
|
|
eventId: null;
|
|
eventSlug: null;
|
|
slotsLoaded: false;
|
|
};
|
|
/**
|
|
* Fired when booker has been reloaded (full page reload within modal).
|
|
* Purpose: Track when booker is reloaded, distinguishing from first view and reopen.
|
|
* Triggers: When fullReload action is taken (modal stays but iframe content is reloaded).
|
|
* Note: Distinguishes between first view (bookerViewed), reopen (bookerReopened), and reload (bookerReloaded).
|
|
*/
|
|
bookerReloaded:
|
|
| {
|
|
eventId: number;
|
|
eventSlug: string;
|
|
slotsLoaded: true;
|
|
}
|
|
| {
|
|
eventId: null;
|
|
eventSlug: null;
|
|
slotsLoaded: false;
|
|
};
|
|
/**
|
|
* Fired when connect() method has been called to update prerendered embed.
|
|
* Purpose: Signal that prerendered embed is being connected with new configuration.
|
|
* Triggers: When connect() is invoked on a prerendered embed.
|
|
* Note: Only fires during prerender -> non-prerender transition.
|
|
*/
|
|
__connectInitiated: Record<string, never>;
|
|
/**
|
|
* Fired when connect flow has completed successfully.
|
|
* Purpose: Signal that connect flow has finished updating the embed.
|
|
* Triggers: After connect() finishes updating URL params and slots are ready.
|
|
* Actions: Fires linkReady event with isPrerendered: true.
|
|
*/
|
|
__connectCompleted: Record<string, never>;
|
|
/**
|
|
* Fired when a booking is successfully created.
|
|
* Purpose: Notify parent of successful booking creation.
|
|
* Triggers: After booking is confirmed and saved.
|
|
* Note: Lightweight version with restricted data fields.
|
|
*/
|
|
bookingSuccessfulV2: BaseBookingEventPayload & {
|
|
uid: string | undefined;
|
|
};
|
|
|
|
/**
|
|
* @deprecated Use `bookingSuccessfulV2` instead. We restrict the data heavily there, only sending what is absolutely needed and keeping it light as well. Plus, more importantly that can be documented well.
|
|
* Fired when a booking is successfully created.
|
|
* Purpose: Notify parent of successful booking creation.
|
|
*/
|
|
bookingSuccessful: {
|
|
// TODO: Shouldn't send the entire booking and eventType objects, we should send specific fields from them.
|
|
booking: unknown;
|
|
eventType: unknown;
|
|
date: string;
|
|
duration: number | undefined;
|
|
organizer: {
|
|
name: string;
|
|
email: string;
|
|
timeZone: string;
|
|
};
|
|
confirmed: boolean;
|
|
};
|
|
/**
|
|
* Fired when a booking is successfully rescheduled.
|
|
* Purpose: Notify parent of successful booking reschedule.
|
|
* Triggers: After reschedule is confirmed and saved.
|
|
* Note: Lightweight version with restricted data fields.
|
|
*/
|
|
rescheduleBookingSuccessfulV2: BaseBookingEventPayload & {
|
|
uid: string | undefined;
|
|
};
|
|
/**
|
|
* Fired when a dry-run booking validation is successful.
|
|
* Purpose: Notify parent of successful booking validation without creating the booking.
|
|
* Triggers: After booking validation passes in dry-run mode.
|
|
*/
|
|
dryRunBookingSuccessfulV2: BaseBookingEventPayload;
|
|
/**
|
|
* Fired when a dry-run reschedule validation is successful.
|
|
* Purpose: Notify parent of successful reschedule validation without updating the booking.
|
|
* Triggers: After reschedule validation passes in dry-run mode.
|
|
*/
|
|
dryRunRescheduleBookingSuccessfulV2: BaseBookingEventPayload;
|
|
/**
|
|
* @deprecated Use `rescheduleBookingSuccessfulV2` instead. We restrict the data heavily there, only sending what is absolutely needed and keeping it light as well. Plus, more importantly that can be documented well.
|
|
* Fired when a booking is successfully rescheduled.
|
|
* Purpose: Notify parent of successful booking reschedule.
|
|
*/
|
|
rescheduleBookingSuccessful: {
|
|
booking: unknown;
|
|
eventType: unknown;
|
|
date: string;
|
|
duration: number | undefined;
|
|
organizer: {
|
|
name: string;
|
|
email: string;
|
|
timeZone: string;
|
|
};
|
|
confirmed: boolean;
|
|
};
|
|
/**
|
|
* Fired when a booking is cancelled.
|
|
* Purpose: Notify parent of booking cancellation.
|
|
*/
|
|
bookingCancelled: {
|
|
booking: unknown;
|
|
organizer: {
|
|
name: string;
|
|
email: string;
|
|
timeZone?: string | undefined;
|
|
};
|
|
eventType: unknown;
|
|
};
|
|
/**
|
|
* Fired when routing rules redirect to a custom action.
|
|
* Purpose: Notify parent of routing action taken.
|
|
*/
|
|
routed: {
|
|
actionType: "customPageMessage" | "externalRedirectUrl" | "eventTypeRedirectUrl";
|
|
actionValue: string;
|
|
};
|
|
/**
|
|
* Fired when navigation to booker page occurs.
|
|
* Purpose: Track navigation to booker.
|
|
*/
|
|
navigatedToBooker: Record<string, never>;
|
|
/**
|
|
* Wildcard event that fires for all events.
|
|
* Purpose: Allow listening to all events with a single handler.
|
|
*/
|
|
"*": Record<string, unknown>;
|
|
/**
|
|
* Fired when route changes within the embed.
|
|
* Purpose: Notify parent of navigation within embed.
|
|
* Triggers: When URL changes via history API or navigation.
|
|
* Actions: Parent can handle navigation (e.g., scroll into view for inline embeds).
|
|
*/
|
|
__routeChanged: Record<string, never>;
|
|
/**
|
|
* Fired when window load event has completed.
|
|
* Purpose: Signal that dimension calculations are now reliable.
|
|
* Triggers: After document.readyState is "complete" and Safari rendering delay has passed.
|
|
* Note: Used internally to ensure accurate dimension calculations.
|
|
*/
|
|
__windowLoadComplete: Record<string, never>;
|
|
/**
|
|
* Fired when iframe requests to be closed.
|
|
* Purpose: Request to close modal from within iframe.
|
|
* Actions: Parent sets modal state to "closed".
|
|
*/
|
|
__closeIframe: Record<string, never>;
|
|
/**
|
|
* Fired when iframe is ready to receive messages from parent.
|
|
* Purpose: Signal that embed is ready to receive messages.
|
|
* Triggers: When embed SDK initializes and is ready for communication.
|
|
* Actions: Makes iframe visible (unless prerendering) and processes queued commands.
|
|
*/
|
|
__iframeReady: {
|
|
isPrerendering: boolean;
|
|
};
|
|
/**
|
|
* Fired when iframe content dimensions change.
|
|
* Purpose: Keep iframe size matched to content to prevent scrollbars.
|
|
* Triggers: On initial load, when content size changes, or after window load completes.
|
|
* Actions: Parent adjusts iframe dimensions.
|
|
*/
|
|
__dimensionChanged: {
|
|
iframeHeight: number;
|
|
iframeWidth: number;
|
|
isFirstTime: boolean;
|
|
};
|
|
/**
|
|
* Fired to request scrolling by a specific distance.
|
|
* Purpose: Allow iframe to request parent to scroll.
|
|
* Actions: Parent scrolls the scrollable ancestor by the specified distance.
|
|
*/
|
|
__scrollByDistance: {
|
|
/**
|
|
* Distance in pixels to scroll by.
|
|
*/
|
|
distance: number;
|
|
};
|
|
};
|
|
|
|
export type EventData<T extends keyof EventDataMap> = {
|
|
[K in T]: {
|
|
type: string;
|
|
namespace: string;
|
|
fullType: string;
|
|
data: EventDataMap[K];
|
|
};
|
|
}[T];
|
|
|
|
export type EmbedEvent<T extends keyof EventDataMap> = CustomEvent<EventData<T>>;
|
|
|
|
export class SdkActionManager {
|
|
namespace: Namespace;
|
|
|
|
static parseAction(fullType: string) {
|
|
if (!fullType) {
|
|
return null;
|
|
}
|
|
//FIXME: Ensure that any action if it has :, it is properly encoded.
|
|
const [cal, calNamespace, type] = fullType.split(":");
|
|
if (cal !== "CAL") {
|
|
return null;
|
|
}
|
|
return {
|
|
ns: calNamespace,
|
|
type,
|
|
};
|
|
}
|
|
|
|
getFullActionName(name: string) {
|
|
return this.namespace ? `CAL:${this.namespace}:${name}` : `CAL::${name}`;
|
|
}
|
|
|
|
fire<T extends keyof EventDataMap>(name: T, data: EventDataMap[T]) {
|
|
const fullName = this.getFullActionName(name);
|
|
const detail = {
|
|
type: name,
|
|
namespace: this.namespace,
|
|
fullType: fullName,
|
|
data,
|
|
};
|
|
|
|
_fireEvent(fullName, detail);
|
|
|
|
// Wildcard Event
|
|
_fireEvent(this.getFullActionName("*"), detail);
|
|
}
|
|
|
|
on<T extends keyof EventDataMap>(name: T, callback: (arg0: CustomEvent<EventData<T>>) => void) {
|
|
const fullName = this.getFullActionName(name);
|
|
window.addEventListener(fullName, callback as EventListener);
|
|
}
|
|
|
|
off<T extends keyof EventDataMap>(name: T, callback: (arg0: CustomEvent<EventData<T>>) => void) {
|
|
const fullName = this.getFullActionName(name);
|
|
window.removeEventListener(fullName, callback as EventListener);
|
|
}
|
|
|
|
constructor(ns: string | null) {
|
|
ns = ns || "";
|
|
this.namespace = ns;
|
|
}
|
|
}
|