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

75% Statements 21/28
75% Branches 18/24
57.14% Functions 4/7
76.92% Lines 20/26

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                                                                              1x   1x 5x 5x   5x 5x 5x   5x   5x   5x   5x 1x         5x 5x   5x           5x   5x 5x                       5x                                                                                                                           5x              
import React, { useCallback, useMemo, useRef, useState } from 'react';
import { StatusBar, View } from 'react-native';
 
import { createNativeStackNavigator } from '@react-navigation/native-stack';
 
import { HAS_DARK_PALETTE } from '@repo/ui/themes/colors';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
 
import { SCREENS } from '@repo/constants/screens';
 
import { useGetMeInfo } from '@repo/hooks';
import { useDisabledFeatures } from '@repo/hooks/useFlags';
 
import { ChatbotFab } from '@/components/ChatbotFab';
 
import { useDeepLinkContext } from '@/contexts/DeepLinkContext';
 
import { useNavigateNotifications } from '@/hooks/notifications/useNavigateNotifications';
import { usePostLoginNotifications } from '@/hooks/notifications/usePostLoginNotifications';
 
import type { AppStackParamList } from '@/types/navigation';
import { RequestCategory } from '@/types/request';
import { UserInfoBase, UserRole } from '@/types/user';
 
import { ChatbotScreen } from '@/screens/Chatbot';
import { ComingSoonScreen } from '@/screens/ComingSoon';
import { CreateRequestSuccessScreen } from '@/screens/CreateRequestSuccess';
import { HomeScreen } from '@/screens/Home';
import { MaintenanceScreen } from '@/screens/Maintenance';
import { MealReservationScreen } from '@/screens/MealReservation';
import { NewRequestScreen } from '@/screens/NewRequest';
import { PendingRequestsScreen } from '@/screens/PendingRequests';
import { ProfileScreen } from '@/screens/Profile';
import { ReallocationScreen } from '@/screens/Reallocation';
import { RequestDetailScreen } from '@/screens/RequestDetail';
import { RoomBookingScreen } from '@/screens/RoomBooking';
import { TimeOffScreen } from '@/screens/TimeOff';
 
const Stack = createNativeStackNavigator<AppStackParamList>();
 
export const Navigation = () => {
  usePostLoginNotifications();
  useNavigateNotifications();
 
  const { theme, isDark } = useTheme();
  const { value: disableFeatures } = useDisabledFeatures();
  const { navigationRef } = useDeepLinkContext();
 
  const { data: meInfo, isLoading: isLoadingMeInfo } = useGetMeInfo<UserInfoBase>();
 
  const isAdmin = meInfo?.role === UserRole.ADMIN || false;
 
  const [isChatbotScreen, setIsChatbotScreen] = useState(false);
 
  const handleOpenChat = useCallback(() => {
    navigationRef?.current?.navigate(SCREENS.CHATBOT);
  }, [navigationRef]);
 
  // Bottom edge of this container in window coordinates — lets the FAB correct
  // remote-reported offsets on Android (see ChatbotFab).
  const containerRef = useRef<View>(null);
  const [containerBottom, setContainerBottom] = useState(0);
 
  const measureContainer = useCallback(() => {
    containerRef.current?.measureInWindow((_x, y, _width, height) => {
      if (height) setContainerBottom(y + height);
    });
  }, []);
 
  const styles = useStyles();
 
  const screenListeners = useMemo(
    () => ({
      state: (e: any) => {
        const routes = e.data?.state?.routes;
        if (routes) {
          const topRoute = routes[routes.length - 1];
          setIsChatbotScreen(topRoute?.name === SCREENS.CHATBOT);
        }
      },
    }),
    [],
  );
 
  return (
    <View ref={containerRef} style={styles.container} onLayout={measureContainer}>
      <StatusBar
        translucent
        backgroundColor={theme.colors.background.secondary}
        barStyle={isDark && HAS_DARK_PALETTE ? 'light-content' : 'dark-content'}
      />
 
      <Stack.Navigator initialRouteName={SCREENS.HOME} screenListeners={screenListeners}>
        <Stack.Group screenOptions={{ headerShown: false }}>
          <Stack.Screen name={SCREENS.NEW_REQUEST} component={NewRequestScreen} />
          <Stack.Screen name={SCREENS.COMING_SOON} component={ComingSoonScreen} />
          <Stack.Screen name={SCREENS.REQUEST_DETAIL} component={RequestDetailScreen} />
          {!disableFeatures[RequestCategory.REALLOCATION] && (
            <Stack.Screen name={SCREENS.REALLOCATION} component={ReallocationScreen} />
          )}
          {!disableFeatures[RequestCategory.MEAL_RESERVATION] && (
            <Stack.Screen name={SCREENS.MEAL_RESERVATION} component={MealReservationScreen} />
          )}
          {!disableFeatures[RequestCategory.TIME_OFF] && (
            <Stack.Screen name={SCREENS.TIME_OFF} component={TimeOffScreen} />
          )}
          {!disableFeatures[RequestCategory.BOOK_ROOM] && (
            <Stack.Screen name={SCREENS.BOOK_ROOM} component={RoomBookingScreen} />
          )}
          {!disableFeatures[RequestCategory.MAINTENANCE] && (
            <Stack.Screen name={SCREENS.MAINTENANCE} component={MaintenanceScreen} />
          )}
          <Stack.Screen
            name={SCREENS.CHATBOT}
            component={ChatbotScreen}
            options={{ animation: 'slide_from_bottom' }}
          />
        </Stack.Group>
 
        <Stack.Group screenOptions={{ headerShown: false }}>
          <Stack.Screen
            name={SCREENS.HOME}
            component={HomeScreen}
            options={{ gestureEnabled: false }}
          />
          <Stack.Screen
            name={SCREENS.PROFILE}
            component={ProfileScreen}
            options={{ gestureEnabled: false }}
          />
          <Stack.Screen name={SCREENS.PENDING_REQUESTS} component={PendingRequestsScreen} />
          <Stack.Screen
            name={SCREENS.CREATE_REQUEST_SUCCESS}
            component={CreateRequestSuccessScreen}
          />
        </Stack.Group>
      </Stack.Navigator>
 
      {/* Floating chatbot button */}
      {!isLoadingMeInfo && isAdmin && !isChatbotScreen && (
        <ChatbotFab onPress={handleOpenChat} containerBottom={containerBottom} />
      )}
    </View>
  );
};
 
const useStyles = makeStyles(() => ({
  container: {
    flex: 1,
  },
}));
 
export default Navigation;