'use client' import { type ReactNode, createContext, useContext, useRef } from 'react' import { useStore } from 'zustand' import { type WrStore, StoreZus } from './store' export type StoreApi = ReturnType export const StoreContext = createContext( undefined, ) export interface StoreProviderProps { children: ReactNode } export const StoreProvider = ({ children, }: StoreProviderProps) => { const storeRef = useRef(null) if (!storeRef.current) { storeRef.current = StoreZus() } return ( {children} ) } export const useWrStore = ( selector: (store: WrStore) => T, ): T => { const storeContext = useContext(StoreContext) if (!storeContext) { throw new Error(`useStore must be used within StoreProvider`) } return useStore(storeContext, selector) }