* feat: useApiV2AvailableSlots * feat: useApiV2AvailableSlots * fix type check * remove log * fix unit tests * simplify * feat: add missing params to the slots endpoint * updated documentation * _shouldServeCache is correct * fix types * fix enabled prop * fix type * add embedConnectVersion to query key * add teamId to slot type * fix * add fallback to trpc query if apiv2 fails * add comment * remove logs * fix * refactor: default to isTeamEvent in query params * fix: only return v2 slots once fetch is success * fix last e2e failing test * add feature flag * add migration sql * Update packages/prisma/migrations/20250512153630_add_use_api_v2_for_team_slots_feature_flag/migration.sql --------- Co-authored-by: hbjORbj <[email protected]> Co-authored-by: Somay Chauhan <[email protected]> Co-authored-by: supalarry <[email protected]> Co-authored-by: Keith Williams <[email protected]>
150 lines
4.4 KiB
TypeScript
150 lines
4.4 KiB
TypeScript
import { shallow } from "zustand/shallow";
|
|
|
|
import { useSchedule } from "@calcom/features/schedules/lib/use-schedule/useSchedule";
|
|
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
|
import { trpc } from "@calcom/trpc/react";
|
|
|
|
import { useBookerTime } from "../components/hooks/useBookerTime";
|
|
import { useBookerStore } from "../store";
|
|
|
|
export type useEventReturnType = ReturnType<typeof useEvent>;
|
|
export type useScheduleForEventReturnType = ReturnType<typeof useScheduleForEvent>;
|
|
|
|
/**
|
|
* Wrapper hook around the trpc query that fetches
|
|
* the event currently viewed in the booker. It will get
|
|
* the current event slug and username from the booker store.
|
|
*
|
|
* Using this hook means you only need to use one hook, instead
|
|
* of combining multiple conditional hooks.
|
|
*/
|
|
export const useEvent = (props?: { fromRedirectOfNonOrgLink?: boolean; disabled?: boolean }) => {
|
|
const [username, eventSlug, isTeamEvent, org] = useBookerStore(
|
|
(state) => [state.username, state.eventSlug, state.isTeamEvent, state.org],
|
|
shallow
|
|
);
|
|
|
|
const event = trpc.viewer.public.event.useQuery(
|
|
{
|
|
username: username ?? "",
|
|
eventSlug: eventSlug ?? "",
|
|
isTeamEvent,
|
|
org: org ?? null,
|
|
fromRedirectOfNonOrgLink: props?.fromRedirectOfNonOrgLink,
|
|
},
|
|
{
|
|
refetchOnWindowFocus: false,
|
|
enabled: !props?.disabled && Boolean(username) && Boolean(eventSlug),
|
|
}
|
|
);
|
|
|
|
return {
|
|
data: event?.data,
|
|
isSuccess: event?.isSuccess,
|
|
isError: event?.isError,
|
|
isPending: event?.isPending,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Gets schedule for the current event and current month.
|
|
* Gets all values right away and not the store because it increases network timing, only for the first render.
|
|
* We can read from the store if we want to get the latest values.
|
|
*
|
|
* Using this hook means you only need to use one hook, instead
|
|
* of combining multiple conditional hooks.
|
|
*
|
|
* The prefetchNextMonth argument can be used to prefetch two months at once,
|
|
* useful when the user is viewing dates near the end of the month,
|
|
* this way the multi day view will show data of both months.
|
|
*/
|
|
export const useScheduleForEvent = ({
|
|
prefetchNextMonth,
|
|
username,
|
|
eventSlug,
|
|
eventId,
|
|
month,
|
|
duration,
|
|
monthCount,
|
|
dayCount,
|
|
selectedDate,
|
|
orgSlug,
|
|
teamMemberEmail,
|
|
isTeamEvent,
|
|
useApiV2 = true,
|
|
}: {
|
|
prefetchNextMonth?: boolean;
|
|
username?: string | null;
|
|
eventSlug?: string | null;
|
|
eventId?: number | null;
|
|
month?: string | null;
|
|
duration?: number | null;
|
|
monthCount?: number;
|
|
dayCount?: number | null;
|
|
selectedDate?: string | null;
|
|
orgSlug?: string;
|
|
teamMemberEmail?: string | null;
|
|
fromRedirectOfNonOrgLink?: boolean;
|
|
isTeamEvent?: boolean;
|
|
useApiV2?: boolean;
|
|
} = {}) => {
|
|
const { timezone } = useBookerTime();
|
|
const [usernameFromStore, eventSlugFromStore, monthFromStore, durationFromStore] = useBookerStore(
|
|
(state) => [state.username, state.eventSlug, state.month, state.selectedDuration],
|
|
shallow
|
|
);
|
|
|
|
const searchParams = useCompatSearchParams();
|
|
const rescheduleUid = searchParams?.get("rescheduleUid");
|
|
|
|
const scheduleUsingApiV2 = useSchedule({
|
|
username: usernameFromStore ?? username,
|
|
eventSlug: eventSlugFromStore ?? eventSlug,
|
|
eventId,
|
|
timezone,
|
|
selectedDate,
|
|
prefetchNextMonth,
|
|
monthCount,
|
|
dayCount,
|
|
rescheduleUid,
|
|
month: monthFromStore ?? month,
|
|
duration: durationFromStore ?? duration,
|
|
isTeamEvent,
|
|
orgSlug,
|
|
teamMemberEmail,
|
|
useApiV2: useApiV2,
|
|
});
|
|
|
|
const scheduleNotUsingApiV2 = useSchedule({
|
|
username: usernameFromStore ?? username,
|
|
eventSlug: eventSlugFromStore ?? eventSlug,
|
|
eventId,
|
|
timezone,
|
|
selectedDate,
|
|
prefetchNextMonth,
|
|
monthCount,
|
|
dayCount,
|
|
rescheduleUid,
|
|
month: monthFromStore ?? month,
|
|
duration: durationFromStore ?? duration,
|
|
isTeamEvent,
|
|
orgSlug,
|
|
teamMemberEmail,
|
|
useApiV2: false,
|
|
// only run this query if the one using Api v2 fails
|
|
// Network error does not trigger `isError` flag, so we are instead using `failureReason` here
|
|
enabled: isTeamEvent && !!scheduleUsingApiV2?.failureReason,
|
|
});
|
|
|
|
const schedule = scheduleUsingApiV2?.isSuccess ? scheduleUsingApiV2 : scheduleNotUsingApiV2;
|
|
|
|
return {
|
|
data: schedule?.data,
|
|
isPending: schedule?.isPending,
|
|
isError: schedule?.isError,
|
|
isSuccess: schedule?.isSuccess,
|
|
isLoading: schedule?.isLoading,
|
|
invalidate: schedule?.invalidate,
|
|
};
|
|
};
|