import React, { useEffect, useRef, useState } from 'react'; import { StyleSheet, View } from 'react-native'; import * as Location from 'expo-location'; import * as MapLibreRN from '@maplibre/maplibre-react-native'; import { SheetManager, useSheetRouteParams, useSheetRouter } from 'react-native-actions-sheet'; import { Colors } from 'src/theme'; import { VECTOR_MAP_HOST } from 'src/constants'; import { ButtonVariants } from 'src/types/components'; import { Button } from 'src/components'; const RouteB = () => { const router = useSheetRouter('chat-attachments'); const params = useSheetRouteParams('chat-attachments', 'route-b'); const { onSendLocation, insetsBottom }: { onSendLocation: (coords: { latitude: number; longitude: number }) => void; insetsBottom: number; } = params; const [currentLocation, setCurrentLocation] = useState<{ latitude: number; longitude: number; } | null>(null); const [selectedLocation, setSelectedLocation] = useState<{ latitude: number; longitude: number; } | null>(null); const [loading, setLoading] = useState(true); const cameraRef = useRef(null); const [mapDimensions, setMapDimensions] = useState({ width: 0, height: 0, x: 0, y: 0 }); useEffect(() => { const getLocation = async () => { try { const { status } = await Location.requestForegroundPermissionsAsync(); if (status !== 'granted') { return; } const location = await Location.getCurrentPositionAsync({}); const coords = { latitude: location.coords.latitude, longitude: location.coords.longitude }; setCurrentLocation(coords); setSelectedLocation(coords); } catch (err) { console.warn('Error fetching location:', err); } }; getLocation(); }, []); useEffect(() => { if (currentLocation) { const timeoutId = setTimeout(() => { if (cameraRef.current) { cameraRef.current.setCamera({ centerCoordinate: [currentLocation.longitude, currentLocation.latitude], zoomLevel: 14, animationDuration: 500 }); setLoading(false); } else { console.warn('Camera ref is not available.'); } }, 500); return () => clearTimeout(timeoutId); } }, [currentLocation]); const confirmLocation = () => { if (selectedLocation) { onSendLocation(selectedLocation); } SheetManager.hide('location-picker'); SheetManager.hide('chat-attachments'); }; const sendCurrentLocation = () => { if (currentLocation) { onSendLocation(currentLocation); } SheetManager.hide('location-picker'); SheetManager.hide('chat-attachments'); }; const handleRegionChange = (event: any) => { const { geometry } = event; if (geometry) { const [longitude, latitude] = geometry.coordinates; setSelectedLocation({ latitude, longitude }); } }; return ( { const { x, y, width, height } = event.nativeEvent.layout; setMapDimensions({ x, y, width, height }); }} > {currentLocation && ( )}