* 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>
100 lines
3.6 KiB
TypeScript
100 lines
3.6 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { shallow } from "zustand/shallow";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
import { useOverlayCalendarStore } from "@calcom/features/bookings/Booker/components/OverlayCalendar/store";
|
|
import type { ToggledConnectedCalendars } from "@calcom/features/bookings/Booker/types";
|
|
import { useTimePreferences } from "@calcom/features/bookings/lib";
|
|
|
|
import type { WrappedBookerPropsMain } from "../types";
|
|
import { useLocalSet } from "@calcom/features/bookings/Booker/hooks/useLocalSet";
|
|
|
|
export const useOverlayCalendar = ({
|
|
connectedCalendars,
|
|
overlayBusyDates,
|
|
onToggleCalendar,
|
|
}: Pick<
|
|
WrappedBookerPropsMain["calendars"],
|
|
"overlayBusyDates" | "onToggleCalendar" | "connectedCalendars"
|
|
>): {
|
|
isOpenOverlayContinueModal: boolean;
|
|
isOpenOverlaySettingsModal: boolean;
|
|
handleCloseContinueModal: (val: boolean) => void;
|
|
handleCloseSettingsModal: (val: boolean) => void;
|
|
handleToggleConnectedCalendar: (externalCalendarId: string, credentialId: number) => void;
|
|
checkIsCalendarToggled: (externalId: string, credentialId: number) => boolean;
|
|
} => {
|
|
const { set, toggleValue, hasItem } = useLocalSet<ToggledConnectedCalendars>(
|
|
"toggledConnectedCalendars",
|
|
[]
|
|
);
|
|
const [initalised, setInitalised] = useState(false);
|
|
const [continueWithProvider, setContinueWithProvider] = useOverlayCalendarStore(
|
|
(state) => [state.continueWithProviderModal, state.setContinueWithProviderModal],
|
|
shallow
|
|
);
|
|
const [calendarSettingsOverlay, setCalendarSettingsOverlay] = useOverlayCalendarStore(
|
|
(state) => [state.calendarSettingsOverlayModal, state.setCalendarSettingsOverlayModal],
|
|
shallow
|
|
);
|
|
const setOverlayBusyDates = useOverlayCalendarStore((state) => state.setOverlayBusyDates);
|
|
const { timezone } = useTimePreferences();
|
|
|
|
useEffect(() => {
|
|
if (overlayBusyDates) {
|
|
const nowDate = dayjs();
|
|
const usersTimezoneDate = nowDate.tz(timezone);
|
|
|
|
const offset = (usersTimezoneDate.utcOffset() - nowDate.utcOffset()) / 60;
|
|
|
|
const offsettedArray = overlayBusyDates.map((item) => {
|
|
return {
|
|
...item,
|
|
start: dayjs(item.start).add(offset, "hours").toDate(),
|
|
end: dayjs(item.end).add(offset, "hours").toDate(),
|
|
};
|
|
});
|
|
setOverlayBusyDates(offsettedArray);
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [overlayBusyDates]);
|
|
|
|
useEffect(() => {
|
|
if (connectedCalendars && set.size === 0 && !initalised) {
|
|
connectedCalendars.forEach((item) => {
|
|
item.calendars?.forEach((cal) => {
|
|
const id = { credentialId: item.credentialId, externalId: cal.externalId };
|
|
if (cal.primary) {
|
|
toggleValue(id);
|
|
}
|
|
});
|
|
});
|
|
setInitalised(true);
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [hasItem, set, initalised]);
|
|
|
|
const handleToggleConnectedCalendar = (externalCalendarId: string, credentialId: number): void => {
|
|
const calendarsToLoad = toggleValue({
|
|
credentialId: credentialId,
|
|
externalId: externalCalendarId,
|
|
});
|
|
setOverlayBusyDates([]);
|
|
onToggleCalendar(calendarsToLoad);
|
|
};
|
|
|
|
return {
|
|
isOpenOverlayContinueModal: continueWithProvider,
|
|
isOpenOverlaySettingsModal: calendarSettingsOverlay,
|
|
handleCloseContinueModal: (val: boolean) => setContinueWithProvider(val),
|
|
handleCloseSettingsModal: (val: boolean) => setCalendarSettingsOverlay(val),
|
|
handleToggleConnectedCalendar,
|
|
checkIsCalendarToggled: (externalId: string, credentialId: number) => {
|
|
return hasItem({
|
|
credentialId: credentialId,
|
|
externalId: externalId,
|
|
});
|
|
},
|
|
};
|
|
};
|