19 lines
685 B
TypeScript
19 lines
685 B
TypeScript
'use client';
|
|
import { useInitialState } from './hooks';
|
|
import { createOrderStore } from './store';
|
|
import { createContext, type PropsWithChildren, useRef } from 'react';
|
|
|
|
export type OrderStoreApi = ReturnType<typeof createOrderStore>;
|
|
|
|
export const OrderStoreContext = createContext<OrderStoreApi | undefined>(undefined);
|
|
|
|
export function OrderStoreProvider({ children }: Readonly<PropsWithChildren>) {
|
|
const storeRef = useRef<null | OrderStoreApi>(null);
|
|
const initialState = useInitialState();
|
|
if (storeRef.current === null) {
|
|
storeRef.current = createOrderStore(initialState);
|
|
}
|
|
|
|
return <OrderStoreContext value={storeRef.current}>{children}</OrderStoreContext>;
|
|
}
|