Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Omar López <[email protected]> Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: Alex van Andel <[email protected]> Co-authored-by: zomars <[email protected]> Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Benny Joo <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]> Co-authored-by: Gergő Móricz <[email protected]> Co-authored-by: Keith Williams <[email protected]>
71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
import { useSession } from "next-auth/react";
|
|
import { useSearchParams } from "next/navigation";
|
|
import { useEffect } from "react";
|
|
import { shallow } from "zustand/shallow";
|
|
|
|
import { useTimePreferences } from "@calcom/features/bookings/lib";
|
|
import { localStorage } from "@calcom/lib/webstorage";
|
|
import { trpc } from "@calcom/trpc/react";
|
|
|
|
import { useBookerStore } from "../../store";
|
|
import { useOverlayCalendarStore } from "../OverlayCalendar/store";
|
|
import { useLocalSet } from "./useLocalSet";
|
|
|
|
export type UseCalendarsReturnType = ReturnType<typeof useCalendars>;
|
|
export const useCalendars = () => {
|
|
const searchParams = useSearchParams();
|
|
const selectedDate = useBookerStore((state) => state.selectedDate);
|
|
const { timezone } = useTimePreferences();
|
|
const switchEnabled =
|
|
searchParams?.get("overlayCalendar") === "true" ||
|
|
localStorage?.getItem("overlayCalendarSwitchDefault") === "true";
|
|
const { set, clearSet } = useLocalSet<{
|
|
credentialId: number;
|
|
externalId: string;
|
|
}>("toggledConnectedCalendars", []);
|
|
const utils = trpc.useContext();
|
|
const { data: session } = useSession();
|
|
|
|
const [calendarSettingsOverlay] = useOverlayCalendarStore(
|
|
(state) => [state.calendarSettingsOverlayModal, state.setCalendarSettingsOverlayModal],
|
|
shallow
|
|
);
|
|
|
|
const { data: overlayBusyDates, isError } = trpc.viewer.availability.calendarOverlay.useQuery(
|
|
{
|
|
loggedInUsersTz: timezone || "Europe/London",
|
|
dateFrom: selectedDate,
|
|
dateTo: selectedDate,
|
|
calendarsToLoad: Array.from(set).map((item) => ({
|
|
credentialId: item.credentialId,
|
|
externalId: item.externalId,
|
|
})),
|
|
},
|
|
{
|
|
enabled: !!session && set.size > 0 && switchEnabled,
|
|
}
|
|
);
|
|
|
|
useEffect(
|
|
function refactorMeWithoutEffect() {
|
|
if (!isError) return;
|
|
clearSet();
|
|
},
|
|
[isError]
|
|
);
|
|
|
|
const { data, isPending } = trpc.viewer.connectedCalendars.useQuery(undefined, {
|
|
enabled: !!calendarSettingsOverlay || Boolean(searchParams?.get("overlayCalendar")),
|
|
});
|
|
|
|
return {
|
|
overlayBusyDates,
|
|
isOverlayCalendarEnabled: switchEnabled,
|
|
connectedCalendars: data?.connectedCalendars || [],
|
|
loadingConnectedCalendar: isPending,
|
|
onToggleCalendar: () => {
|
|
utils.viewer.availability.calendarOverlay.reset();
|
|
},
|
|
};
|
|
};
|