All files / apps/host/src/navigation AppStackNavigation.tsx

75.55% Statements 34/45
91.66% Branches 11/12
26.66% Functions 4/15
84.61% Lines 33/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                              1x     1x     1x     1x     1x               5x                       1x 1x 1x 1x 1x   1x   1x 5x 5x 5x   5x 5x     5x 1x             4x 2x                                                                                     2x     1x 8x 8x     8x   8x 8x     8x 1x             7x     1x                    
import React, { lazy, Suspense, useEffect, useRef } from 'react';
import { ActivityIndicator, StatusBar, StyleSheet, useColorScheme, View } from 'react-native';
 
import { createNativeStackNavigator } from '@react-navigation/native-stack';
 
import { SCREENS } from '@repo/constants/screens';
 
import { useEnableOldUI } from '@repo/hooks';
 
import { useAuth } from '@/contexts/AuthContext';
 
import { useNavigateNotifications } from '@/hooks/notifications/useNavigateNotifications';
import { usePostLoginNotifications } from '@/hooks/notifications/usePostLoginNotifications';
 
import { AppStackParamList } from '@/types/navigation';
 
import { HomeScreenV2 } from '@/screens/HomeV2';
import { LazyLoadMaintenanceScreen } from '@/screens/LazyLoadMaintenance';
import { LazyLoadMealReservationScreen } from '@/screens/LazyLoadMealReservation';
import { LazyLoadReallocationScreen } from '@/screens/LazyLoadReallocation';
import { LazyLoadRoomBookingScreen } from '@/screens/LazyLoadRoomBooking';
import { LazyLoadTimeOffScreen } from '@/screens/LazyLoadTimeOff';
 
const PendingRequestsScreen = lazy(() =>
  import('@/screens/PendingRequests').then(module => ({ default: module.PendingRequestsScreen })),
);
const RequestDetailScreen = lazy(() =>
  import('@/screens/RequestDetail').then(module => ({ default: module.RequestDetailScreen })),
);
const NewRequestScreen = lazy(() =>
  import('@/screens/NewRequest').then(module => ({ default: module.NewRequestScreen })),
);
const ComingSoonScreen = lazy(() =>
  import('@/screens/ComingSoon').then(module => ({ default: module.ComingSoonScreen })),
);
const ProfileScreen = lazy(() =>
  import('@/screens/Profile').then(module => ({ default: module.ProfileScreen })),
);
 
import { AuthNavigator } from './AuthStackNavigation';
import BottomTab from './BottomTab';
import NavigationV2 from './NavigationV2';
 
const withSuspense = (Component: React.ComponentType<any>) => (props: any) => (
  <Suspense
    fallback={
      <View style={styles.loadingContainer} testID="loading-indicator">
        <ActivityIndicator size="large" />
      </View>
    }
  >
    <Component {...props} />
  </Suspense>
);
 
const LazyPendingRequestsScreen = withSuspense(PendingRequestsScreen);
const LazyRequestDetailScreen = withSuspense(RequestDetailScreen);
const LazyNewRequestScreen = withSuspense(NewRequestScreen);
const LazyComingSoonScreen = withSuspense(ComingSoonScreen);
const LazyProfileScreen = withSuspense(ProfileScreen);
 
const AppStack = createNativeStackNavigator<AppStackParamList>();
 
const ProtectedNavigator = () => {
  usePostLoginNotifications();
  useNavigateNotifications();
  const isDarkMode = useColorScheme() === 'dark';
 
  const { user } = useAuth();
  const { value: enableOldUI, loading: flagLoading } = useEnableOldUI(user?.email);
 
  // Show loading while flag is being fetched
  if (flagLoading) {
    return (
      <View style={styles.loadingContainer} testID="loading-indicator">
        <ActivityIndicator size="large" />
      </View>
    );
  }
 
  if (enableOldUI) {
    return (
      <View style={styles.container}>
        <StatusBar
          translucent
          backgroundColor="transparent"
          barStyle={isDarkMode ? 'light-content' : 'dark-content'}
        />
        <AppStack.Navigator>
          <AppStack.Group
            screenOptions={{
              headerShown: false,
            }}
          >
            <AppStack.Screen name={SCREENS.BOTTOM_TAB} component={BottomTab} />
            <AppStack.Screen name={SCREENS.NEW_REQUEST} component={LazyNewRequestScreen} />
            <AppStack.Screen name={SCREENS.COMING_SOON} component={LazyComingSoonScreen} />
            <AppStack.Screen name={SCREENS.REQUEST_DETAIL} component={LazyRequestDetailScreen} />
            <AppStack.Screen name={SCREENS.REALLOCATION} component={LazyLoadReallocationScreen} />
            <AppStack.Screen
              name={SCREENS.PENDING_REQUESTS}
              component={LazyPendingRequestsScreen}
            />
            <AppStack.Screen
              name={SCREENS.MEAL_RESERVATION}
              component={LazyLoadMealReservationScreen}
            />
            <AppStack.Screen name={SCREENS.TIME_OFF} component={LazyLoadTimeOffScreen} />
            <AppStack.Screen name={SCREENS.BOOK_ROOM} component={LazyLoadRoomBookingScreen} />
            <AppStack.Screen name={SCREENS.MAINTENANCE} component={LazyLoadMaintenanceScreen} />
          </AppStack.Group>
          <AppStack.Group screenOptions={{ headerShown: false }}>
            <AppStack.Screen name={SCREENS.HOME_V2} component={HomeScreenV2} />
            <AppStack.Screen
              name={SCREENS.PROFILE}
              component={LazyProfileScreen}
              options={{ gestureEnabled: false }}
            />
          </AppStack.Group>
        </AppStack.Navigator>
      </View>
    );
  }
 
  return <NavigationV2 />;
};
 
export const AppStackNavigation = () => {
  const { user, loading } = useAuth();
  const prevUserRef = useRef(user);
 
  // Derive isLoggedOut during render to avoid one-render delay from state update
  const isLoggedOut = !!prevUserRef.current && !user;
 
  useEffect(() => {
    prevUserRef.current = user;
  }, [user]);
 
  if (loading) {
    return (
      <View style={styles.loadingContainer} testID="loading-indicator">
        <ActivityIndicator size="large" />
      </View>
    );
  }
 
  return user ? <ProtectedNavigator /> : <AuthNavigator isLoggedOut={isLoggedOut} />;
};
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  loadingContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});