"use client"; import { createContext, FC, PropsWithChildren, Reducer, startTransition, useContext, useReducer, } from "react"; export function generateProvider({ defaultValue, reducer, name, }: { defaultValue: Shape; reducer: Reducer; name: string; }) { const Context = createContext(defaultValue); const DispatchContext = createContext<(action: Action) => void>(() => {}); const useShapeContext = () => { return useContext(Context); }; const useShapeDispatch = () => { return useContext(DispatchContext); }; const Provider: FC> = ({ children, generateInitialValue, }) => { const [state, dispatch] = useReducer( reducer, undefined, generateInitialValue ); const transitionDispatch = (action: Action) => { startTransition(() => { dispatch(action); }); }; return ( {children} ); }; Provider.displayName = `${name}Provider`; return { useContext: useShapeContext, useDispatch: useShapeDispatch, Provider, }; }