Piyush Agarwal d1ad533352 Klimate App
2024-11-07 11:47:30 +05:30

82 lines
1.9 KiB
TypeScript

import { useState, useEffect } from "react";
import type { Coordinates } from "@/api/types";
interface GeolocationState {
coordinates: Coordinates | null;
error: string | null;
isLoading: boolean;
}
export function useGeolocation() {
const [locationData, setLocationData] = useState<GeolocationState>({
coordinates: null,
error: null,
isLoading: true,
});
const getLocation = () => {
setLocationData((prev) => ({ ...prev, isLoading: true, error: null }));
if (!navigator.geolocation) {
setLocationData({
coordinates: null,
error: "Geolocation is not supported by your browser",
isLoading: false,
});
return;
}
navigator.geolocation.getCurrentPosition(
(position) => {
setLocationData({
coordinates: {
lat: position.coords.latitude,
lon: position.coords.longitude,
},
error: null,
isLoading: false,
});
},
(error) => {
let errorMessage: string;
switch (error.code) {
case error.PERMISSION_DENIED:
errorMessage =
"Location permission denied. Please enable location access.";
break;
case error.POSITION_UNAVAILABLE:
errorMessage = "Location information is unavailable.";
break;
case error.TIMEOUT:
errorMessage = "Location request timed out.";
break;
default:
errorMessage = "An unknown error occurred.";
}
setLocationData({
coordinates: null,
error: errorMessage,
isLoading: false,
});
},
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0,
}
);
};
// Get location on component mount
useEffect(() => {
getLocation();
}, []);
return {
...locationData,
getLocation, // Expose method to manually refresh location
};
}