| 1 | import React, { |
| 2 | createContext, |
| 3 | useContext, |
| 4 | useReducer, |
| 5 | useCallback, |
| 6 | ReactNode, |
| 7 | } from "react"; |
| 8 | |
| 9 | /** |
| 10 | * Navigation screens for the UI |
| 11 | * Only one screen can be active at a time |
| 12 | */ |
| 13 | export type NavigationScreen = |
| 14 | | "chat" // Normal chat interface |
| 15 | | "config" // Config selector (includes organization switching) |
| 16 | | "model" // Model selector |
| 17 | | "free-trial" // Free trial transition UI |
| 18 | | "login" // Login prompt |
| 19 | | "mcp" // MCP selector |
| 20 | | "session"; // Session selector |
| 21 | |
| 22 | interface NavigationState { |
| 23 | currentScreen: NavigationScreen; |
| 24 | // Screen-specific data (e.g., login prompt details) |
| 25 | screenData?: any; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Navigation actions |
| 30 | */ |
| 31 | type NavigationAction = |
| 32 | | { type: "NAVIGATE_TO"; screen: NavigationScreen; data?: any } |
| 33 | | { type: "CLOSE_SCREEN" }; |
| 34 | |
| 35 | /** |
| 36 | * Navigation reducer - handles all state transitions deterministically |
| 37 | */ |
| 38 | function navigationReducer( |
| 39 | state: NavigationState, |
| 40 | action: NavigationAction, |
| 41 | ): NavigationState { |
| 42 | switch (action.type) { |
| 43 | case "NAVIGATE_TO": |
| 44 | return { |
| 45 | currentScreen: action.screen, |
| 46 | screenData: action.data ?? null, |
| 47 | }; |
| 48 | |
| 49 | case "CLOSE_SCREEN": |
| 50 | return { |
| 51 | currentScreen: "chat", |
| 52 | screenData: null, |
| 53 | }; |
| 54 | |
| 55 | default: |
| 56 | return state; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | interface NavigationContextValue { |
| 61 | // Current navigation state |
| 62 | state: NavigationState; |
| 63 | |
| 64 | // Navigation methods |
| 65 | navigateTo: (screen: NavigationScreen, data?: any) => void; |
| 66 | closeCurrentScreen: () => void; |
| 67 | |
| 68 | // Check if a specific screen is active |
| 69 | isScreenActive: (screen: NavigationScreen) => boolean; |
| 70 | } |
| 71 | |
| 72 | const NavigationContext = createContext<NavigationContextValue | undefined>( |
| 73 | undefined, |
| 74 | ); |
| 75 | |
| 76 | export function useNavigation() { |
| 77 | const context = useContext(NavigationContext); |
| 78 | if (!context) { |
| 79 | throw new Error("useNavigation must be used within NavigationProvider"); |
| 80 | } |
| 81 | return context; |
| 82 | } |
| 83 | |
| 84 | interface NavigationProviderProps { |
| 85 | children: ReactNode; |
| 86 | } |
| 87 | |
| 88 | export function NavigationProvider({ children }: NavigationProviderProps) { |
| 89 | const [state, dispatch] = useReducer(navigationReducer, { |
| 90 | currentScreen: "chat", |
| 91 | screenData: null, |
| 92 | }); |
| 93 | |
| 94 | const navigateTo = useCallback((screen: NavigationScreen, data?: any) => { |
| 95 | dispatch({ type: "NAVIGATE_TO", screen, data }); |
| 96 | }, []); |
| 97 | |
| 98 | const closeCurrentScreen = useCallback(() => { |
| 99 | dispatch({ type: "CLOSE_SCREEN" }); |
| 100 | }, []); |
| 101 | |
| 102 | const isScreenActive = useCallback( |
| 103 | (screen: NavigationScreen): boolean => { |
| 104 | return state.currentScreen === screen; |
| 105 | }, |
| 106 | [state.currentScreen], |
| 107 | ); |
| 108 | |
| 109 | const value: NavigationContextValue = { |
| 110 | state, |
| 111 | navigateTo, |
| 112 | closeCurrentScreen, |
| 113 | isScreenActive, |
| 114 | }; |
| 115 | |
| 116 | return ( |
| 117 | <NavigationContext.Provider value={value}> |
| 118 | {children} |
| 119 | </NavigationContext.Provider> |
| 120 | ); |
| 121 | } |