/** * @license * Copyright 2025 Vybestack LLC * SPDX-License-Identifier: Apache-2.0 */ import type { ReactNode } from 'react'; import type React from 'react'; import { createContext, useContext, useReducer, useMemo } from 'react'; import type { SessionState, SessionAction, } from '../reducers/sessionReducer.js'; import { sessionReducer } from '../reducers/sessionReducer.js'; // Context type with strict typing for [state, dispatch] type SessionStateContextType = [SessionState, React.Dispatch]; // Create the context const SessionStateContext = createContext( undefined, ); // Provider props interface SessionStateProviderProps { children: ReactNode; initialState: SessionState; } // Provider component export const SessionStateProvider: React.FC = ({ children, initialState, }) => { const [state, dispatch] = useReducer(sessionReducer, initialState); // Memoize the context value to prevent unnecessary re-renders const contextValue = useMemo( () => [state, dispatch], [state, dispatch], ); return ( {children} ); }; // Hook to use the session state context export const useSessionState = (): SessionStateContextType => { const context = useContext(SessionStateContext); if (!context) { throw new Error('useSessionState must be used within SessionStateProvider'); } return context; };