* move booking actions into a subfolder * fix logic to render booking actions dropdown * skip dropdown portal when rendered from detail sheet * fix type error
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, useContext, useRef, type ReactNode } from "react";
|
|
import { useStore } from "zustand";
|
|
import type { StoreApi } from "zustand";
|
|
|
|
import { createBookingActionsStore, type BookingActionsStore } from "./store";
|
|
|
|
export const BookingActionsStoreContext = createContext<StoreApi<BookingActionsStore> | null>(null);
|
|
|
|
export interface BookingActionsStoreProviderProps {
|
|
children: ReactNode;
|
|
}
|
|
|
|
export const BookingActionsStoreProvider = ({ children }: BookingActionsStoreProviderProps) => {
|
|
const storeRef = useRef<StoreApi<BookingActionsStore>>();
|
|
if (!storeRef.current) {
|
|
storeRef.current = createBookingActionsStore();
|
|
}
|
|
|
|
return (
|
|
<BookingActionsStoreContext.Provider value={storeRef.current}>
|
|
{children}
|
|
</BookingActionsStoreContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useBookingActionsStoreContext = <T,>(
|
|
selector: (store: BookingActionsStore) => T,
|
|
equalityFn?: (a: T, b: T) => boolean
|
|
): T => {
|
|
const bookingActionsStoreContext = useContext(BookingActionsStoreContext);
|
|
|
|
if (!bookingActionsStoreContext) {
|
|
throw new Error("useBookingActionsStoreContext must be used within BookingActionsStoreProvider");
|
|
}
|
|
|
|
return useStore(bookingActionsStoreContext, selector, equalityFn);
|
|
};
|