mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 07:31:12 -05:00
Refactor code structure and remove redundant changes
This commit is contained in:
42
src-old/hooks/useLocationAutoDetect.ts
Normal file
42
src-old/hooks/useLocationAutoDetect.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import { useUnitPreferences } from '@/hooks/useUnitPreferences';
|
||||
import { handleNonCriticalError } from '@/lib/errorHandler';
|
||||
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) => {
|
||||
handleNonCriticalError(error, {
|
||||
action: 'Auto-detect user location',
|
||||
userId: user?.id,
|
||||
metadata: {
|
||||
autoDetectEnabled: preferences.auto_detect,
|
||||
context: 'initial_load'
|
||||
}
|
||||
});
|
||||
storage.setItem('location_detection_attempted', 'true');
|
||||
});
|
||||
}
|
||||
}, [user, loading, preferences.auto_detect]);
|
||||
}
|
||||
Reference in New Issue
Block a user