mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:11:13 -05:00
Fix: Imperial units not displaying
This commit is contained in:
@@ -4,22 +4,35 @@ import { useUnitPreferences } from '@/hooks/useUnitPreferences';
|
|||||||
|
|
||||||
export function useLocationAutoDetect() {
|
export function useLocationAutoDetect() {
|
||||||
const { user } = useAuth();
|
const { user } = useAuth();
|
||||||
const { preferences, autoDetectPreferences } = useUnitPreferences();
|
const { preferences, autoDetectPreferences, loading } = useUnitPreferences();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Only auto-detect if user has auto_detect enabled and we haven't detected yet
|
// Only run auto-detection after preferences have loaded
|
||||||
if (preferences.auto_detect && preferences.measurement_system === 'metric') {
|
if (loading) return;
|
||||||
// Check if we've already attempted detection
|
|
||||||
const hasAttemptedDetection = localStorage.getItem('location_detection_attempted');
|
console.log('🌍 useLocationAutoDetect running:', {
|
||||||
|
user: !!user,
|
||||||
if (!hasAttemptedDetection) {
|
preferences,
|
||||||
autoDetectPreferences().then(() => {
|
loading
|
||||||
localStorage.setItem('location_detection_attempted', 'true');
|
});
|
||||||
}).catch((error) => {
|
|
||||||
console.error('Failed to auto-detect location:', error);
|
// For debugging - clear the flag to test detection again
|
||||||
localStorage.setItem('location_detection_attempted', 'true');
|
localStorage.removeItem('location_detection_attempted');
|
||||||
});
|
|
||||||
}
|
// Check if we've already attempted detection
|
||||||
|
const hasAttemptedDetection = localStorage.getItem('location_detection_attempted');
|
||||||
|
console.log('🔍 Detection attempt status:', hasAttemptedDetection);
|
||||||
|
|
||||||
|
// Auto-detect if we haven't attempted it yet and auto_detect is enabled
|
||||||
|
if (preferences.auto_detect && !hasAttemptedDetection) {
|
||||||
|
console.log('🚀 Starting auto-detection...');
|
||||||
|
autoDetectPreferences().then((result) => {
|
||||||
|
console.log('✅ Auto-detection completed:', result);
|
||||||
|
localStorage.setItem('location_detection_attempted', 'true');
|
||||||
|
}).catch((error) => {
|
||||||
|
console.error('❌ Failed to auto-detect location:', error);
|
||||||
|
localStorage.setItem('location_detection_attempted', 'true');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, [user, preferences.auto_detect, preferences.measurement_system, autoDetectPreferences]);
|
}, [user, preferences, loading, autoDetectPreferences]);
|
||||||
}
|
}
|
||||||
@@ -57,10 +57,13 @@ export function useUnitPreferences() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const autoDetectPreferences = async () => {
|
const autoDetectPreferences = async () => {
|
||||||
|
console.log('🌐 Calling edge function for location detection...');
|
||||||
try {
|
try {
|
||||||
const response = await supabase.functions.invoke('detect-location');
|
const response = await supabase.functions.invoke('detect-location');
|
||||||
|
console.log('📡 Edge function response:', response);
|
||||||
|
|
||||||
if (response.data && response.data.measurementSystem) {
|
if (response.data && response.data.measurementSystem) {
|
||||||
|
console.log('🎯 Detected measurement system:', response.data.measurementSystem);
|
||||||
const newPreferences: UnitPreferences = {
|
const newPreferences: UnitPreferences = {
|
||||||
...DEFAULT_PREFERENCES,
|
...DEFAULT_PREFERENCES,
|
||||||
measurement_system: response.data.measurementSystem,
|
measurement_system: response.data.measurementSystem,
|
||||||
@@ -71,15 +74,19 @@ export function useUnitPreferences() {
|
|||||||
// Save to localStorage for anonymous users
|
// Save to localStorage for anonymous users
|
||||||
if (!user) {
|
if (!user) {
|
||||||
localStorage.setItem('unit_preferences', JSON.stringify(newPreferences));
|
localStorage.setItem('unit_preferences', JSON.stringify(newPreferences));
|
||||||
|
console.log('💾 Saved preferences to localStorage:', newPreferences);
|
||||||
}
|
}
|
||||||
|
|
||||||
return newPreferences;
|
return newPreferences;
|
||||||
|
} else {
|
||||||
|
console.log('⚠️ No measurement system in response');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error auto-detecting location:', error);
|
console.error('❌ Error auto-detecting location:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to default
|
// Fallback to default
|
||||||
|
console.log('🔄 Falling back to default preferences');
|
||||||
setPreferences(DEFAULT_PREFERENCES);
|
setPreferences(DEFAULT_PREFERENCES);
|
||||||
return DEFAULT_PREFERENCES;
|
return DEFAULT_PREFERENCES;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user