From a6e9d77bda176dca16ccc3b8408e615f93fa1b2b 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 22:51:44 +0000 Subject: [PATCH] Fix unit display errors --- src/components/ui/measurement-display.tsx | 7 +-- src/components/ui/speed-display.tsx | 52 +++++++++++++++++++ src/hooks/useLocationAutoDetect.ts | 25 +++++++++ src/main.tsx | 8 ++- ...8_1ec91ae0-5287-4c2b-89d2-1629961c4cd6.sql | 26 ++++++++++ 5 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 src/components/ui/speed-display.tsx create mode 100644 src/hooks/useLocationAutoDetect.ts create mode 100644 supabase/migrations/20250928225018_1ec91ae0-5287-4c2b-89d2-1629961c4cd6.sql diff --git a/src/components/ui/measurement-display.tsx b/src/components/ui/measurement-display.tsx index f20c6941..31de4bf9 100644 --- a/src/components/ui/measurement-display.tsx +++ b/src/components/ui/measurement-display.tsx @@ -47,7 +47,7 @@ export function MeasurementDisplay({ return defaultPrefs; }, [profile]); - const { displayValue, unit, alternateDisplay } = useMemo(() => { + const { displayValue, unit, alternateDisplay, tooltipText } = useMemo(() => { const system = unitPreferences.measurement_system; let displayValue: number; @@ -88,12 +88,13 @@ export function MeasurementDisplay({ } const alternateDisplay = showBothUnits ? ` (${alternateValue} ${alternateUnit})` : ''; + const tooltipText = showBothUnits ? undefined : `${alternateValue} ${alternateUnit}`; - return { displayValue, unit, alternateDisplay }; + return { displayValue, unit, alternateDisplay, tooltipText }; }, [value, type, unitPreferences.measurement_system, showBothUnits]); return ( - + {displayValue} {unit}{alternateDisplay} ); diff --git a/src/components/ui/speed-display.tsx b/src/components/ui/speed-display.tsx new file mode 100644 index 00000000..7aaeea7f --- /dev/null +++ b/src/components/ui/speed-display.tsx @@ -0,0 +1,52 @@ +import { MeasurementDisplay } from './measurement-display'; + +interface SpeedDisplayProps { + kmh: number; + showBothUnits?: boolean; + className?: string; +} + +export function SpeedDisplay({ kmh, showBothUnits = false, className }: SpeedDisplayProps) { + return ( + + ); +} + +interface DistanceDisplayProps { + meters: number; + showBothUnits?: boolean; + className?: string; +} + +export function DistanceDisplay({ meters, showBothUnits = false, className }: DistanceDisplayProps) { + return ( + + ); +} + +interface HeightDisplayProps { + cm: number; + showBothUnits?: boolean; + className?: string; +} + +export function HeightDisplay({ cm, showBothUnits = false, className }: HeightDisplayProps) { + return ( + + ); +} \ No newline at end of file diff --git a/src/hooks/useLocationAutoDetect.ts b/src/hooks/useLocationAutoDetect.ts new file mode 100644 index 00000000..ba266143 --- /dev/null +++ b/src/hooks/useLocationAutoDetect.ts @@ -0,0 +1,25 @@ +import { useEffect } from 'react'; +import { useAuth } from '@/hooks/useAuth'; +import { useUnitPreferences } from '@/hooks/useUnitPreferences'; + +export function useLocationAutoDetect() { + const { user } = useAuth(); + const { preferences, autoDetectPreferences } = 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'); + }); + } + } + }, [user, preferences.auto_detect, preferences.measurement_system, autoDetectPreferences]); +} \ No newline at end of file diff --git a/src/main.tsx b/src/main.tsx index e18433e1..4cc81906 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -3,9 +3,15 @@ import { createRoot } from "react-dom/client"; import App from "./App.tsx"; import "./index.css"; import { ThemeProvider } from "@/components/theme/ThemeProvider"; +import { useLocationAutoDetect } from "./hooks/useLocationAutoDetect"; + +function AppWithAutoDetect() { + useLocationAutoDetect(); + return ; +} createRoot(document.getElementById("root")!).render( - + ); diff --git a/supabase/migrations/20250928225018_1ec91ae0-5287-4c2b-89d2-1629961c4cd6.sql b/supabase/migrations/20250928225018_1ec91ae0-5287-4c2b-89d2-1629961c4cd6.sql new file mode 100644 index 00000000..257985d4 --- /dev/null +++ b/supabase/migrations/20250928225018_1ec91ae0-5287-4c2b-89d2-1629961c4cd6.sql @@ -0,0 +1,26 @@ +-- Update existing user_preferences records to include unit preferences if they don't have them +UPDATE public.user_preferences +SET unit_preferences = '{"measurement_system": "metric", "temperature": "celsius", "auto_detect": true}'::jsonb +WHERE unit_preferences IS NULL OR unit_preferences = '{}'::jsonb; + +-- Update the initialize_user_preferences function to include unit preferences +CREATE OR REPLACE FUNCTION public.initialize_user_preferences() +RETURNS trigger +LANGUAGE plpgsql +SECURITY DEFINER +SET search_path = 'public' +AS $function$ +BEGIN + INSERT INTO public.user_preferences ( + user_id, + unit_preferences + ) + VALUES ( + NEW.user_id, + '{"measurement_system": "metric", "temperature": "celsius", "auto_detect": true}'::jsonb + ) + ON CONFLICT (user_id) DO UPDATE SET + unit_preferences = COALESCE(public.user_preferences.unit_preferences, '{"measurement_system": "metric", "temperature": "celsius", "auto_detect": true}'::jsonb); + RETURN NEW; +END; +$function$; \ No newline at end of file