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