Improve image handling, optimize hooks, and add rate limiting

This commit introduces several improvements:
- Enhances `RideModelCard` by safely accessing and displaying ride count and image data, preventing potential errors.
- Refactors `useEntityVersions` and `useSearch` hooks to use `useCallback` and improve performance and prevent race conditions.
- Introduces a `MAX_MAP_SIZE` and cleanup mechanism for the rate limiting map in `detect-location` Supabase function to prevent memory leaks.
- Adds robust error handling and cleanup for image uploads in `uploadPendingImages`.
- Modifies `ManufacturerModels` to correctly map and display ride counts.
- Includes error handling for topological sort in `process-selective-approval` Supabase function.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 39bb006b-d046-477f-a1f9-b7821836f3a1
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
This commit is contained in:
pac7
2025-10-08 17:55:37 +00:00
parent b17d36de03
commit 0f2219f849
8 changed files with 165 additions and 76 deletions

View File

@@ -1,4 +1,4 @@
import { useState, useEffect, useRef } from 'react';
import { useState, useEffect, useRef, useCallback } from 'react';
import { supabase } from '@/integrations/supabase/client';
import { toast } from 'sonner';
@@ -42,10 +42,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);
const fetchVersions = async () => {
const fetchVersions = useCallback(async () => {
try {
if (!isMountedRef.current) return;
if (!isMountedRef.current || fetchInProgressRef.current) return;
fetchInProgressRef.current = true;
setLoading(true);
@@ -87,11 +92,12 @@ export function useEntityVersions(entityType: string, entityId: string) {
toast.error('Failed to load version history');
}
} finally {
fetchInProgressRef.current = false;
if (isMountedRef.current) {
setLoading(false);
}
}
};
}, [entityType, entityId]);
const fetchFieldHistory = async (versionId: string) => {
try {
@@ -195,7 +201,7 @@ export function useEntityVersions(entityType: string, entityId: string) {
if (entityType && entityId) {
fetchVersions();
}
}, [entityType, entityId]);
}, [entityType, entityId, fetchVersions]);
// Set up realtime subscription for version changes
useEffect(() => {