Files
cal.diy/apps/web/modules/bookings/components/BookingCalendarContainer.tsx
T
Eunjae Lee 3986d61f40 feat(bookings): improve bookings redesign (#25251)
* use booking.uid instead of booking.id for url param

* show timezone on calendar

* fix type

* restore horizontal tab and remove header and subtitle

* clean up sidebar items

* fix event propagation from attendees

* fetch all statuses except for cancelled on calendar view

* clean up styles of the badges on BookingListItem

* fix useMediaQuery

* add close button to the header

* add assignment reason to the details sheet

* use separator row

* use ToggleGroup for the top bookings tab

* move ViewToggleButton

* resize the action button

* remove wrong prop

* fix type error

* fix type error

* hide view toggle button on mobile (and fix the breakpoint)

* remove unused e2e tests

* fix e2e tests

* hide toggle button when feature flag is off

* update skeleton

* improve attendees on booking list item and slide over

* improve attendee dropdown

* fix type error

* move query to containers

* select attendee email

* infinite fetching for calendar view

* update styles

* fix compatibility

* fix: add backward compatibility for status field in getAllUserBookings

* increase calendar height

* fix type error

* support Member filter only for admin / owners

* add debug log (TEMP)

* add event border color

* show Reject / Accept buttons on BookingDetailsSheet

* move description section to the top

* update When section

* update style of Who section

* add CancelBookingDialog WIP

* fix CancelBookingDialog

* increase clickable area

* add schedule info section WIP

* fix flaky reject button

* fixing reschedule info WIP

* add fromReschedule index to Booking

* improve rescheduled information

* improve reassignment

* fix type error

* fix unit test

* respect user's weekStart value on the booking calendar view

* update debug log

* improve payment section

* clean up

* fix log message

* reposition filters on list view

* fix bookings controller api2 e2e test

* clean up file by extracting logic into custom hooks

* rename files

* merge BookingCalendar into its container

* extract logic into separate hook files

* remove redundant logic

* rearrange items on calendar view

* add WeekPicker

* extract filter button

* responsive header on list view

* horizontal scroll for ToggleGroup WIP

* fix type error

* fix cancelling recurring event

* address feedback

* fix e2e tests

* fix unit test

* fix e2e tests

* make hover style more visible for ToggleGroup

* fix margin on CancelBookingDialog

* update styles on the slide over (mostly font weight)

* update style of CancelBookingDialog

* update styles

* update margin top for the header

* refactor getBookingDetails handler

* fix gap in who section

* auto-filter the current user on the calendar view

* calculate calendar height considering top banners

* improve booking details sheet interaction without overlay

* update calendar event styles

* update reject dialog style

* put uid first in the query params

* fix class name

* memoize functions in useMediaQuery

* query attendee with id instead of email

* update margins

* replace TRPCError with ErrorWithCode

* move calculation outside loop

* remove dead code
2025-12-10 13:40:04 +00:00

224 lines
7.5 KiB
TypeScript

"use client";
import { useReactTable, getCoreRowModel, getSortedRowModel } from "@tanstack/react-table";
import React, { useMemo, useEffect } from "react";
import dayjs from "@calcom/dayjs";
import { DataTableFilters } from "@calcom/features/data-table";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc/react";
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
import { Alert } from "@calcom/ui/components/alert";
import { Button } from "@calcom/ui/components/button";
import { ButtonGroup } from "@calcom/ui/components/buttonGroup";
import { Icon } from "@calcom/ui/components/icon";
import { useBookingCalendarData } from "~/bookings/hooks/useBookingCalendarData";
import { useBookingFilters } from "~/bookings/hooks/useBookingFilters";
import { useCalendarAllowedFilters } from "~/bookings/hooks/useCalendarAllowedFilters";
import { useCurrentWeekStart } from "~/bookings/hooks/useCurrentWeekStart";
import { useFacetedUniqueValues } from "~/bookings/hooks/useFacetedUniqueValues";
import { buildFilterColumns, getFilterColumnVisibility } from "../columns/filterColumns";
import { getWeekStart } from "../lib/weekUtils";
import { BookingDetailsSheetStoreProvider } from "../store/bookingDetailsSheetStore";
import type { RowData, BookingListingStatus, BookingsGetOutput } from "../types";
import { BookingCalendarView } from "./BookingCalendarView";
import { BookingDetailsSheet } from "./BookingDetailsSheet";
import { ViewToggleButton } from "./ViewToggleButton";
import { WeekPicker } from "./WeekPicker";
// For calendar view, fetch all statuses except cancelled
const STATUSES: BookingListingStatus[] = ["upcoming", "unconfirmed", "recurring", "past"];
interface BookingCalendarContainerProps {
status: BookingListingStatus;
permissions: {
canReadOthersBookings: boolean;
};
}
interface BookingCalendarInnerProps extends BookingCalendarContainerProps {
data?: {
bookings: BookingsGetOutput["bookings"];
recurringInfo: BookingsGetOutput["recurringInfo"];
totalCount: BookingsGetOutput["totalCount"];
};
allowedFilterIds: string[];
isPending: boolean;
hasError: boolean;
errorMessage?: string;
}
function BookingCalendarInner({
status,
permissions,
data,
allowedFilterIds,
hasError,
errorMessage,
}: BookingCalendarInnerProps) {
const { t } = useLocale();
const user = useMeQuery().data;
const { currentWeekStart, setCurrentWeekStart, userWeekStart } = useCurrentWeekStart();
const goToPreviousWeek = () => {
setCurrentWeekStart(currentWeekStart.subtract(1, "week"));
};
const goToNextWeek = () => {
setCurrentWeekStart(currentWeekStart.add(1, "week"));
};
const goToToday = () => {
setCurrentWeekStart(getWeekStart(dayjs(), userWeekStart));
};
const ErrorView = errorMessage ? (
<Alert severity="error" title={t("something_went_wrong")} message={errorMessage} />
) : undefined;
const columns = useMemo(() => {
return buildFilterColumns({ t, permissions, status }).filter((column) =>
allowedFilterIds.includes(column.id || "")
);
}, [allowedFilterIds, t, permissions, status]);
const getFacetedUniqueValues = useFacetedUniqueValues();
const rowData = useBookingCalendarData({ data, status });
const table = useReactTable<RowData>({
data: rowData,
columns,
initialState: {
columnVisibility: getFilterColumnVisibility(),
},
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
getFacetedUniqueValues,
});
// Extract bookings from table data
const bookings = useMemo(() => {
return rowData
.filter((row): row is Extract<RowData, { type: "data" }> => row.type === "data")
.map((row) => row.booking);
}, [rowData]);
return (
<>
<div className="mb-4 flex items-center justify-between">
<div className="flex items-center gap-2">
<WeekPicker
currentWeekStart={currentWeekStart}
userWeekStart={userWeekStart}
onDateChange={setCurrentWeekStart}
/>
{allowedFilterIds.length > 0 && <DataTableFilters.FilterBar table={table} />}
</div>
<div className="flex items-center gap-2">
<Button color="secondary" onClick={goToToday} className="capitalize">
{t("today")}
</Button>
<ButtonGroup combined>
<Button color="secondary" onClick={goToPreviousWeek}>
<span className="sr-only">{t("view_previous_week")}</span>
<Icon name="chevron-left" className="h-4 w-4" />
</Button>
<Button color="secondary" onClick={goToNextWeek}>
<span className="sr-only">{t("view_next_week")}</span>
<Icon name="chevron-right" className="h-4 w-4" />
</Button>
</ButtonGroup>
<ViewToggleButton />
</div>
</div>
{hasError && ErrorView ? (
ErrorView
) : (
<BookingCalendarView
bookings={bookings}
currentWeekStart={currentWeekStart}
onWeekStartChange={setCurrentWeekStart}
/>
)}
<BookingDetailsSheet
userTimeZone={user?.timeZone}
userTimeFormat={user?.timeFormat === null ? undefined : user?.timeFormat}
userId={user?.id}
userEmail={user?.email}
/>
</>
);
}
export function BookingCalendarContainer(props: BookingCalendarContainerProps) {
const { canReadOthersBookings } = props.permissions;
const { userIds } = useBookingFilters();
const { currentWeekStart } = useCurrentWeekStart();
const allowedFilterIds = useCalendarAllowedFilters({
canReadOthersBookings,
});
const query = trpc.viewer.bookings.get.useInfiniteQuery(
{
limit: 100, // Use max limit for calendar view
filters: {
statuses: STATUSES,
userIds,
// Always fetch only the current week for calendar view
afterStartDate: currentWeekStart.startOf("day").toISOString(),
beforeEndDate: currentWeekStart.add(6, "day").endOf("day").toISOString(),
},
},
{
getNextPageParam: (lastPage) => lastPage.nextCursor,
staleTime: 5 * 60 * 1000, // 5 minutes - data is considered fresh
gcTime: 30 * 60 * 1000, // 30 minutes - cache retention time
}
);
// Automatically fetch all pages until no more data
const { hasNextPage, isFetchingNextPage, fetchNextPage } = query;
useEffect(() => {
if (hasNextPage && !isFetchingNextPage) {
fetchNextPage();
}
}, [hasNextPage, isFetchingNextPage, fetchNextPage]);
// Flatten all pages into a single data object
const data = useMemo(() => {
if (!query.data?.pages) return undefined;
// Combine all bookings from all pages
const allBookings = query.data.pages.flatMap((page) => page.bookings);
// recurringInfo is the same across all pages (queried globally by user, not per page)
const recurringInfo = query.data.pages[0]?.recurringInfo ?? [];
// totalCount is the same across all pages
const totalCount = query.data.pages[0]?.totalCount ?? 0;
return { bookings: allBookings, recurringInfo, totalCount };
}, [query.data?.pages]);
const bookings = useMemo(() => data?.bookings ?? [], [data?.bookings]);
return (
<BookingDetailsSheetStoreProvider bookings={bookings}>
<BookingCalendarInner
{...props}
data={data}
allowedFilterIds={allowedFilterIds}
isPending={query.isPending}
hasError={!!query.error}
errorMessage={query.error?.message}
/>
</BookingDetailsSheetStoreProvider>
);
}