52d5261b51
* display UTM parameters * persist view (list | calendar) in localStorage * move to next page on clicking "next" of last item in the page improve navigation ^ Conflicts: ^ apps/web/modules/bookings/components/BookingCalendarContainer.tsx * fix navigation on calendar view * Delete apps/web/modules/bookings/NAVIGATION_IMPLEMENTATION.md * avoid page size being 0 * check feature flag on user level * use map to improve useBookingListData * address feedback * feat(bookings): add smart navigation for calendar view - Add sort option to tRPC bookings.get schema and handler - Create NAVIGATION_PROBE_WINDOW_MONTHS constant (3 months) - Create useNearestFutureBooking hook to find nearest future booking - Create useNearestPastBooking hook to find nearest past booking - Update useCalendarNavigationCapabilities to use probe results - Update BookingCalendarContainer to wire up probe hooks This enables the booking details sheet to: - Disable next/prev buttons when no bookings exist in that direction - Jump directly to the week containing the nearest booking Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * fix type error * feat: add booking selection state when using slideover (#25637) * implement booking selection when using slideover * remove pixel shift * fix * auto scroll to selected event on calendar * make DateValues header sticky when scrolling --------- Co-authored-by: Eunjae Lee <hey@eunjae.dev> * prefetch previous / next weeks on calendar view * clean up classes * handle "fetched & but no data" situation more correctly in useCalendarAutoSelector --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com>
128 lines
4.6 KiB
TypeScript
128 lines
4.6 KiB
TypeScript
import { useMemo } from "react";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import type {
|
|
RowData,
|
|
BookingRowData,
|
|
BookingListingStatus,
|
|
BookingOutput,
|
|
BookingsGetOutput,
|
|
} from "../types";
|
|
|
|
/**
|
|
* Transform raw bookings into final data structure with separators
|
|
* - Deduplicates recurring bookings for recurring/unconfirmed/cancelled tabs
|
|
* - For "upcoming" status, organizes into "Today" and "Next" sections
|
|
*/
|
|
export function useBookingListData({
|
|
data,
|
|
status,
|
|
userTimeZone,
|
|
}: {
|
|
data?: BookingsGetOutput;
|
|
status: BookingListingStatus;
|
|
userTimeZone?: string;
|
|
}) {
|
|
const { t } = useLocale();
|
|
|
|
// Build a Map for recurringInfo lookups
|
|
const recurringInfoMap = useMemo(() => {
|
|
const map = new Map<string, NonNullable<typeof data>["recurringInfo"][number]>();
|
|
for (const info of data?.recurringInfo ?? []) {
|
|
if (info.recurringEventId) {
|
|
map.set(info.recurringEventId, info);
|
|
}
|
|
}
|
|
return map;
|
|
}, [data?.recurringInfo]);
|
|
|
|
/**
|
|
* Transform raw bookings into flat list (excluding today's bookings for "upcoming" status)
|
|
* - Deduplicates recurring bookings for recurring/unconfirmed/cancelled tabs
|
|
* - For "upcoming" status, filters out today's bookings (they're shown in separate "Today" section)
|
|
*/
|
|
const flatData = useMemo<BookingRowData[]>(() => {
|
|
const todayDateString = dayjs().tz(userTimeZone).format("YYYY-MM-DD");
|
|
|
|
// For recurring/unconfirmed/cancelled tabs: track recurring series to show only one representative booking per series
|
|
// Key: recurringEventId, Value: array of all bookings in that series
|
|
const shownBookings: Record<string, BookingOutput[]> = {};
|
|
|
|
const filterBookings = (booking: BookingOutput) => {
|
|
// Deduplicate recurring bookings for specific status tabs
|
|
// This ensures we show only ONE booking per recurring series instead of all occurrences
|
|
if (status === "recurring" || status == "unconfirmed" || status === "cancelled") {
|
|
// Non-recurring bookings are always shown
|
|
if (!booking.recurringEventId) {
|
|
return true;
|
|
}
|
|
|
|
// If we've already encountered this recurring series
|
|
if (
|
|
shownBookings[booking.recurringEventId] !== undefined &&
|
|
shownBookings[booking.recurringEventId].length > 0
|
|
) {
|
|
// Store this occurrence but DON'T display it (return false to filter out)
|
|
shownBookings[booking.recurringEventId].push(booking);
|
|
return false;
|
|
}
|
|
|
|
// First occurrence of this recurring series - show it and start tracking
|
|
shownBookings[booking.recurringEventId] = [booking];
|
|
} else if (status === "upcoming") {
|
|
// For "upcoming" tab, exclude today's bookings (they're shown separately in the "Today" section)
|
|
return dayjs(booking.startTime).tz(userTimeZone).format("YYYY-MM-DD") !== todayDateString;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
return (
|
|
data?.bookings.filter(filterBookings).map((booking) => ({
|
|
type: "data" as const,
|
|
booking,
|
|
recurringInfo: booking.recurringEventId ? recurringInfoMap.get(booking.recurringEventId) : undefined,
|
|
isToday: false,
|
|
})) || []
|
|
);
|
|
}, [data?.bookings, recurringInfoMap, status, userTimeZone]);
|
|
|
|
// Extract today's bookings for the "Today" section (only used in "upcoming" status)
|
|
const bookingsToday = useMemo<BookingRowData[]>(() => {
|
|
const todayDateString = dayjs().tz(userTimeZone).format("YYYY-MM-DD");
|
|
|
|
return (data?.bookings ?? [])
|
|
.filter(
|
|
(booking: BookingOutput) =>
|
|
dayjs(booking.startTime).tz(userTimeZone).format("YYYY-MM-DD") === todayDateString
|
|
)
|
|
.map((booking) => ({
|
|
type: "data" as const,
|
|
booking,
|
|
recurringInfo: booking.recurringEventId ? recurringInfoMap.get(booking.recurringEventId) : undefined,
|
|
isToday: true,
|
|
}));
|
|
}, [data?.bookings, recurringInfoMap, userTimeZone]);
|
|
|
|
// Combine data with section separators for "upcoming" tab
|
|
const finalData = useMemo<RowData[]>(() => {
|
|
// For other statuses, just return the flat list
|
|
if (status !== "upcoming") {
|
|
return flatData;
|
|
}
|
|
|
|
// For "upcoming" status, organize into "Today" and "Next" sections
|
|
const merged: RowData[] = [];
|
|
if (bookingsToday.length > 0) {
|
|
merged.push({ type: "separator" as const, label: t("today") }, ...bookingsToday);
|
|
}
|
|
if (flatData.length > 0) {
|
|
merged.push({ type: "separator" as const, label: t("next") }, ...flatData);
|
|
}
|
|
return merged;
|
|
}, [bookingsToday, flatData, status, t]);
|
|
|
|
return finalData;
|
|
}
|