Files
calendar/apps/web/modules/schedules/hooks/useEvent.ts
T
Syed Ali ShahbazGitHubhackice20YashMorganDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
4c73695d3a fix: refresh slots on timezone change for booker timezone restrictions (#27491)
* fix: refresh slots on timezone change for booker timezone restrictions

* refactor: use useMemo for timezone change detection

* revert: remove unnecessary formatting changes

* feat: add timezone refresh for platform components

Add timezone change detection and slot refresh to BookerPlatformWrapper
and EventTypeCalendarViewComponent to handle restriction schedules with
useBookerTimezone enabled.

* refactor: extract timezone slot refresh logic into reusable hook

* Update packages/platform/atoms/calendar-view/EventTypeCalendarViewComponent.tsx

Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>

* Update packages/platform/atoms/calendar-view/EventTypeCalendarViewComponent.tsx

Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>

* fix

* fix: prevent unnecessary getSchedule calls when useBookerTimezone is disabled

* fix: add timezone fields to BookerEvent type

* fix: add missing properties to BookerEvent and BookerEventProfile types

* trying to fix type errors

* Add restrictionScheduleId and useBookerTimezone fields

* fix: correct import path for useBookerTime hook

Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com>

* fix: explicitly include restrictionScheduleId and useBookerTimezone in getPublicEvent return

Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com>

* chore: trigger fresh CI build

Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com>

* fix: cast event.data to BookerEvent for timezone fields access

Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com>

* refactor: address review feedback for timezone slot refresh

Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com>

* refactor: add explicit return type to event handler to ensure type propagation

Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com>

* refactor: extract useStableTimezone hook and remove dead timezone detection code

Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com>

---------

Co-authored-by: hackice20 <yashkam431@gmail.com>
Co-authored-by: Yash <116657771+hackice20@users.noreply.github.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-17 07:46:40 -03:00

137 lines
4.1 KiB
TypeScript

import { shallow } from "zustand/shallow";
import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider";
import { useSchedule } from "@calcom/web/modules/schedules/hooks/useSchedule";
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
import { trpc } from "@calcom/trpc/react";
import { useBookerTime } from "@calcom/features/bookings/Booker/hooks/useBookerTime";
import { useStableTimezone } from "@calcom/features/bookings/Booker/hooks/useStableTimezone";
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] = useBookerStoreContext(
(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 = ({
username,
eventSlug,
eventId,
month,
duration,
dayCount,
selectedDate,
orgSlug,
teamMemberEmail,
isTeamEvent,
useApiV2 = true,
bookerLayout,
restrictionSchedule,
}: {
username?: string | null;
eventSlug?: string | null;
eventId?: number | null;
month?: string | null;
duration?: number | null;
dayCount?: number | null;
selectedDate?: string | null;
orgSlug?: string;
teamMemberEmail?: string | null;
fromRedirectOfNonOrgLink?: boolean;
isTeamEvent?: boolean;
useApiV2?: boolean;
/**
* Required when prefetching is needed
*/
bookerLayout?: {
layout: string;
extraDays: number;
columnViewExtraDays: { current: number };
};
restrictionSchedule?: { id: number | null; useBookerTimezone: boolean };
}) => {
const { timezone: rawTimezone } = useBookerTime();
const [usernameFromStore, eventSlugFromStore, monthFromStore, durationFromStore] = useBookerStoreContext(
(state) => [state.username, state.eventSlug, state.month, state.selectedDuration],
shallow
);
const effectiveTimezone = useStableTimezone(rawTimezone, restrictionSchedule);
const searchParams = useCompatSearchParams();
const rescheduleUid = searchParams?.get("rescheduleUid");
const schedule = useSchedule({
username: usernameFromStore ?? username,
eventSlug: eventSlugFromStore ?? eventSlug,
eventId,
timezone: effectiveTimezone,
selectedDate,
dayCount,
rescheduleUid,
month: monthFromStore ?? month,
duration: durationFromStore ?? duration,
isTeamEvent,
orgSlug,
teamMemberEmail,
useApiV2: useApiV2,
bookerLayout,
});
return {
data: schedule?.data,
isPending: schedule?.isPending,
isError: schedule?.isError,
isSuccess: schedule?.isSuccess,
isLoading: schedule?.isLoading,
invalidate: schedule?.invalidate,
dataUpdatedAt: schedule?.dataUpdatedAt,
};
};