Files
cal.diy/apps/web/modules/bookings/hooks/useBookingCursor.ts
T
Eunjae Lee 1f102bf3b4 fix: revert bookings redesign (#25172)
* Revert "refactor: extract logic as bookingDetailsSheetStore (#25129)"

This reverts commit 7a0b8a9eba.

* Revert "feat: bookings page redesign v3 with calendar view (#24664)"

This reverts commit 89230474a5.
2025-11-14 17:24:31 +00:00

44 lines
1.2 KiB
TypeScript

import { useMemo, useCallback } from "react";
import type { RowData } from "../types";
function isDataRow(row: RowData): row is Extract<RowData, { type: "data" }> {
return row.type === "data";
}
export function useBookingCursor({
bookings,
selectedBookingId,
setSelectedBookingId,
}: {
bookings: RowData[];
selectedBookingId: number | null;
setSelectedBookingId: (bookingId: number | null) => void;
}) {
const bookingRows = useMemo(() => bookings.filter(isDataRow), [bookings]);
const currentIndex = useMemo(
() => bookingRows.findIndex((row) => selectedBookingId && row.booking.id === selectedBookingId),
[bookingRows, selectedBookingId]
);
const onPrevious = useCallback(() => {
if (currentIndex >= 1) {
setSelectedBookingId(bookingRows[currentIndex - 1].booking.id);
}
}, [bookingRows, currentIndex, setSelectedBookingId]);
const onNext = useCallback(() => {
if (currentIndex >= 0 && currentIndex < bookingRows.length - 1) {
setSelectedBookingId(bookingRows[currentIndex + 1].booking.id);
}
}, [bookingRows, currentIndex, setSelectedBookingId]);
return {
onPrevious,
onNext,
hasPrevious: currentIndex > 0,
hasNext: currentIndex < bookingRows.length - 1 && currentIndex >= 0,
};
}