import { useState } from "react"; import { useLocation } from "@/hooks/use-location"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Separator } from "@/components/ui/separator"; import { MapPin, ExternalLink, Copy, Share, Loader2, AlertCircle } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; interface LocationDetectorProps { onLocationDetected: (locationData: { latitude: number; longitude: number; googleMapsUrl: string; locationDetectedAutomatically: boolean; }) => void; onManualLocationSubmit: (manualData: { street: string; city: string; state: string; pinCode: string; country: string; locationDetectedAutomatically: false; }) => void; } export const LocationDetector: React.FC = ({ onLocationDetected, onManualLocationSubmit }) => { const { location, error, isLoading, getLocation, copyToClipboard } = useLocation(); const { toast } = useToast(); const [showManualEntry, setShowManualEntry] = useState(false); const [manualLocation, setManualLocation] = useState({ street: "", city: "", state: "", pinCode: "", country: "" }); const handleGetLocation = () => { getLocation(); }; const handleUseDetectedLocation = () => { if (location) { onLocationDetected({ latitude: location.latitude, longitude: location.longitude, googleMapsUrl: location.googleMapsUrl, locationDetectedAutomatically: true }); toast({ title: "Location detected!", description: "Your location has been automatically detected and saved." }); } }; const handleCopyUrl = async () => { if (location) { try { await copyToClipboard(location.googleMapsUrl); toast({ title: "Link copied!", description: "Google Maps link has been copied to your clipboard." }); } catch (err) { toast({ variant: "destructive", title: "Failed to copy", description: "Could not copy the link to clipboard." }); } } }; const handleManualSubmit = () => { if (!manualLocation.street || !manualLocation.city || !manualLocation.state || !manualLocation.pinCode || !manualLocation.country) { toast({ variant: "destructive", title: "Missing information", description: "Please fill in all required fields." }); return; } onManualLocationSubmit({ ...manualLocation, locationDetectedAutomatically: false }); toast({ title: "Manual location saved!", description: "Your location information has been saved." }); }; return ( Location Detection {/* Automatic Location Detection */}
{location && (

Location detected successfully!

Latitude: {location.latitude.toFixed(6)}, Longitude: {location.longitude.toFixed(6)}

{location.accuracy && (

Accuracy: ±{Math.round(location.accuracy)} meters

)}
)} {error && (

Location Detection Failed

{error.message}

)}
OR
{/* Manual Entry Option */}
{!showManualEntry ? ( ) : (

Manual Location Entry

setManualLocation(prev => ({...prev, street: e.target.value}))} placeholder="Enter street address" data-testid="input-street" />
setManualLocation(prev => ({...prev, city: e.target.value}))} placeholder="City" data-testid="input-city" />
setManualLocation(prev => ({...prev, state: e.target.value}))} placeholder="State" data-testid="input-state" />
setManualLocation(prev => ({...prev, pinCode: e.target.value}))} placeholder="PIN Code" data-testid="input-pincode" />
setManualLocation(prev => ({...prev, country: e.target.value}))} placeholder="Country" data-testid="input-country" />
)}
); };