feat: don't update query params for booker atom (#20000)

* feat: Add `omitUpdatingParams` prop to Booker store for query param control

* remove console

* Update booking.tsx

* fix: type check failing

* undo changes

* feat: Expose `onBookerStateChange` callback to track `booker-store` updates

* add `onBookerStateChange` docs

* Update store.ts

* added `allowUpdatingUrlParams` for backwards compatibility
This commit is contained in:
Somay Chauhan authored and GitHub committed 2025-03-13 23:47:24 +05:30
1 parent f8aa2701b6
commit b1ebefb854
5 files changed
+141 -15

No files matched your search

@@ -1,5 +1,7 @@
import { useQueryClient } from "@tanstack/react-query";
import { useMemo, useEffect, useCallback, useState } from "react";
// eslint-disable-next-line no-restricted-imports
import debounce from "lodash/debounce";
import { useMemo, useEffect, useCallback, useState, useRef } from "react";
import { shallow } from "zustand/shallow";
import dayjs from "@calcom/dayjs";
@@ -40,6 +42,7 @@ import { AtomsWrapper } from "../src/components/atoms-wrapper";
import type {
BookerPlatformWrapperAtomPropsForIndividual,
BookerPlatformWrapperAtomPropsForTeam,
BookerStoreValues,
} from "./types";
export const BookerPlatformWrapper = (
@@ -53,6 +56,8 @@ export const BookerPlatformWrapper = (
crmAppSlug,
crmOwnerRecordType,
preventEventTypeRedirect,
onBookerStateChange,
allowUpdatingUrlParams = false,
} = props;
const layout = BookerLayouts[view];
@@ -70,6 +75,48 @@ export const BookerPlatformWrapper = (
const [isOverlayCalendarEnabled, setIsOverlayCalendarEnabled] = useState(
Boolean(localStorage?.getItem?.("overlayCalendarSwitchDefault"))
);
const prevStateRef = useRef<BookerStoreValues | null>(null);
const getStateValues = useCallback(
(state: ReturnType<typeof useBookerStore.getState>): BookerStoreValues => {
return Object.fromEntries(
Object.entries(state).filter(([_, value]) => typeof value !== "function")
) as BookerStoreValues;
},
[]
);
const debouncedStateChange = useMemo(() => {
return debounce(
(currentStateValues: BookerStoreValues, callback: (values: BookerStoreValues) => void) => {
const prevState = prevStateRef.current;
const stateChanged = !prevState || JSON.stringify(prevState) !== JSON.stringify(currentStateValues);
if (stateChanged) {
callback(currentStateValues);
prevStateRef.current = currentStateValues;
}
},
50
);
}, []);
useEffect(() => {
if (!onBookerStateChange) return;
const unsubscribe = useBookerStore.subscribe((state) => {
const currentStateValues = getStateValues(state);
debouncedStateChange(currentStateValues, onBookerStateChange);
});
// Initial call with current state
const initialState = getStateValues(useBookerStore.getState());
onBookerStateChange(initialState);
prevStateRef.current = initialState;
return () => {
unsubscribe();
debouncedStateChange.cancel();
};
}, [onBookerStateChange, getStateValues, debouncedStateChange]);
useGetBookingForReschedule({
uid: props.rescheduleUid ?? props.bookingUid ?? "",
@@ -166,6 +213,8 @@ export const BookerPlatformWrapper = (
org: props.entity?.orgSlug,
username,
bookingData,
isPlatform: true,
allowUpdatingUrlParams,
});
const [dayCount] = useBookerStore((state) => [state.dayCount, state.setDayCount], shallow);
const selectedDate = useBookerStore((state) => state.selectedDate);
@@ -397,6 +446,13 @@ export const BookerPlatformWrapper = (
},
[setIsOverlayCalendarEnabled]
);
const selectedDateProp = useMemo(
() => dayjs(props.selectedDate).format("YYYY-MM-DD"),
[props.selectedDate]
);
useEffect(() => {
setSelectedDate(selectedDateProp, true);
}, [selectedDateProp]);
useEffect(() => {
// reset booker whenever it's unmounted
@@ -408,7 +464,6 @@ export const BookerPlatformWrapper = (
setSelectedDuration(null);
setOrg(null);
setSelectedMonth(null);
setSelectedDuration(null);
if (props.rescheduleUid) {
// clean booking data from cache
queryClient.removeQueries({
+27
View File
@@ -1,4 +1,5 @@
import type { BookerProps } from "@calcom/features/bookings/Booker";
import type { BookerStore } from "@calcom/features/bookings/Booker/store";
import type { BookingResponse } from "@calcom/platform-libraries";
import type {
ApiSuccessResponse,
@@ -10,6 +11,30 @@ import type { BookerLayouts } from "@calcom/prisma/zod-utils";
import type { UseCreateBookingInput } from "../hooks/bookings/useCreateBooking";
// Type that includes only the data values from BookerStore (excluding functions)
export type BookerStoreValues = Omit<
BookerStore,
| "setState"
| "setLayout"
| "setSelectedDate"
| "setSelectedDatesAndTimes"
| "addToSelectedDate"
| "setVerifiedEmail"
| "setMonth"
| "setDayCount"
| "setSeatedEventData"
| "setTimezone"
| "initialize"
| "setSelectedDuration"
| "setBookingData"
| "setRecurringEventCount"
| "setOccurenceCount"
| "setTentativeSelectedTimeslots"
| "setSelectedTimeslot"
| "setFormValues"
| "setOrg"
>;
export type BookerPlatformWrapperAtomProps = Omit<
BookerProps,
"username" | "entity" | "isTeamEvent" | "teamId"
@@ -39,6 +64,7 @@ export type BookerPlatformWrapperAtomProps = Omit<
onReserveSlotError?: (data: ApiErrorResponse) => void;
onDeleteSlotSuccess?: (data: ApiSuccessResponseWithoutData) => void;
onDeleteSlotError?: (data: ApiErrorResponse) => void;
onBookerStateChange?: (state: BookerStoreValues) => void;
locationUrl?: string;
view?: VIEW_TYPE;
metadata?: Record<string, string>;
@@ -46,6 +72,7 @@ export type BookerPlatformWrapperAtomProps = Omit<
onDryRunSuccess?: () => void;
hostsLimit?: number;
preventEventTypeRedirect?: boolean;
allowUpdatingUrlParams?: boolean;
};
type VIEW_TYPE = keyof typeof BookerLayouts;
@@ -101,6 +101,10 @@ export default function Bookings(props: { calUsername: string; calEmail: string
router.push(`/${data.data.uid}`);
}
}}
onBookerStateChange={(bookerState) => {
console.log("Booker state updated:", bookerState);
// You can perform any actions based on the updated state here
}}
metadata={{ CustomKey: "CustomValue" }}
duration={eventTypeDuration}
customClassNames={{
@@ -120,6 +124,8 @@ export default function Bookings(props: { calUsername: string; calEmail: string
? { isTeamEvent: true, teamId: teams?.[0]?.id || 0 }
: { username: props.calUsername })}
hostsLimit={3}
selectedDate={new Date("2025-03-25")}
allowUpdatingUrlParams={true}
/>
</>
)}