WFDASHDRIVER-APP/components/LocationTracker.tsx
SJCoetz 0d3272da46 Refactor collections to order items
Convert the app from using legacy Collection models to OrderItem-based data. Updated pages (app/page and app/map/page) and all related components (CollectionMap, CollectionItemCard, CollectModal, ProblemModal, LocationTracker) to use OrderItem and new field names (supplier, order, item_status, qty_collected/collected_date, problem_flag/notes, etc.). PocketBase client now queries/updates the order_items collection and maps status/field changes accordingly. Simplified location tracking logic and cleaned up map/navigation UI and Suspense fallbacks. Added import_suppliers.py script for migrating suppliers from NocoDB and included .DS_Store artifacts.
2026-05-25 00:47:47 +02:00

54 lines
1.6 KiB
TypeScript

'use client';
import { useEffect, useRef } from 'react';
import { getSettings, saveDriverLocation, isAuthenticated } from '@/lib/pocketbase';
export default function LocationTracker() {
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
useEffect(() => {
if (!('geolocation' in navigator)) return;
const settings = getSettings();
const intervalMs = Math.max(30000, settings.locationInterval * 1000);
const trackLocation = () => {
if (!isAuthenticated()) return;
const auth = JSON.parse(localStorage.getItem('pocketbase_auth') || '{}');
const user = auth?.model;
if (!user) return;
navigator.geolocation.getCurrentPosition(
(position) => {
if (position.coords.accuracy > settings.minimumAccuracy) return;
saveDriverLocation({
driver: user.id,
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(),
});
},
() => {},
{ enableHighAccuracy: settings.enableHighAccuracy, maximumAge: intervalMs, timeout: 30000 }
);
};
trackLocation();
intervalRef.current = setInterval(trackLocation, intervalMs);
return () => {
if (intervalRef.current !== null) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
}, []);
return null;
}