import { useDriverStore, useLocationStore } from "@/store"; import { calculateDriverTimes, calculateRegion, generateMarkersFromData } from "@/lib/map"; import MapView, { Marker, PROVIDER_DEFAULT } from "react-native-maps"; import tw from "twrnc"; import { useEffect, useState } from "react"; import { Driver, MarkerData } from "@/types/type"; import { icons } from "@/constants"; import { useFetch } from "@/lib/fetch"; import { ActivityIndicator, Text, View } from "react-native"; const Map = () => { const { data: drivers, loading, error } = useFetch("/(api)/driver"); const { userLongitude, userLatitude, destinationLatitude, destinationLongitude } = useLocationStore(); const { selectedDriver, setDrivers } = useDriverStore(); const [markers, setMarkers] = useState([]); const region = calculateRegion({ userLongitude, userLatitude, destinationLatitude, destinationLongitude, }); useEffect(() => { if (Array.isArray(drivers)) { if (!userLatitude || !userLongitude) return; const newMarkers = generateMarkersFromData({ data: drivers, userLatitude, userLongitude, }); setMarkers(newMarkers); } }, [drivers]); useEffect(() => { if (markers.length > 0 && destinationLatitude && destinationLongitude) { calculateDriverTimes({ markers, userLongitude, userLatitude, destinationLatitude, destinationLongitude }).then((drivers) => { setDrivers(drivers as MarkerData[]); }); } }, [markers, destinationLatitude, destinationLongitude]); if (loading || !userLatitude || !userLongitude) { return ( ); } if (error) { return ( Error:{error} ); } return ( {markers?.map((marker) => ( ))} ) } export default Map;