Fix: Resolve React error #301
The error is a minified React error, likely caused by a problem with the component's rendering. The error occurs on the /whatsapp route.
This commit is contained in:
parent
57d71c73af
commit
66e18c3988
@ -11,6 +11,7 @@ interface ProtectedRouteProps {
|
|||||||
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
|
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [shouldShowToast, setShouldShowToast] = useState(false);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
@ -30,10 +31,12 @@ const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
|||||||
} else {
|
} else {
|
||||||
console.log('Nenhuma sessão encontrada, redirecionando para login');
|
console.log('Nenhuma sessão encontrada, redirecionando para login');
|
||||||
setIsAuthenticated(false);
|
setIsAuthenticated(false);
|
||||||
|
setShouldShowToast(true); // Mark that we should show toast, but don't do it here
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erro ao verificar autenticação:', error);
|
console.error('Erro ao verificar autenticação:', error);
|
||||||
setIsAuthenticated(false);
|
setIsAuthenticated(false);
|
||||||
|
setShouldShowToast(true);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
@ -42,6 +45,17 @@ const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
|||||||
checkAuthentication();
|
checkAuthentication();
|
||||||
}, []); // Execute only on mount
|
}, []); // Execute only on mount
|
||||||
|
|
||||||
|
// Show toast in a separate useEffect to avoid re-render loops
|
||||||
|
useEffect(() => {
|
||||||
|
if (shouldShowToast) {
|
||||||
|
toast({
|
||||||
|
title: "Autenticação necessária",
|
||||||
|
description: "Por favor, faça login para acessar esta página"
|
||||||
|
});
|
||||||
|
setShouldShowToast(false); // Reset the flag after showing toast
|
||||||
|
}
|
||||||
|
}, [shouldShowToast, toast]);
|
||||||
|
|
||||||
// Se estiver carregando, mostrar estado de carregamento
|
// Se estiver carregando, mostrar estado de carregamento
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return (
|
||||||
@ -53,11 +67,6 @@ const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
|||||||
|
|
||||||
// Se não estiver autenticado, redirecionar para a página de login
|
// Se não estiver autenticado, redirecionar para a página de login
|
||||||
if (!isAuthenticated) {
|
if (!isAuthenticated) {
|
||||||
// Use toast here - it's safer after render and won't cause infinite re-renders
|
|
||||||
toast({
|
|
||||||
title: "Autenticação necessária",
|
|
||||||
description: "Por favor, faça login para acessar esta página"
|
|
||||||
});
|
|
||||||
return <Navigate to="/auth" state={{ from: location }} replace />;
|
return <Navigate to="/auth" state={{ from: location }} replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,6 +22,8 @@ export const useWhatsAppInstance = (
|
|||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [instanceFound, setInstanceFound] = useState(false);
|
const [instanceFound, setInstanceFound] = useState(false);
|
||||||
|
const [shouldShowToast, setShouldShowToast] = useState(false);
|
||||||
|
const [toastMessage, setToastMessage] = useState({ title: '', description: '', variant: '' as any });
|
||||||
|
|
||||||
// Function to fetch specific instance by name
|
// Function to fetch specific instance by name
|
||||||
const fetchInstanceByName = async () => {
|
const fetchInstanceByName = async () => {
|
||||||
@ -58,10 +60,14 @@ export const useWhatsAppInstance = (
|
|||||||
console.log('Found instance:', foundInstance);
|
console.log('Found instance:', foundInstance);
|
||||||
addInstance(foundInstance);
|
addInstance(foundInstance);
|
||||||
setInstanceFound(true);
|
setInstanceFound(true);
|
||||||
toast({
|
|
||||||
|
// Set toast data instead of calling toast directly
|
||||||
|
setToastMessage({
|
||||||
title: "Instância encontrada",
|
title: "Instância encontrada",
|
||||||
description: `A instância ${instanceName} foi localizada no servidor.`
|
description: `A instância ${instanceName} foi localizada no servidor.`,
|
||||||
|
variant: "default"
|
||||||
});
|
});
|
||||||
|
setShouldShowToast(true);
|
||||||
} else {
|
} else {
|
||||||
setInstanceFound(false);
|
setInstanceFound(false);
|
||||||
console.log(`Instance ${instanceName} not found`);
|
console.log(`Instance ${instanceName} not found`);
|
||||||
@ -69,16 +75,31 @@ export const useWhatsAppInstance = (
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching specific instance:', error);
|
console.error('Error fetching specific instance:', error);
|
||||||
setInstanceFound(false);
|
setInstanceFound(false);
|
||||||
toast({
|
|
||||||
|
// Set toast data instead of calling toast directly
|
||||||
|
setToastMessage({
|
||||||
title: "Instância não encontrada",
|
title: "Instância não encontrada",
|
||||||
description: "Não foi possível encontrar a instância com este nome.",
|
description: "Não foi possível encontrar a instância com este nome.",
|
||||||
variant: "destructive"
|
variant: "destructive"
|
||||||
});
|
});
|
||||||
|
setShouldShowToast(true);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Show toast in a separate useEffect to avoid re-render loops
|
||||||
|
useEffect(() => {
|
||||||
|
if (shouldShowToast) {
|
||||||
|
toast({
|
||||||
|
title: toastMessage.title,
|
||||||
|
description: toastMessage.description,
|
||||||
|
variant: toastMessage.variant
|
||||||
|
});
|
||||||
|
setShouldShowToast(false);
|
||||||
|
}
|
||||||
|
}, [shouldShowToast, toastMessage, toast]);
|
||||||
|
|
||||||
// Save instance name to localStorage
|
// Save instance name to localStorage
|
||||||
const saveInstanceName = (name: string) => {
|
const saveInstanceName = (name: string) => {
|
||||||
if (currentUserId) {
|
if (currentUserId) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user