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

90.9% Statements 30/33
78.26% Branches 18/23
77.77% Functions 7/9
90.9% Lines 30/33

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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214                                                                                          1x   1x 4x         1x 4x   4x 4x 4x 4x 4x   4x 4x   4x 4x 4x     4x 4x   4x   4x       4x         4x 1x 1x       4x 4x     4x                                                                                                                                                               1x 4x 4x   4x                                                                      
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import {
  Dimensions,
  Keyboard,
  Pressable,
  StatusBar,
  StyleSheet,
  useColorScheme,
  View,
} from 'react-native';
 
import { BottomSheetBackdrop, BottomSheetModal } from '@gorhom/bottom-sheet';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
 
import { ChatbotIcon } from '@repo/ui/icons/Chatbot';
import { useTheme } from '@repo/ui/themes/ThemeContext';
 
import { SCREENS } from '@repo/constants/screens';
 
import { useGetMeInfo } from '@repo/hooks';
import { useDisabledFeatures } from '@repo/hooks/flags';
 
import { ChatBox } from '@/components/ChatBox';
import { ChatHeader } from '@/components/ChatBox/ChatHeader';
 
import { useChat } from '@/contexts/ChatContext';
 
import type { AppStackV2ParamList } from '@/types/navigation';
import { RequestCategory } from '@/types/request';
import { UserInfoBase, UserRole } from '@/types/user';
 
import { ComingSoonScreen } from '@/screens/ComingSoon';
import { CreateRequestSuccessScreen } from '@/screens/CreateRequestSuccess';
import { HomeScreenV2 } from '@/screens/HomeV2';
import { LazyLoadMaintenanceScreen } from '@/screens/LazyLoadMaintenance';
import { LazyLoadMealReservationScreen } from '@/screens/LazyLoadMealReservation';
import { LazyLoadRoomBookingScreen } from '@/screens/LazyLoadRoomBooking';
import { NewRequestScreen } from '@/screens/NewRequest';
import { PendingRequestsScreen } from '@/screens/PendingRequests';
import { ProfileScreen } from '@/screens/Profile';
import { ReallocationV2Screen } from '@/screens/ReallocationV2';
import { RequestDetailScreen } from '@/screens/RequestDetail';
import { TimeOffV2Screen } from '@/screens/TimeOffV2';
 
const Stack = createNativeStackNavigator<AppStackV2ParamList>();
 
const CustomBackdrop = (props: any) => (
  <BottomSheetBackdrop {...props} disappearsOnIndex={-1} appearsOnIndex={0} opacity={0.5} />
);
 
// CustomHandle removed in favor of integrated ChatHeader
 
const NavigationV2 = () => {
  const { fabOffset } = useChat();
 
  const insets = useSafeAreaInsets();
  const styles = useStyles(fabOffset, insets.bottom);
  const { theme } = useTheme();
  const isDarkMode = useColorScheme() === 'dark';
  const { value: disableFeatures } = useDisabledFeatures();
 
  const bottomSheetRef = useRef<BottomSheetModal>(null);
  const screenHeight = Dimensions.get('screen').height;
 
  const snapPoints = useMemo(() => {
    const statusBarHeight = insets.top || (StatusBar.currentHeight ?? 40);
    return [screenHeight - statusBarHeight];
  }, [screenHeight, insets.top]);
 
  const { data: meInfo, isLoading: isLoadingMeInfo } = useGetMeInfo<UserInfoBase>();
  const { setCloseChat } = useChat();
 
  const isAdmin = meInfo?.role === UserRole.ADMIN || false;
 
  const handleOpenChat = useCallback(() => {
    bottomSheetRef.current?.present();
  }, []);
 
  const handleCloseChat = useCallback(() => {
    Keyboard.dismiss();
    bottomSheetRef.current?.dismiss();
  }, []);
 
  const handleAnimate = useCallback((_: number, toIndex: number) => {
    Eif (toIndex === -1) {
      Keyboard.dismiss();
    }
  }, []);
 
  useEffect(() => {
    setCloseChat(handleCloseChat);
  }, [handleCloseChat, setCloseChat]);
 
  return (
    <View style={styles.container}>
      <StatusBar
        translucent
        backgroundColor={theme.colors.background.secondary}
        barStyle={isDarkMode ? 'light-content' : 'dark-content'}
      />
 
      <Stack.Navigator initialRouteName={SCREENS.HOME_V2}>
        <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={ReallocationV2Screen} />
          )}
          {!disableFeatures[RequestCategory.MEAL_RESERVATION] && (
            <Stack.Screen
              name={SCREENS.MEAL_RESERVATION}
              component={LazyLoadMealReservationScreen}
            />
          )}
          {!disableFeatures[RequestCategory.TIME_OFF] && (
            <Stack.Screen name={SCREENS.TIME_OFF} component={TimeOffV2Screen} />
          )}
          {!disableFeatures[RequestCategory.BOOK_ROOM] && (
            <Stack.Screen name={SCREENS.BOOK_ROOM} component={LazyLoadRoomBookingScreen} />
          )}
          {!disableFeatures[RequestCategory.MAINTENANCE] && (
            <Stack.Screen name={SCREENS.MAINTENANCE} component={LazyLoadMaintenanceScreen} />
          )}
        </Stack.Group>
 
        <Stack.Group screenOptions={{ headerShown: false }}>
          <Stack.Screen
            name={SCREENS.HOME_V2}
            component={HomeScreenV2}
            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 button */}
      {!isLoadingMeInfo && isAdmin && (
        <Pressable style={[styles.nextIcon]} onPress={handleOpenChat} testID="chatbot-fab">
          <ChatbotIcon height={52} width={52} color={theme.colors.white} />
        </Pressable>
      )}
 
      {/* Bottom Sheet */}
      <BottomSheetModal
        ref={bottomSheetRef}
        snapPoints={snapPoints}
        enableDynamicSizing={false}
        enableContentPanningGesture={false}
        backdropComponent={CustomBackdrop}
        keyboardBehavior="fillParent"
        keyboardBlurBehavior="restore"
        android_keyboardInputMode="adjustResize"
        topInset={insets.top}
        handleComponent={ChatHeader}
        onAnimate={handleAnimate}
        backgroundStyle={styles.bottomSheetBackground}
      >
        <ChatBox />
      </BottomSheetModal>
    </View>
  );
};
 
const useStyles = (fabOffset: number, bottomInset: number = 0) => {
  const { theme } = useTheme();
  return useMemo(
    () =>
      StyleSheet.create({
        container: {
          flex: 1,
        },
        nextIcon: {
          position: 'absolute',
          bottom: 32 + fabOffset + bottomInset,
          right: 16,
          justifyContent: 'center',
          alignItems: 'center',
        },
        handleContainer: {
          backgroundColor: theme.colors.slate97,
          borderTopLeftRadius: 16,
          borderTopRightRadius: 16,
          paddingVertical: 10,
          alignItems: 'center',
        },
        handleIndicator: {
          width: 40,
          height: 4,
          borderRadius: 2,
          backgroundColor: 'rgba(255, 255, 255, 0.4)',
        },
        bottomSheetBackground: {
          backgroundColor: theme.colors.background.secondary,
          borderTopLeftRadius: 16,
          borderTopRightRadius: 16,
        },
      }),
    [theme, fabOffset, bottomInset],
  );
};
 
export default NavigationV2;