diff --git a/src/components/whatsapp/QrCodeDialog.tsx b/src/components/whatsapp/QrCodeDialog.tsx index 1270559..2011827 100644 --- a/src/components/whatsapp/QrCodeDialog.tsx +++ b/src/components/whatsapp/QrCodeDialog.tsx @@ -1,5 +1,5 @@ -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { Dialog, DialogContent, @@ -9,7 +9,7 @@ import { } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Alert, AlertDescription } from '@/components/ui/alert'; -import { RefreshCw } from 'lucide-react'; +import { RefreshCw, ScanQrCode } from 'lucide-react'; import { WhatsAppInstance } from '@/types/whatsAppTypes'; import { fetchQrCode } from '@/services/whatsAppService'; import { useToast } from '@/hooks/use-toast'; @@ -32,6 +32,17 @@ const QrCodeDialog = ({ const [qrCodeData, setQrCodeData] = useState(null); const [qrError, setQrError] = useState(null); + // Auto-fetch QR code when dialog opens with an active instance + useEffect(() => { + if (open && activeInstance) { + handleRefreshQrCode(); + } else { + // Clear QR code data when dialog closes + setQrCodeData(null); + setQrError(null); + } + }, [open, activeInstance]); + const handleOpenChange = (newOpen: boolean) => { onOpenChange(newOpen); @@ -55,11 +66,14 @@ const QrCodeDialog = ({ if (data && data.base64) { // Save the base64 image data directly - it already contains the data:image prefix setQrCodeData(data.base64); + setQrError(null); } else { + setQrCodeData(null); setQrError("QR Code não disponível. A instância pode já estar conectada ou houve um erro na API."); } } catch (error) { console.error("Error fetching QR code:", error); + setQrCodeData(null); setQrError("Falha ao obter QR Code. Verifique a conexão ou tente novamente mais tarde."); toast({ title: "Erro ao obter QR Code", @@ -82,7 +96,8 @@ const QrCodeDialog = ({
{loadingQR && ( -
+
+

Carregando QR Code...

)} @@ -90,15 +105,20 @@ const QrCodeDialog = ({ {qrCodeData && !loadingQR && (
- QR Code WhatsApp + {qrCodeData.startsWith('data:image/') ? ( + QR Code WhatsApp + ) : ( +
Formato de QR Code inválido
+ )} +
+
+ +

Escaneie este QR Code com seu WhatsApp para finalizar a conexão

-

- Escaneie este QR Code com seu WhatsApp para finalizar a conexão. -

)} @@ -116,8 +136,8 @@ const QrCodeDialog = ({ disabled={loadingQR} className="flex items-center" > - - Atualizar QR Code + + {loadingQR ? 'Atualizando...' : 'Atualizar QR Code'} )}