Files
calendar/apps/web/modules/schedules/hooks/useSlotsForDate.ts
T
Benny JooandGitHub e64fad8e34 refactor: remove 6 @calcom/trpc imports from packages/features (#27727)
* migrate booker layout selector

* move schedule hooks and types

* migrate feature opt in components

* migrate feedback dialog

* update

* migrate schedule hooks

* migrate useSegments

* update

* update import paths

* fix import paths

* mv useTimesForSchedule back to features

* fix
2026-02-07 09:05:09 -03:00

53 lines
1.5 KiB
TypeScript

import { useCallback, useEffect, useMemo, useState } from "react";
import type { Slots, Slot } from "../lib/types";
/**
* Gets slots for a specific date from the schedule cache.
* @param date Format YYYY-MM-DD
* @param scheduleCache Instance of useScheduleWithCache
*/
export const useSlotsForDate = (date: string | null, slots?: Slots) => {
const slotsForDate = useMemo(() => {
if (!date || typeof slots === "undefined") return [];
return slots[date] || [];
}, [date, slots]);
return slotsForDate;
};
export const useSlotsForAvailableDates = (dates: (string | null)[], slots?: Slots) => {
const [slotsPerDay, setSlotsPerDay] = useState<{ date: string | null; slots: Slots[string] }[]>([]);
const toggleConfirmButton = useCallback((selectedSlot: Slot) => {
setSlotsPerDay((prevSlotsPerDay) =>
prevSlotsPerDay.map(({ date, slots }) => ({
date,
slots: slots.map((slot) => ({
...slot,
showConfirmButton: slot.time === selectedSlot.time ? !selectedSlot?.showConfirmButton : false,
})),
}))
);
}, []);
useEffect(() => {
if (slots === undefined) {
setSlotsPerDay([]);
return;
}
const updatedSlots = dates
.filter((date) => date !== null)
.map((date) => ({
slots: slots[`${date}`] || [],
date,
}));
setSlotsPerDay(updatedSlots);
}, [JSON.stringify(dates), JSON.stringify(slots)]);
return { slotsPerDay, setSlotsPerDay, toggleConfirmButton } as const;
};