Fix: Resolve React rendering errors

The AI will address two runtime errors: a minified React error (likely related to incorrect component usage or props) and a "too many re-renders" error (indicating an infinite loop). The AI will analyze the code, particularly the `WhatsApp` component and its related hooks and components, to identify and fix the root causes of these errors.
This commit is contained in:
gpt-engineer-app[bot] 2025-05-19 23:09:26 +00:00
parent f476c1422b
commit 9cd9dcca3a
2 changed files with 32 additions and 20 deletions

View File

@ -14,6 +14,7 @@ export const useWhatsAppInstances = () => {
const [instances, setInstances] = useState<WhatsAppInstance[]>([]); const [instances, setInstances] = useState<WhatsAppInstance[]>([]);
const [currentUserId, setCurrentUserId] = useState<string>(''); const [currentUserId, setCurrentUserId] = useState<string>('');
const [isRefreshing, setIsRefreshing] = useState(false); const [isRefreshing, setIsRefreshing] = useState(false);
const [isCheckingStatus, setIsCheckingStatus] = useState(false);
// Get current user ID and load instances on component mount // Get current user ID and load instances on component mount
useEffect(() => { useEffect(() => {
@ -38,9 +39,10 @@ export const useWhatsAppInstances = () => {
// Function to check connection status for all instances // Function to check connection status for all instances
const checkAllInstancesStatus = useCallback(async () => { const checkAllInstancesStatus = useCallback(async () => {
if (instances.length === 0) return; if (instances.length === 0 || isCheckingStatus) return;
console.log(`Checking connection status for ${instances.length} instances`); console.log(`Checking connection status for ${instances.length} instances`);
setIsCheckingStatus(true);
try { try {
const updatedInstances = await Promise.all( const updatedInstances = await Promise.all(
@ -96,8 +98,10 @@ export const useWhatsAppInstances = () => {
} }
} catch (error) { } catch (error) {
console.error("Error checking instances status:", error); console.error("Error checking instances status:", error);
} finally {
setIsCheckingStatus(false);
} }
}, [instances]); }, [instances, isCheckingStatus]);
// Handler para atualizar a lista de instâncias do servidor // Handler para atualizar a lista de instâncias do servidor
const refreshInstances = async () => { const refreshInstances = async () => {
@ -185,8 +189,6 @@ export const useWhatsAppInstances = () => {
}); });
} finally { } finally {
setIsRefreshing(false); setIsRefreshing(false);
// Verificar status após atualizar a lista
await checkAllInstancesStatus();
} }
}; };

View File

@ -37,9 +37,13 @@ const WhatsApp = () => {
useEffect(() => { useEffect(() => {
console.log("Setting up periodic status checks, current instances:", instances.length); console.log("Setting up periodic status checks, current instances:", instances.length);
// Check status initially // Check status initially after a short delay to prevent immediate execution
let initialCheck: NodeJS.Timeout;
if (instances.length > 0) { if (instances.length > 0) {
initialCheck = setTimeout(() => {
console.log("Running initial status check");
checkAllInstancesStatus(); checkAllInstancesStatus();
}, 1000);
} }
// Set up interval for periodic checks (every 30 seconds) // Set up interval for periodic checks (every 30 seconds)
@ -50,16 +54,18 @@ const WhatsApp = () => {
} }
}, 30000); // 30 seconds }, 30000); // 30 seconds
// Clean up interval when component unmounts // Clean up interval and timeout when component unmounts
return () => { return () => {
if (interval) {
clearInterval(interval); clearInterval(interval);
if (initialCheck) {
clearTimeout(initialCheck);
} }
}; };
}, [instances.length, checkAllInstancesStatus]); }, [instances.length, checkAllInstancesStatus]);
// Run refresh instances on initial load to get server instances // Run refresh instances on initial load to get server instances
useEffect(() => { useEffect(() => {
// Only run on first render
const initialLoad = async () => { const initialLoad = async () => {
if (instances.length === 0) { if (instances.length === 0) {
try { try {
@ -71,6 +77,8 @@ const WhatsApp = () => {
}; };
initialLoad(); initialLoad();
// This effect should only run once when the component mounts
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
// Handler for when a new instance is created // Handler for when a new instance is created
@ -83,7 +91,8 @@ const WhatsApp = () => {
handleViewQrCode(newInstance); handleViewQrCode(newInstance);
} }
// Trigger a status check for all instances // Trigger a status check for all instances with a delay
setTimeout(async () => {
try { try {
await checkAllInstancesStatus(); await checkAllInstancesStatus();
} catch (error) { } catch (error) {
@ -94,6 +103,7 @@ const WhatsApp = () => {
variant: "default", variant: "default",
}); });
} }
}, 2000);
}; };
// Handler for when an instance is deleted // Handler for when an instance is deleted