mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 07:31:12 -05:00
Improve error handling and stability across the application
Refactor error handling in `useEntityVersions` and `useSearch` hooks, enhance `NotificationService` with better error extraction and logging, and implement critical fallback mechanisms in the `detect-location` function's rate limit cleanup. Update CORS configuration in `upload-image` function for stricter origin checks and better security. Replit-Commit-Author: Agent Replit-Commit-Session-Id: f4df1950-6410-48d0-b2de-f4096732504b Replit-Commit-Checkpoint-Type: intermediate_checkpoint
This commit is contained in:
@@ -45,15 +45,22 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
|
||||
// Use a request counter to track the latest fetch and prevent race conditions
|
||||
const requestCounterRef = useRef(0);
|
||||
|
||||
// Request counter for fetchFieldHistory
|
||||
const fieldHistoryRequestCounterRef = useRef(0);
|
||||
|
||||
const fetchVersions = useCallback(async () => {
|
||||
if (!isMountedRef.current) return;
|
||||
|
||||
// Increment counter and capture the current request ID BEFORE try block
|
||||
const currentRequestId = ++requestCounterRef.current;
|
||||
|
||||
try {
|
||||
if (!isMountedRef.current) return;
|
||||
|
||||
// Increment counter and capture the current request ID
|
||||
const currentRequestId = ++requestCounterRef.current;
|
||||
|
||||
setLoading(true);
|
||||
// Only set loading if this is still the latest request
|
||||
if (isMountedRef.current && currentRequestId === requestCounterRef.current) {
|
||||
setLoading(true);
|
||||
}
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('entity_versions')
|
||||
@@ -64,8 +71,8 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
// Only continue if this is still the latest request
|
||||
if (currentRequestId !== requestCounterRef.current) return;
|
||||
// Only continue if this is still the latest request and component is mounted
|
||||
if (!isMountedRef.current || currentRequestId !== requestCounterRef.current) return;
|
||||
|
||||
// Safety check: verify data is an array before processing
|
||||
if (!Array.isArray(data)) {
|
||||
@@ -84,8 +91,8 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
.select('user_id, username, avatar_url')
|
||||
.in('user_id', userIds);
|
||||
|
||||
// Check again if this is still the latest request
|
||||
if (currentRequestId !== requestCounterRef.current) return;
|
||||
// Check again if this is still the latest request and component is mounted
|
||||
if (!isMountedRef.current || currentRequestId !== requestCounterRef.current) return;
|
||||
|
||||
// Safety check: verify profiles array exists before filtering
|
||||
const profilesArray = Array.isArray(profiles) ? profiles : [];
|
||||
@@ -109,8 +116,10 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching versions:', error);
|
||||
if (isMountedRef.current) {
|
||||
// Safe error message access with fallback
|
||||
|
||||
// Use the captured currentRequestId (DO NOT re-read requestCounterRef.current)
|
||||
// Only update state if component is mounted and this is still the latest request
|
||||
if (isMountedRef.current && currentRequestId === requestCounterRef.current) {
|
||||
const errorMessage = error?.message || 'Failed to load version history';
|
||||
toast.error(errorMessage);
|
||||
setLoading(false);
|
||||
@@ -119,6 +128,11 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
}, [entityType, entityId]);
|
||||
|
||||
const fetchFieldHistory = async (versionId: string) => {
|
||||
if (!isMountedRef.current) return;
|
||||
|
||||
// Increment counter and capture the current request ID BEFORE try block
|
||||
const currentRequestId = ++fieldHistoryRequestCounterRef.current;
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('entity_field_history')
|
||||
@@ -128,14 +142,17 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
if (isMountedRef.current) {
|
||||
// Safety check: ensure data is an array
|
||||
// Only update state if component is mounted and this is still the latest request
|
||||
if (isMountedRef.current && currentRequestId === fieldHistoryRequestCounterRef.current) {
|
||||
const fieldChanges = Array.isArray(data) ? data as FieldChange[] : [];
|
||||
setFieldHistory(fieldChanges);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching field history:', error);
|
||||
if (isMountedRef.current) {
|
||||
|
||||
// Use the captured currentRequestId (DO NOT re-read fieldHistoryRequestCounterRef.current)
|
||||
// Only show error if component is mounted and this is still the latest request
|
||||
if (isMountedRef.current && currentRequestId === fieldHistoryRequestCounterRef.current) {
|
||||
const errorMessage = error?.message || 'Failed to load field history';
|
||||
toast.error(errorMessage);
|
||||
}
|
||||
@@ -164,6 +181,8 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
|
||||
const rollbackToVersion = async (targetVersionId: string, reason: string) => {
|
||||
try {
|
||||
if (!isMountedRef.current) return null;
|
||||
|
||||
const { data: userData } = await supabase.auth.getUser();
|
||||
if (!userData.user) throw new Error('Not authenticated');
|
||||
|
||||
@@ -194,6 +213,8 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
|
||||
const createVersion = async (versionData: any, changeReason?: string, submissionId?: string) => {
|
||||
try {
|
||||
if (!isMountedRef.current) return null;
|
||||
|
||||
const { data: userData } = await supabase.auth.getUser();
|
||||
if (!userData.user) throw new Error('Not authenticated');
|
||||
|
||||
@@ -273,7 +294,7 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
channelRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [entityType, entityId]);
|
||||
}, [entityType, entityId, fetchVersions]);
|
||||
|
||||
// Set mounted ref on mount and cleanup on unmount
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user