39 lines
990 B
TypeScript
39 lines
990 B
TypeScript
"use client";
|
|
|
|
import { createContext, useRef } from "react";
|
|
import {
|
|
createAlertStore,
|
|
createAttachmentStore,
|
|
createMenuStore,
|
|
createUserStore,
|
|
} from "../store";
|
|
|
|
type StoreContextType = {
|
|
alertStore: ReturnType<typeof createAlertStore>;
|
|
attachmentStore: ReturnType<typeof createAttachmentStore>;
|
|
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(),
|
|
attachmentStore: createAttachmentStore(),
|
|
menuStore: createMenuStore(),
|
|
userStore: createUserStore(),
|
|
};
|
|
}
|
|
|
|
return (
|
|
<StoreContext.Provider value={storeRef.current}>
|
|
{children}
|
|
</StoreContext.Provider>
|
|
);
|
|
};
|