Improve error handling and dependency checks for data processing

Address issues in FieldHistoryDialog by clearing changes on error, enhance useEntityVersions to provide default profiles for missing users, add localStorage availability check in useLocationAutoDetect, and improve dependency resolution logic in supabase/functions/process-selective-approval.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 334e80ad-8f74-4e3a-acec-6dd811858e98
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
This commit is contained in:
pac7
2025-10-08 12:23:22 +00:00
parent 6f74fcaed7
commit 2fc78cbde5
4 changed files with 40 additions and 8 deletions

View File

@@ -88,6 +88,7 @@ export function FieldHistoryDialog({
setChanges(mergedData as FieldChange[]);
} catch (error) {
console.error('Error fetching field history:', error);
setChanges([]);
} finally {
setLoading(false);
}

View File

@@ -65,10 +65,16 @@ export function useEntityVersions(entityType: string, entityId: string) {
.select('user_id, username, avatar_url')
.in('user_id', userIds);
const versionsWithProfiles = data?.map(v => ({
...v,
changer_profile: profiles?.find(p => p.user_id === v.changed_by)
})) as EntityVersion[];
const versionsWithProfiles = data?.map(v => {
const profile = profiles?.find(p => p.user_id === v.changed_by);
return {
...v,
changer_profile: profile || {
username: 'Unknown',
avatar_url: null
}
};
}) as EntityVersion[];
// Only update state if component is still mounted
if (isMountedRef.current) {

View File

@@ -2,6 +2,20 @@ import { useEffect } from 'react';
import { useAuth } from '@/hooks/useAuth';
import { useUnitPreferences } from '@/hooks/useUnitPreferences';
function isLocalStorageAvailable(): boolean {
try {
if (typeof window === 'undefined' || typeof localStorage === 'undefined') {
return false;
}
const testKey = '__localStorage_test__';
localStorage.setItem(testKey, 'test');
localStorage.removeItem(testKey);
return true;
} catch {
return false;
}
}
export function useLocationAutoDetect() {
const { user } = useAuth();
const { preferences, autoDetectPreferences, loading } = useUnitPreferences();
@@ -10,16 +24,26 @@ export function useLocationAutoDetect() {
// Only run auto-detection after preferences have loaded
if (loading) return;
// Check if localStorage is available
if (!isLocalStorageAvailable()) {
console.warn('localStorage is not available, skipping location auto-detection');
return;
}
// Check if we've already attempted detection
const hasAttemptedDetection = localStorage.getItem('location_detection_attempted');
// Auto-detect if we haven't attempted it yet and auto_detect is enabled
if (preferences.auto_detect && !hasAttemptedDetection) {
autoDetectPreferences().then(() => {
localStorage.setItem('location_detection_attempted', 'true');
if (isLocalStorageAvailable()) {
localStorage.setItem('location_detection_attempted', 'true');
}
}).catch((error) => {
console.error('❌ Failed to auto-detect location:', error);
localStorage.setItem('location_detection_attempted', 'true');
if (isLocalStorageAvailable()) {
localStorage.setItem('location_detection_attempted', 'true');
}
});
}
}, [user, loading, preferences.auto_detect]);

View File

@@ -318,9 +318,10 @@ function topologicalSort(items: any[]): any[] {
if (item.depends_on) {
const parent = items.find(i => i.id === item.depends_on);
if (parent) {
visit(parent);
if (!parent) {
throw new Error(`Missing dependency: item ${item.id} depends on ${item.depends_on} which is not in the submission`);
}
visit(parent);
}
visiting.delete(item.id);