Files
thrilltrack-explorer/src/hooks/useLocationAutoDetect.ts
gpt-engineer-app[bot] 3b82887974 Move to Phase 4-5
2025-10-21 18:15:22 +00:00

34 lines
1.3 KiB
TypeScript

import { useEffect } from 'react';
import { useAuth } from '@/hooks/useAuth';
import { useUnitPreferences } from '@/hooks/useUnitPreferences';
import { logger } from '@/lib/logger';
import * as storage from '@/lib/localStorage';
export function useLocationAutoDetect() {
const { user } = useAuth();
const { preferences, autoDetectPreferences, loading } = useUnitPreferences();
useEffect(() => {
// Only run auto-detection after preferences have loaded
if (loading) return;
// Check if localStorage is available
if (!storage.isLocalStorageAvailable()) {
logger.warn('localStorage is not available, skipping location auto-detection');
return;
}
// Check if we've already attempted detection
const hasAttemptedDetection = storage.getItem('location_detection_attempted');
// Auto-detect if we haven't attempted it yet and auto_detect is enabled
if (preferences.auto_detect && !hasAttemptedDetection) {
autoDetectPreferences().then(() => {
storage.setItem('location_detection_attempted', 'true');
}).catch((error) => {
logger.error('Failed to auto-detect location', { error });
storage.setItem('location_detection_attempted', 'true');
});
}
}, [user, loading, preferences.auto_detect]);
}