All files / src/components/navigation-config NavigationConfigView.tsx

82.14% Statements 23/28
78.57% Branches 11/14
78.57% Functions 11/14
80% Lines 20/25

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                                                  9x 9x 9x 9x 48x 93x     48x   9x 8x 8x   9x 1x               1x 1x     9x       2x       1x                                                   1x                                       1x                             1x                                              
import React from "react";
import { StyleSheet, View, Text, ActivityIndicator, Alert } from "react-native";
import BottomDrawer from "../BottomDrawer";
import NavigationTransportCard from "./NavigationTransportCard";
import NavigationPathRow from "./NavigationPathRow";
import useNavigationConfig from "../../hooks/useNavigationConfig";
import useNavigationInfo from "../../hooks/useNavigationInfo";
import { OutdoorDirectionResponse } from "../../api/outdoorDirectionsApi";
import { TRANSPORT_MODE_API_MAP } from "../../type";
import { NAVIGATION_STATE } from "../../const";
import { useNavigationStore } from "../../hooks/useNavigationState";
 
interface NavigationConfigViewProps {
  readonly durations: OutdoorDirectionResponse[];
  readonly visible: boolean;
  readonly onClose: () => void;
  readonly onGo?: () => void;
}
 
export default function NavigationConfigView({
  durations,
  visible,
  onClose,
  onGo,
}: NavigationConfigViewProps) {
  const navigationMode = useNavigationConfig((s) => s.navigationMode);
  const setNavigationMode = useNavigationConfig((s) => s.setNavigationMode);
  const isLoading = useNavigationInfo((s) => s.isLoading);
  const getDurationForMode = (mode: string) => {
    const route = durations.find(
      (d) => d.transportMode?.toLowerCase() === mode.toLowerCase(),
    );
 
    return route ? route.duration : "N/A";
  };
  const getSelectedDuration = () => {
    const apiKey = TRANSPORT_MODE_API_MAP[navigationMode] || "walking";
    return getDurationForMode(apiKey);
  };
  const handleGo = () => {
    Iif (navigationMode === "SHUTTLE" && getSelectedDuration() === "N/A") {
      Alert.alert(
        "Shuttle Unavailable",
        "Shuttle service is currently unavailable for this route.",
      );
      return;
    }
    // Logic to start the actual turn-by-turn navigation
    console.log("Start navigation with mode:", navigationMode);
    onGo?.();
  };
 
  return (
    <BottomDrawer
      visible={visible}
      onClose={() => {
        if (
          useNavigationStore.getState().navigationState !==
          NAVIGATION_STATE.NAVIGATING
        ) {
          onClose();
        }
      }}
      snapPoints={["35%"]} // Adjusted height for this content
      enablePanDownToClose={true}
      contentContainerStyle={styles.drawerContent}
    >
      {isLoading ? (
        <View style={styles.loadingContainer}>
          <ActivityIndicator size="large" color="#800020" />
          <Text>Calculating Route...</Text>
        </View>
      ) : (
        <>
          {/* 1. Transport Mode Selection Row */}
          <View style={styles.transportRow}>
            <NavigationTransportCard
              mode="WALK"
              duration={getDurationForMode(TRANSPORT_MODE_API_MAP.WALK)}
              isSelected={navigationMode === "WALK"}
              onSelect={() => setNavigationMode("WALK")}
            />
            <NavigationTransportCard
              mode="BIKE"
              duration={getDurationForMode(TRANSPORT_MODE_API_MAP.BIKE)}
              isSelected={navigationMode === "BIKE"}
              onSelect={() => setNavigationMode("BIKE")}
            />
            <NavigationTransportCard
              mode="CAR"
              duration={getDurationForMode(TRANSPORT_MODE_API_MAP.CAR)}
              isSelected={navigationMode === "CAR"}
              onSelect={() => setNavigationMode("CAR")}
            />
            <NavigationTransportCard
              mode="BUS"
              duration={getDurationForMode(TRANSPORT_MODE_API_MAP.BUS)}
              isSelected={navigationMode === "BUS"}
              onSelect={() => setNavigationMode("BUS")}
            />
            <NavigationTransportCard
              mode="SHUTTLE"
              duration={
                getDurationForMode(TRANSPORT_MODE_API_MAP.SHUTTLE) || "N/A"
              }
              isSelected={navigationMode === "SHUTTLE"}
              onSelect={() => setNavigationMode("SHUTTLE")}
            />
          </View>
 
          {/* 2. Stats & Action Row */}
          <NavigationPathRow
            duration={getSelectedDuration()}
            handleGo={handleGo}
          />
        </>
      )}
    </BottomDrawer>
  );
}
 
const styles = StyleSheet.create({
  drawerContent: {
    paddingHorizontal: 20,
    paddingTop: 10,
    alignItems: "center",
    backgroundColor: "",
  },
  transportRow: {
    flexDirection: "row",
    justifyContent: "space-between",
    alignItems: "center",
    width: "100%",
    marginBottom: 20,
    borderRadius: 5,
    backgroundColor: "#D9D9D9",
  },
  loadingContainer: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    height: 150,
  },
});