* 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>
176 lines
5.6 KiB
TypeScript
176 lines
5.6 KiB
TypeScript
import { shallow } from "zustand/shallow";
|
|
|
|
import type { Dayjs } from "@calcom/dayjs";
|
|
import dayjs from "@calcom/dayjs";
|
|
import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider";
|
|
import type { DatePickerClassNames } from "@calcom/features/bookings/Booker/types";
|
|
import { DatePicker as DatePickerComponent } from "@calcom/features/calendars/components/DatePicker";
|
|
import { useNonEmptyScheduleDays } from "@calcom/features/schedules/lib/use-schedule/useNonEmptyScheduleDays";
|
|
import { weekdayToWeekIndex } from "@calcom/lib/dayjs";
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
import type { User } from "@calcom/prisma/client";
|
|
import type { PeriodData } from "@calcom/types/Event";
|
|
import { useSlotsViewOnSmallScreen } from "@calcom/embed-core/embed-iframe";
|
|
|
|
import type { Slots } from "@calcom/features/bookings/types";
|
|
|
|
const useMoveToNextMonthOnNoAvailability = ({
|
|
browsingDate,
|
|
nonEmptyScheduleDays,
|
|
onMonthChange,
|
|
isLoading,
|
|
}: {
|
|
browsingDate: Dayjs;
|
|
nonEmptyScheduleDays: string[];
|
|
isLoading: boolean;
|
|
onMonthChange: (date: Dayjs) => void;
|
|
}) => {
|
|
if (isLoading) {
|
|
return {
|
|
moveToNextMonthOnNoAvailability: () => {
|
|
/* return noop until ready */
|
|
},
|
|
};
|
|
}
|
|
|
|
const nonEmptyScheduleDaysInBrowsingMonth = nonEmptyScheduleDays.filter(
|
|
(date) => dayjs(date).isSame(browsingDate, "month")
|
|
);
|
|
|
|
const moveToNextMonthOnNoAvailability = () => {
|
|
const currentMonth = dayjs().startOf("month").format("YYYY-MM");
|
|
const browsingMonth = browsingDate.format("YYYY-MM");
|
|
// Not meeting the criteria to move to next month
|
|
// Has to be currentMonth and it must have all days unbookable
|
|
if (
|
|
currentMonth != browsingMonth ||
|
|
nonEmptyScheduleDaysInBrowsingMonth.length
|
|
) {
|
|
return;
|
|
}
|
|
onMonthChange(browsingDate.add(1, "month"));
|
|
};
|
|
return {
|
|
moveToNextMonthOnNoAvailability,
|
|
};
|
|
};
|
|
|
|
export const DatePicker = ({
|
|
event,
|
|
slots = {},
|
|
isLoading,
|
|
classNames,
|
|
scrollToTimeSlots,
|
|
showNoAvailabilityDialog,
|
|
onDateChange,
|
|
}: {
|
|
event: {
|
|
data?: {
|
|
subsetOfUsers: Pick<User, "weekStart">[];
|
|
periodType?: PeriodData["periodType"];
|
|
periodStartDate?: PeriodData["periodStartDate"];
|
|
periodEndDate?: PeriodData["periodEndDate"];
|
|
periodDays?: PeriodData["periodDays"];
|
|
periodCountCalendarDays?: PeriodData["periodCountCalendarDays"];
|
|
} | null;
|
|
};
|
|
slots?: Slots;
|
|
isLoading?: boolean;
|
|
classNames?: DatePickerClassNames;
|
|
scrollToTimeSlots?: () => void;
|
|
showNoAvailabilityDialog?: boolean;
|
|
onDateChange?: () => void;
|
|
}) => {
|
|
const { i18n } = useLocale();
|
|
const [month, selectedDate, layout] = useBookerStoreContext(
|
|
(state) => [state.month, state.selectedDate, state.layout],
|
|
shallow
|
|
);
|
|
|
|
const [setSelectedDate, setMonth, setDayCount] = useBookerStoreContext(
|
|
(state) => [state.setSelectedDate, state.setMonth, state.setDayCount],
|
|
shallow
|
|
);
|
|
|
|
const slotsViewOnSmallScreen = useSlotsViewOnSmallScreen();
|
|
|
|
const onMonthChange = (date: Dayjs) => {
|
|
setMonth(date.format("YYYY-MM"));
|
|
if (!slotsViewOnSmallScreen) {
|
|
setSelectedDate({ date: date.format("YYYY-MM-DD") });
|
|
}
|
|
setDayCount(null); // Whenever the month is changed, we nullify getting X days
|
|
};
|
|
|
|
const nonEmptyScheduleDays = useNonEmptyScheduleDays(slots);
|
|
const browsingDate = month ? dayjs(month) : dayjs().startOf("month");
|
|
|
|
const { moveToNextMonthOnNoAvailability } =
|
|
useMoveToNextMonthOnNoAvailability({
|
|
browsingDate,
|
|
nonEmptyScheduleDays,
|
|
onMonthChange,
|
|
isLoading: isLoading ?? true,
|
|
});
|
|
moveToNextMonthOnNoAvailability();
|
|
|
|
// Determine if this is a compact sidebar view based on layout
|
|
const isCompact = layout !== "month_view" && layout !== "mobile";
|
|
|
|
const periodData: PeriodData = {
|
|
...{
|
|
periodType: "UNLIMITED",
|
|
periodStartDate: null,
|
|
periodEndDate: null,
|
|
periodDays: null,
|
|
periodCountCalendarDays: false,
|
|
},
|
|
...(event?.data && {
|
|
periodType: event.data.periodType,
|
|
periodStartDate: event.data.periodStartDate,
|
|
periodEndDate: event.data.periodEndDate,
|
|
periodDays: event.data.periodDays,
|
|
periodCountCalendarDays: event.data.periodCountCalendarDays,
|
|
}),
|
|
};
|
|
return (
|
|
<DatePickerComponent
|
|
customClassNames={{
|
|
datePickerTitle: classNames?.datePickerTitle,
|
|
datePickerDays: classNames?.datePickerDays,
|
|
datePickersDates: classNames?.datePickerDate,
|
|
datePickerDatesActive: classNames?.datePickerDatesActive,
|
|
datePickerToggle: classNames?.datePickerToggle,
|
|
}}
|
|
className={classNames?.datePickerContainer}
|
|
isLoading={isLoading}
|
|
onChange={(date: Dayjs | null, omitUpdatingParams?: boolean) => {
|
|
const newDate = date === null ? null : date.format("YYYY-MM-DD");
|
|
const previousDate = selectedDate;
|
|
const dateChanged = newDate !== previousDate;
|
|
|
|
setSelectedDate({
|
|
date: date === null ? null : date.format("YYYY-MM-DD"),
|
|
omitUpdatingParams,
|
|
preventMonthSwitching: !isCompact, // Prevent month switching when in monthly view
|
|
});
|
|
|
|
if (dateChanged) {
|
|
onDateChange?.();
|
|
}
|
|
}}
|
|
onMonthChange={onMonthChange}
|
|
includedDates={nonEmptyScheduleDays}
|
|
locale={i18n.language}
|
|
browsingDate={month ? dayjs(month) : undefined}
|
|
selected={dayjs(selectedDate)}
|
|
weekStart={weekdayToWeekIndex(event?.data?.subsetOfUsers?.[0]?.weekStart)}
|
|
slots={slots}
|
|
scrollToTimeSlots={scrollToTimeSlots}
|
|
periodData={periodData}
|
|
isCompact={isCompact}
|
|
showNoAvailabilityDialog={showNoAvailabilityDialog}
|
|
/>
|
|
);
|
|
};
|