mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 07:31:12 -05:00
Improve form validation and image handling for entities
Refactor validation logic for 'founded_year' in multiple form components, enhance image cleanup in `EntityMultiImageUploader`, update `useEntityVersions` to prevent race conditions, improve error handling for recent searches in `useSearch`, refine rate limiting logic in `detect-location` Supabase function, and update CORS configuration for `upload-image` Supabase function. Replit-Commit-Author: Agent Replit-Commit-Session-Id: b9af4867-23a7-43cc-baeb-4a97f66b4150 Replit-Commit-Checkpoint-Type: intermediate_checkpoint
This commit is contained in:
@@ -43,14 +43,15 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
// Track the current channel to prevent duplicate subscriptions
|
||||
const channelRef = useRef<ReturnType<typeof supabase.channel> | null>(null);
|
||||
|
||||
// Track if a fetch is in progress to prevent race conditions
|
||||
const fetchInProgressRef = useRef(false);
|
||||
// Use a request counter to track the latest fetch and prevent race conditions
|
||||
const requestCounterRef = useRef(0);
|
||||
|
||||
const fetchVersions = useCallback(async () => {
|
||||
try {
|
||||
if (!isMountedRef.current || fetchInProgressRef.current) return;
|
||||
if (!isMountedRef.current) return;
|
||||
|
||||
fetchInProgressRef.current = true;
|
||||
// Increment counter and capture the current request ID
|
||||
const currentRequestId = ++requestCounterRef.current;
|
||||
|
||||
setLoading(true);
|
||||
|
||||
@@ -63,6 +64,9 @@ 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;
|
||||
|
||||
// Fetch profiles separately
|
||||
const userIds = [...new Set(data?.map(v => v.changed_by).filter(Boolean) || [])];
|
||||
const { data: profiles } = await supabase
|
||||
@@ -70,6 +74,9 @@ 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;
|
||||
|
||||
const versionsWithProfiles = data?.map(v => {
|
||||
const profile = profiles?.find(p => p.user_id === v.changed_by);
|
||||
return {
|
||||
@@ -81,19 +88,16 @@ export function useEntityVersions(entityType: string, entityId: string) {
|
||||
};
|
||||
}) as EntityVersion[];
|
||||
|
||||
// Only update state if component is still mounted
|
||||
if (isMountedRef.current) {
|
||||
// Only update state if component is still mounted and this is still the latest request
|
||||
if (isMountedRef.current && currentRequestId === requestCounterRef.current) {
|
||||
setVersions(versionsWithProfiles || []);
|
||||
setCurrentVersion(versionsWithProfiles?.find(v => v.is_current) || null);
|
||||
setLoading(false);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching versions:', error);
|
||||
if (isMountedRef.current) {
|
||||
toast.error('Failed to load version history');
|
||||
}
|
||||
} finally {
|
||||
fetchInProgressRef.current = false;
|
||||
if (isMountedRef.current) {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user