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