mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 13:51:13 -05:00
Implement full Novu integration
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { useAuth } from '@/hooks/useAuth';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { logger } from '@/lib/logger';
|
||||
import { UnitPreferences, getMeasurementSystemFromCountry } from '@/lib/units';
|
||||
|
||||
const DEFAULT_PREFERENCES: UnitPreferences = {
|
||||
@@ -21,21 +22,28 @@ export function useUnitPreferences() {
|
||||
const loadPreferences = async () => {
|
||||
try {
|
||||
if (user) {
|
||||
// Load from database for logged-in users
|
||||
const { data } = await supabase
|
||||
const { data, error } = await supabase
|
||||
.from('user_preferences')
|
||||
.select('unit_preferences')
|
||||
.eq('user_id', user.id)
|
||||
.maybeSingle();
|
||||
|
||||
if (error && error.code !== 'PGRST116') {
|
||||
logger.error('Failed to fetch unit preferences', {
|
||||
userId: user.id,
|
||||
action: 'fetch_unit_preferences',
|
||||
error: error.message,
|
||||
errorCode: error.code
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (data?.unit_preferences && typeof data.unit_preferences === 'object') {
|
||||
setPreferences({ ...DEFAULT_PREFERENCES, ...(data.unit_preferences as unknown as UnitPreferences) });
|
||||
} else {
|
||||
// Auto-detect for new users
|
||||
await autoDetectPreferences();
|
||||
}
|
||||
} else {
|
||||
// Check localStorage for anonymous users
|
||||
const stored = localStorage.getItem('unit_preferences');
|
||||
if (stored) {
|
||||
try {
|
||||
@@ -49,7 +57,11 @@ export function useUnitPreferences() {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading unit preferences:', error);
|
||||
logger.error('Error loading unit preferences', {
|
||||
userId: user?.id,
|
||||
action: 'load_unit_preferences',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
await autoDetectPreferences();
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@@ -68,9 +80,7 @@ export function useUnitPreferences() {
|
||||
|
||||
setPreferences(newPreferences);
|
||||
|
||||
// Save to database for logged-in users, localStorage for anonymous users
|
||||
if (user) {
|
||||
// Use upsert with merge
|
||||
const { error } = await supabase
|
||||
.from('user_preferences')
|
||||
.upsert({
|
||||
@@ -80,7 +90,11 @@ export function useUnitPreferences() {
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error('Error saving preferences to database:', error);
|
||||
logger.error('Error saving auto-detected preferences', {
|
||||
userId: user.id,
|
||||
action: 'save_auto_detected_preferences',
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
} else {
|
||||
localStorage.setItem('unit_preferences', JSON.stringify(newPreferences));
|
||||
@@ -89,7 +103,11 @@ export function useUnitPreferences() {
|
||||
return newPreferences;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ Error auto-detecting location:', error);
|
||||
logger.error('Error auto-detecting location', {
|
||||
userId: user?.id,
|
||||
action: 'auto_detect_location',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
}
|
||||
|
||||
// Fallback to default
|
||||
@@ -103,7 +121,6 @@ export function useUnitPreferences() {
|
||||
|
||||
try {
|
||||
if (user) {
|
||||
// Save to database for logged-in users
|
||||
await supabase
|
||||
.from('user_preferences')
|
||||
.update({
|
||||
@@ -111,13 +128,20 @@ export function useUnitPreferences() {
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.eq('user_id', user.id);
|
||||
|
||||
logger.info('Unit preferences updated', {
|
||||
userId: user.id,
|
||||
action: 'update_unit_preferences'
|
||||
});
|
||||
} else {
|
||||
// Save to localStorage for anonymous users
|
||||
localStorage.setItem('unit_preferences', JSON.stringify(updated));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error saving unit preferences:', error);
|
||||
// Revert on error
|
||||
logger.error('Error saving unit preferences', {
|
||||
userId: user?.id,
|
||||
action: 'save_unit_preferences',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
});
|
||||
setPreferences(preferences);
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user