mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
* perf: Slim down loggedInViewer some more * Move locationOptions to apps router * Moved calVideo related handlers to separate router * Moved calendar related handlers to separate router * Rename * Moved deleteCredential to new router * Moved updateProfile to me router * fix unit test * fix e2e test * tweak * missing --------- Co-authored-by: Anik Dhabal Babu <[email protected]> Co-authored-by: unknown <[email protected]>
77 lines
2.4 KiB
TypeScript
77 lines
2.4 KiB
TypeScript
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>;
|
|
type UseCalendarsProps = {
|
|
hasSession: boolean;
|
|
};
|
|
export type ToggledConnectedCalendars = Set<{
|
|
credentialId: number;
|
|
externalId: string;
|
|
}>;
|
|
|
|
export const useCalendars = ({ hasSession }: UseCalendarsProps) => {
|
|
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.useUtils();
|
|
|
|
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: hasSession && set.size > 0 && switchEnabled,
|
|
}
|
|
);
|
|
|
|
useEffect(
|
|
function refactorMeWithoutEffect() {
|
|
if (!isError) return;
|
|
clearSet();
|
|
},
|
|
[isError]
|
|
);
|
|
|
|
const { data, isPending } = trpc.viewer.calendars.connectedCalendars.useQuery(undefined, {
|
|
enabled: !!calendarSettingsOverlay || Boolean(searchParams?.get("overlayCalendar")),
|
|
});
|
|
|
|
return {
|
|
overlayBusyDates,
|
|
isOverlayCalendarEnabled: switchEnabled,
|
|
connectedCalendars: data?.connectedCalendars || [],
|
|
loadingConnectedCalendar: isPending,
|
|
onToggleCalendar: (data: ToggledConnectedCalendars) => {
|
|
utils.viewer.availability.calendarOverlay.reset();
|
|
},
|
|
};
|
|
};
|