aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app/hooks
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>2026-03-13 11:12:36 +0000
committercopilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>2026-03-13 11:12:36 +0000
commit8b4252dc937d6c937bd718515f03dd48948a1519 (patch)
treee26f0455ebbf8ac0d28bcda749da26618b67cefe /src/frontend/app/hooks
parent1953bb738bb845c47e63ebc0789308a3cd00ddc2 (diff)
feat: geolocation hook, map context menu, simplified home planner widget"
Co-authored-by: arielcostas <94913521+arielcostas@users.noreply.github.com>
Diffstat (limited to 'src/frontend/app/hooks')
-rw-r--r--src/frontend/app/hooks/useGeolocation.ts59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/frontend/app/hooks/useGeolocation.ts b/src/frontend/app/hooks/useGeolocation.ts
new file mode 100644
index 0000000..572a40c
--- /dev/null
+++ b/src/frontend/app/hooks/useGeolocation.ts
@@ -0,0 +1,59 @@
+import { useCallback } from "react";
+import { useMap } from "../contexts/MapContext";
+import type { LngLatLike } from "maplibre-gl";
+
+export interface UseGeolocationResult {
+ userLocation: { latitude: number; longitude: number } | null;
+ hasLocationPermission: boolean;
+ requestLocation: () => void;
+}
+
+function lngLatToCoords(
+ loc: LngLatLike
+): { latitude: number; longitude: number } {
+ if (Array.isArray(loc)) {
+ // Stored as [lat, lng] per codebase convention
+ return { latitude: loc[0], longitude: loc[1] };
+ }
+ if ("lat" in loc) {
+ return {
+ latitude: loc.lat,
+ longitude: "lng" in loc ? (loc as any).lng : (loc as any).lon,
+ };
+ }
+ return { latitude: 0, longitude: 0 };
+}
+
+/**
+ * Provides the current user location from the global MapContext.
+ * Location updates are driven by the MapContext's watchPosition subscription
+ * (started automatically when geolocation permission is granted).
+ *
+ * Call `requestLocation()` to prompt the user for permission and start tracking.
+ */
+export function useGeolocation(): UseGeolocationResult {
+ const { mapState, setUserLocation, setLocationPermission } = useMap();
+
+ const requestLocation = useCallback(() => {
+ if (typeof window === "undefined" || !("geolocation" in navigator)) return;
+ navigator.geolocation.getCurrentPosition(
+ (pos) => {
+ setUserLocation([pos.coords.latitude, pos.coords.longitude]);
+ setLocationPermission(true);
+ },
+ () => {
+ setLocationPermission(false);
+ },
+ { enableHighAccuracy: false, maximumAge: 60000, timeout: 10000 }
+ );
+ }, [setUserLocation, setLocationPermission]);
+
+ const rawLoc = mapState.userLocation;
+ const userLocation = rawLoc ? lngLatToCoords(rawLoc) : null;
+
+ return {
+ userLocation,
+ hasLocationPermission: mapState.hasLocationPermission,
+ requestLocation,
+ };
+}