mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 13:31:13 -05:00
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:
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { usernameSchema } from '@/lib/validation';
|
||||
import { useDebounce } from './useDebounce';
|
||||
@@ -20,6 +20,33 @@ export function useUsernameValidation(username: string, currentUsername?: string
|
||||
|
||||
const debouncedUsername = useDebounce(username, 500);
|
||||
|
||||
const checkUsernameAvailability = useCallback(async (normalizedUsername: string) => {
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('profiles')
|
||||
.select('username')
|
||||
.eq('username', normalizedUsername)
|
||||
.maybeSingle();
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
const isAvailable = !data;
|
||||
setState({
|
||||
isValid: isAvailable,
|
||||
isAvailable,
|
||||
isChecking: false,
|
||||
error: isAvailable ? null : 'Username is already taken',
|
||||
});
|
||||
} catch (error) {
|
||||
setState({
|
||||
isValid: false,
|
||||
isAvailable: null,
|
||||
isChecking: false,
|
||||
error: 'Error checking username availability',
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!debouncedUsername || debouncedUsername === currentUsername) {
|
||||
setState({
|
||||
@@ -47,34 +74,7 @@ export function useUsernameValidation(username: string, currentUsername?: string
|
||||
setState(prev => ({ ...prev, isChecking: true, error: null }));
|
||||
|
||||
checkUsernameAvailability(validation.data);
|
||||
}, [debouncedUsername, currentUsername]);
|
||||
|
||||
const checkUsernameAvailability = async (normalizedUsername: string) => {
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('profiles')
|
||||
.select('username')
|
||||
.eq('username', normalizedUsername)
|
||||
.maybeSingle();
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
const isAvailable = !data;
|
||||
setState({
|
||||
isValid: isAvailable,
|
||||
isAvailable,
|
||||
isChecking: false,
|
||||
error: isAvailable ? null : 'Username is already taken',
|
||||
});
|
||||
} catch (error) {
|
||||
setState({
|
||||
isValid: false,
|
||||
isAvailable: null,
|
||||
isChecking: false,
|
||||
error: 'Error checking username availability',
|
||||
});
|
||||
}
|
||||
};
|
||||
}, [debouncedUsername, currentUsername, checkUsernameAvailability]);
|
||||
|
||||
return state;
|
||||
}
|
||||
Reference in New Issue
Block a user