Blame · Line-by-line history
RootNavigator.tsx
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| c53cf00 | 1 | import React, { createContext, useState, useEffect } from 'react'; |
| 2 | import { NavigationContainer } from '@react-navigation/native'; | |
| 3 | import { createNativeStackNavigator } from '@react-navigation/native-stack'; | |
| 4 | import { View, ActivityIndicator } from 'react-native'; | |
| 5 | import { colors } from '../theme/colors'; | |
| 6 | import { AuthScreen } from '../screens/AuthScreen'; | |
| 7 | import { MainTabNavigator } from './MainTabNavigator'; | |
| 8 | import { useAuth } from '../hooks/useAuth'; | |
| 9 | import { type User } from '../api/client'; | |
| 10 | ||
| 11 | // ─── Auth context — shared across the whole app ─────────────────────────────── | |
| 12 | ||
| 13 | export interface AuthContextValue { | |
| 14 | user: User | null; | |
| 15 | token: string | null; | |
| 16 | login: (tokenOrUsername: string, password?: string, host?: string) => Promise<void>; | |
| 17 | logout: () => Promise<void>; | |
| 18 | } | |
| 19 | ||
| 20 | export const AuthContext = createContext<AuthContextValue>({ | |
| 21 | user: null, | |
| 22 | token: null, | |
| 23 | login: async () => {}, | |
| 24 | logout: async () => {}, | |
| 25 | }); | |
| 26 | ||
| 27 | // ─── Root param list ────────────────────────────────────────────────────────── | |
| 28 | ||
| 29 | type RootStackParamList = { | |
| 30 | Auth: undefined; | |
| 31 | Main: undefined; | |
| 32 | }; | |
| 33 | ||
| 34 | const Stack = createNativeStackNavigator<RootStackParamList>(); | |
| 35 | ||
| 36 | // ─── Navigator ──────────────────────────────────────────────────────────────── | |
| 37 | ||
| 38 | export function RootNavigator() { | |
| 39 | const auth = useAuth(); | |
| 40 | ||
| 41 | const contextValue: AuthContextValue = { | |
| 42 | user: auth.user, | |
| 43 | token: auth.token, | |
| 44 | login: auth.login, | |
| 45 | logout: auth.logout, | |
| 46 | }; | |
| 47 | ||
| 48 | if (auth.loading) { | |
| 49 | return ( | |
| 50 | <View style={{ flex: 1, backgroundColor: colors.bg, alignItems: 'center', justifyContent: 'center' }}> | |
| 51 | <ActivityIndicator size="large" color={colors.accent} /> | |
| 52 | </View> | |
| 53 | ); | |
| 54 | } | |
| 55 | ||
| 56 | return ( | |
| 57 | <AuthContext.Provider value={contextValue}> | |
| 58 | <NavigationContainer | |
| 59 | theme={{ | |
| 60 | dark: true, | |
| 61 | colors: { | |
| 62 | primary: colors.accent, | |
| 63 | background: colors.bg, | |
| 64 | card: colors.bgSecondary, | |
| 65 | text: colors.text, | |
| 66 | border: colors.border, | |
| 67 | notification: colors.accent, | |
| 68 | }, | |
| 69 | }} | |
| 70 | > | |
| 71 | <Stack.Navigator screenOptions={{ headerShown: false }}> | |
| 72 | {auth.isAuthenticated ? ( | |
| 73 | <Stack.Screen name="Main" component={MainTabNavigator} /> | |
| 74 | ) : ( | |
| 75 | <Stack.Screen name="Auth" component={AuthScreen} /> | |
| 76 | )} | |
| 77 | </Stack.Navigator> | |
| 78 | </NavigationContainer> | |
| 79 | </AuthContext.Provider> | |
| 80 | ); | |
| 81 | } |