"use client" import type React from "react" import { useState, useEffect } from "react" import { useAuth } from "@/context/AuthContext" import { LocationStorageService, type LocationData } from "@/lib/location/locationStorage" import type { GeolocationPosition } from "@/lib/location/types" interface LocationCaptureProps { onLocationCaptured?: (location: LocationData) => void onError?: (error: string) => void } export const LocationCapture: React.FC = ({ onLocationCaptured, onError }) => { const { session, fileSystem } = useAuth() const [isCapturing, setIsCapturing] = useState(false) const [permissionState, setPermissionState] = useState<"prompt" | "granted" | "denied">("prompt") const [currentLocation, setCurrentLocation] = useState(null) const [error, setError] = useState(null) // Show loading state while auth is initializing if (session.loading) { return (

Loading authentication...

) } // Check permission status on mount useEffect(() => { if ("permissions" in navigator) { navigator.permissions.query({ name: "geolocation" }).then((result) => { setPermissionState(result.state as "prompt" | "granted" | "denied") result.addEventListener("change", () => { setPermissionState(result.state as "prompt" | "granted" | "denied") }) }) } }, []) const captureLocation = async () => { // Don't proceed if still loading if (session.loading) { return } if (!session.authed) { const errorMsg = "You must be logged in to share your location. Please log in and try again." setError(errorMsg) onError?.(errorMsg) return } if (!fileSystem) { const errorMsg = "File system not available. Please refresh the page and try again." setError(errorMsg) onError?.(errorMsg) return } setIsCapturing(true) setError(null) try { // Request geolocation const position = await new Promise((resolve, reject) => { navigator.geolocation.getCurrentPosition( (pos) => resolve(pos as GeolocationPosition), (err) => reject(err), { enableHighAccuracy: true, timeout: 10000, maximumAge: 0, }, ) }) setCurrentLocation(position) // Create location data const locationData: LocationData = { id: crypto.randomUUID(), userId: session.username, latitude: position.coords.latitude, longitude: position.coords.longitude, accuracy: position.coords.accuracy, timestamp: position.timestamp, expiresAt: null, // Will be set when creating a share precision: "exact", } // Save to filesystem const storageService = new LocationStorageService(fileSystem) await storageService.initialize() await storageService.saveLocation(locationData) onLocationCaptured?.(locationData) } catch (err: any) { let errorMsg = "Failed to capture location" if (err.code === 1) { errorMsg = "Location permission denied. Please enable location access in your browser settings." setPermissionState("denied") } else if (err.code === 2) { errorMsg = "Location unavailable. Please check your device settings." } else if (err.code === 3) { errorMsg = "Location request timed out. Please try again." } setError(errorMsg) onError?.(errorMsg) } finally { setIsCapturing(false) } } return (

Share Your Location

Securely share your current location with others

{/* Permission status */} {permissionState === "denied" && (

Location access is blocked. Please enable it in your browser settings to continue.

)} {/* Current location display */} {currentLocation && (

Current Location

Latitude: {currentLocation.coords.latitude.toFixed(6)}

Longitude: {currentLocation.coords.longitude.toFixed(6)}

Accuracy: ±{Math.round(currentLocation.coords.accuracy)}m

Captured {new Date(currentLocation.timestamp).toLocaleString()}

)} {/* Error display */} {error && (

{error}

)} {/* Capture button */} {!session.authed && (

Please log in to share your location

)}
) }