2026-05-24 19:51:29 +00:00
|
|
|
'use client';
|
|
|
|
|
|
2026-05-24 22:47:47 +00:00
|
|
|
import { useEffect, useRef } from 'react';
|
2026-07-07 18:43:17 +00:00
|
|
|
import { useAuth } from '@/components/AuthProvider';
|
2026-05-24 22:47:47 +00:00
|
|
|
import { getSettings, saveDriverLocation, isAuthenticated } from '@/lib/pocketbase';
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
export default function LocationTracker() {
|
2026-07-07 18:43:17 +00:00
|
|
|
const { isLoggedIn } = useAuth();
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
const watchRef = useRef<number | null>(null);
|
2026-05-24 19:51:29 +00:00
|
|
|
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
const wakeLockRef = useRef<WakeLockSentinel | null>(null);
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-07-07 18:43:17 +00:00
|
|
|
// Skip entirely when not signed in — previously the watcher
|
|
|
|
|
// was set up regardless of auth, which meant we requested a
|
|
|
|
|
// GPS lock on the login screen (and ate battery doing nothing).
|
|
|
|
|
if (!isLoggedIn) return;
|
2026-05-24 22:47:47 +00:00
|
|
|
if (!('geolocation' in navigator)) return;
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
const settings = getSettings();
|
2026-05-24 20:31:35 +00:00
|
|
|
const intervalMs = Math.max(30000, settings.locationInterval * 1000);
|
2026-05-24 19:51:29 +00:00
|
|
|
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
const sendLocation = (position: GeolocationPosition) => {
|
2026-05-24 22:47:47 +00:00
|
|
|
if (!isAuthenticated()) return;
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
if (position.coords.accuracy > settings.minimumAccuracy) return;
|
2026-05-24 22:47:47 +00:00
|
|
|
|
|
|
|
|
const auth = JSON.parse(localStorage.getItem('pocketbase_auth') || '{}');
|
|
|
|
|
const user = auth?.model;
|
|
|
|
|
if (!user) return;
|
|
|
|
|
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
saveDriverLocation({
|
2026-07-07 23:35:26 +00:00
|
|
|
// PB schema note: driver_locations stores the user relation under
|
|
|
|
|
// `relation`, not `driver`. Using the wrong key here silently
|
|
|
|
|
// 400'd inside a try/catch — see lib/types.ts DriverLocation.
|
|
|
|
|
relation: user.id,
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
latitude: position.coords.latitude,
|
|
|
|
|
longitude: position.coords.longitude,
|
|
|
|
|
accuracy: position.coords.accuracy || undefined,
|
|
|
|
|
altitude: position.coords.altitude || undefined,
|
|
|
|
|
speed: position.coords.speed || undefined,
|
|
|
|
|
heading: position.coords.heading || undefined,
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 1. watchPosition keeps GPS active (helps Android stay alive longer)
|
|
|
|
|
watchRef.current = navigator.geolocation.watchPosition(
|
|
|
|
|
sendLocation,
|
|
|
|
|
() => {},
|
|
|
|
|
{
|
|
|
|
|
enableHighAccuracy: settings.enableHighAccuracy,
|
|
|
|
|
maximumAge: 5000, // fresh GPS every 5s
|
|
|
|
|
timeout: 30000,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 2. Interval fallback (for when watchPosition is throttled)
|
|
|
|
|
intervalRef.current = setInterval(() => {
|
2026-05-24 20:31:35 +00:00
|
|
|
navigator.geolocation.getCurrentPosition(
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
sendLocation,
|
2026-05-24 22:47:47 +00:00
|
|
|
() => {},
|
|
|
|
|
{ enableHighAccuracy: settings.enableHighAccuracy, maximumAge: intervalMs, timeout: 30000 }
|
2026-05-24 19:51:29 +00:00
|
|
|
);
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
}, intervalMs);
|
2026-05-24 19:51:29 +00:00
|
|
|
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
// 3. Screen Wake Lock — keeps phone awake while app is open
|
|
|
|
|
if ('wakeLock' in navigator) {
|
|
|
|
|
navigator.wakeLock.request('screen')
|
|
|
|
|
.then((lock) => { wakeLockRef.current = lock; })
|
|
|
|
|
.catch(() => {});
|
|
|
|
|
// Re-acquire if page regains focus
|
|
|
|
|
const onFocus = () => {
|
|
|
|
|
if (!wakeLockRef.current) {
|
|
|
|
|
navigator.wakeLock.request('screen')
|
|
|
|
|
.then((lock) => { wakeLockRef.current = lock; })
|
|
|
|
|
.catch(() => {});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener('visibilitychange', () => {
|
|
|
|
|
if (document.visibilityState === 'visible') onFocus();
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-05-24 19:51:29 +00:00
|
|
|
|
2026-05-24 20:31:35 +00:00
|
|
|
return () => {
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
if (watchRef.current !== null) {
|
|
|
|
|
navigator.geolocation.clearWatch(watchRef.current);
|
|
|
|
|
watchRef.current = null;
|
|
|
|
|
}
|
2026-05-24 19:51:29 +00:00
|
|
|
if (intervalRef.current !== null) {
|
|
|
|
|
clearInterval(intervalRef.current);
|
|
|
|
|
intervalRef.current = null;
|
|
|
|
|
}
|
Add PWA support and UI redesign
Add full PWA support (icons, manifest, service worker, pwa lib and bridge) and introduce install/offline UI. Generate app icons via a new scripts/generate-icons.js and include multiple icon sizes in public/icons; update manifest and layout head meta/preconnect. Revamp global styles (globals.css) with new utilities, animations, spacing for bottom nav, card shadows, skeletons and accessibility tweaks. Polish major screens (login, collections, map, settings) with improved headers, loading states, touch/press feedback, and layout adjustments; add new components (InstallPrompt, OfflineIndicator, PWABridge) and update various components to match new visual/UX patterns. Update Dockerfile to generate icons and use Next.js static export output (/out) for nginx, and bump package setup to support icon generation.
2026-05-26 22:34:50 +00:00
|
|
|
if (wakeLockRef.current) {
|
|
|
|
|
wakeLockRef.current.release();
|
|
|
|
|
wakeLockRef.current = null;
|
|
|
|
|
}
|
2026-05-24 19:51:29 +00:00
|
|
|
};
|
2026-07-07 18:43:17 +00:00
|
|
|
}, [isLoggedIn]);
|
2026-05-24 19:51:29 +00:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|