Fix: Improve preview loading and error handling

This commit is contained in:
gpt-engineer-app[bot]
2025-10-11 14:45:22 +00:00
parent 1e30dedca2
commit 092337ee9e
4 changed files with 123 additions and 30 deletions

View File

@@ -15,7 +15,25 @@ export function useUnitPreferences() {
const [loading, setLoading] = useState(true);
useEffect(() => {
loadPreferences();
let mounted = true;
const load = async () => {
try {
await loadPreferences();
} catch (error) {
console.error('Failed to load preferences:', error);
} finally {
if (mounted) {
setLoading(false);
}
}
};
load();
return () => {
mounted = false;
};
}, [user]);
const loadPreferences = async () => {
@@ -58,7 +76,15 @@ export function useUnitPreferences() {
const autoDetectPreferences = useCallback(async () => {
try {
const response = await supabase.functions.invoke('detect-location');
// Add timeout to prevent hanging
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Location detection timeout')), 5000)
);
const response = await Promise.race([
supabase.functions.invoke('detect-location'),
timeoutPromise
]) as any;
if (response.data && response.data.measurementSystem) {
const newPreferences: UnitPreferences = {