mirror of
https://github.com/piyush-eon/tanstack-query-weather-app.git
synced 2025-11-24 05:11:19 +00:00
82 lines
1.9 KiB
TypeScript
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
|
|
};
|
|
}
|