32 lines
849 B
TypeScript
32 lines
849 B
TypeScript
"use client";
|
|
|
|
import { createContext, useRef } from "react";
|
|
import { createAlertStore, createMenuStore, createUserStore } from "../store";
|
|
|
|
type StoreContextType = {
|
|
alertStore: ReturnType<typeof createAlertStore>;
|
|
menuStore: ReturnType<typeof createMenuStore>;
|
|
userStore: ReturnType<typeof createUserStore>;
|
|
};
|
|
|
|
export const StoreContext = createContext<StoreContextType | undefined>(
|
|
undefined,
|
|
);
|
|
|
|
export const StoreProvider = ({ children }: { children: React.ReactNode }) => {
|
|
const storeRef = useRef<StoreContextType | null>(null);
|
|
if (storeRef.current === null) {
|
|
storeRef.current = {
|
|
alertStore: createAlertStore(),
|
|
menuStore: createMenuStore(),
|
|
userStore: createUserStore(),
|
|
};
|
|
}
|
|
|
|
return (
|
|
<StoreContext.Provider value={storeRef.current}>
|
|
{children}
|
|
</StoreContext.Provider>
|
|
);
|
|
};
|