From 376c17492d23be58d4914732be21e751c4eacec5 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Sun, 28 Sep 2025 23:04:16 +0000 Subject: [PATCH] Fix: Imperial units not displaying --- src/hooks/useLocationAutoDetect.ts | 43 +++++++++++++++++++----------- src/hooks/useUnitPreferences.ts | 9 ++++++- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/hooks/useLocationAutoDetect.ts b/src/hooks/useLocationAutoDetect.ts index ba266143..9461b601 100644 --- a/src/hooks/useLocationAutoDetect.ts +++ b/src/hooks/useLocationAutoDetect.ts @@ -4,22 +4,35 @@ import { useUnitPreferences } from '@/hooks/useUnitPreferences'; export function useLocationAutoDetect() { const { user } = useAuth(); - const { preferences, autoDetectPreferences } = useUnitPreferences(); + const { preferences, autoDetectPreferences, loading } = useUnitPreferences(); useEffect(() => { - // Only auto-detect if user has auto_detect enabled and we haven't detected yet - if (preferences.auto_detect && preferences.measurement_system === 'metric') { - // Check if we've already attempted detection - const hasAttemptedDetection = localStorage.getItem('location_detection_attempted'); - - if (!hasAttemptedDetection) { - autoDetectPreferences().then(() => { - localStorage.setItem('location_detection_attempted', 'true'); - }).catch((error) => { - console.error('Failed to auto-detect location:', error); - localStorage.setItem('location_detection_attempted', 'true'); - }); - } + // Only run auto-detection after preferences have loaded + if (loading) return; + + console.log('🌍 useLocationAutoDetect running:', { + user: !!user, + preferences, + loading + }); + + // For debugging - clear the flag to test detection again + 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]); } \ No newline at end of file diff --git a/src/hooks/useUnitPreferences.ts b/src/hooks/useUnitPreferences.ts index 6cf0dcd7..36ae2e15 100644 --- a/src/hooks/useUnitPreferences.ts +++ b/src/hooks/useUnitPreferences.ts @@ -57,10 +57,13 @@ export function useUnitPreferences() { }; const autoDetectPreferences = async () => { + console.log('🌐 Calling edge function for location detection...'); try { const response = await supabase.functions.invoke('detect-location'); + console.log('📡 Edge function response:', response); if (response.data && response.data.measurementSystem) { + console.log('🎯 Detected measurement system:', response.data.measurementSystem); const newPreferences: UnitPreferences = { ...DEFAULT_PREFERENCES, measurement_system: response.data.measurementSystem, @@ -71,15 +74,19 @@ export function useUnitPreferences() { // Save to localStorage for anonymous users if (!user) { localStorage.setItem('unit_preferences', JSON.stringify(newPreferences)); + console.log('💾 Saved preferences to localStorage:', newPreferences); } return newPreferences; + } else { + console.log('⚠️ No measurement system in response'); } } catch (error) { - console.error('Error auto-detecting location:', error); + console.error('❌ Error auto-detecting location:', error); } // Fallback to default + console.log('🔄 Falling back to default preferences'); setPreferences(DEFAULT_PREFERENCES); return DEFAULT_PREFERENCES; };