Add instance connection status check
Implement automatic connection status checks for each instance card, displaying "Conectado" or "Desconectado" based on the API response. Includes periodic updates and visual status indication.
This commit is contained in:
parent
e7b86b26ea
commit
44e8569b6c
@ -9,6 +9,7 @@ import { Label } from '@/components/ui/label';
|
|||||||
import { MessageCircle, QrCode, RefreshCw, Smartphone } from 'lucide-react';
|
import { MessageCircle, QrCode, RefreshCw, Smartphone } from 'lucide-react';
|
||||||
import { Alert, AlertDescription } from '@/components/ui/alert';
|
import { Alert, AlertDescription } from '@/components/ui/alert';
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
||||||
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
|
||||||
interface WhatsAppInstance {
|
interface WhatsAppInstance {
|
||||||
instanceName: string;
|
instanceName: string;
|
||||||
@ -16,6 +17,7 @@ interface WhatsAppInstance {
|
|||||||
phoneNumber: string;
|
phoneNumber: string;
|
||||||
status?: string;
|
status?: string;
|
||||||
qrcode?: string;
|
qrcode?: string;
|
||||||
|
connectionState?: 'open' | 'closed' | 'connecting';
|
||||||
}
|
}
|
||||||
|
|
||||||
const WhatsApp = () => {
|
const WhatsApp = () => {
|
||||||
@ -33,6 +35,7 @@ const WhatsApp = () => {
|
|||||||
const [qrCodeData, setQrCodeData] = useState<string | null>(null);
|
const [qrCodeData, setQrCodeData] = useState<string | null>(null);
|
||||||
const [qrError, setQrError] = useState<string | null>(null);
|
const [qrError, setQrError] = useState<string | null>(null);
|
||||||
const [qrDialogOpen, setQrDialogOpen] = useState(false);
|
const [qrDialogOpen, setQrDialogOpen] = useState(false);
|
||||||
|
const [statusCheckInterval, setStatusCheckInterval] = useState<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
// Load saved instances from localStorage on component mount
|
// Load saved instances from localStorage on component mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -53,6 +56,72 @@ const WhatsApp = () => {
|
|||||||
}
|
}
|
||||||
}, [instances]);
|
}, [instances]);
|
||||||
|
|
||||||
|
// Set up periodic status checks
|
||||||
|
useEffect(() => {
|
||||||
|
// Check status initially
|
||||||
|
if (instances.length > 0) {
|
||||||
|
checkAllInstancesStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up interval for periodic checks (every 60 seconds)
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
if (instances.length > 0) {
|
||||||
|
checkAllInstancesStatus();
|
||||||
|
}
|
||||||
|
}, 60000); // 60 seconds
|
||||||
|
|
||||||
|
setStatusCheckInterval(interval);
|
||||||
|
|
||||||
|
// Clean up interval when component unmounts
|
||||||
|
return () => {
|
||||||
|
if (interval) {
|
||||||
|
clearInterval(interval);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [instances.length]);
|
||||||
|
|
||||||
|
// Function to check connection status for all instances
|
||||||
|
const checkAllInstancesStatus = async () => {
|
||||||
|
const updatedInstances = await Promise.all(
|
||||||
|
instances.map(async (instance) => {
|
||||||
|
try {
|
||||||
|
const state = await fetchConnectionState(instance.instanceName);
|
||||||
|
return { ...instance, connectionState: state };
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error checking status for ${instance.instanceName}:`, error);
|
||||||
|
return { ...instance, connectionState: 'closed' };
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
setInstances(updatedInstances);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Function to fetch connection state for an instance
|
||||||
|
const fetchConnectionState = async (instanceName: string): Promise<'open' | 'closed' | 'connecting'> => {
|
||||||
|
try {
|
||||||
|
const serverUrl = "evolutionapi2.innova1001.com.br";
|
||||||
|
const apiKey = "beeb77fbd7f48f91db2cd539a573c130";
|
||||||
|
|
||||||
|
const response = await fetch(`https://${serverUrl}/instance/connectionState/${encodeURIComponent(instanceName)}`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'apikey': apiKey
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return 'closed';
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.instance?.state || 'closed';
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching connection state:", error);
|
||||||
|
return 'closed';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const createInstance = async () => {
|
const createInstance = async () => {
|
||||||
// Validate form fields
|
// Validate form fields
|
||||||
if (!instanceName.trim()) {
|
if (!instanceName.trim()) {
|
||||||
@ -103,10 +172,11 @@ const WhatsApp = () => {
|
|||||||
// Create new instance object
|
// Create new instance object
|
||||||
const newInstance: WhatsAppInstance = {
|
const newInstance: WhatsAppInstance = {
|
||||||
instanceName,
|
instanceName,
|
||||||
instanceId: data.instanceId,
|
instanceId: data.instance.instanceId,
|
||||||
phoneNumber,
|
phoneNumber,
|
||||||
status: data.status,
|
status: data.instance.status,
|
||||||
qrcode: data.qrcode
|
qrcode: data.qrcode,
|
||||||
|
connectionState: 'connecting'
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add new instance to the list
|
// Add new instance to the list
|
||||||
@ -124,9 +194,12 @@ const WhatsApp = () => {
|
|||||||
// If there's a QR code in the response, show it
|
// If there's a QR code in the response, show it
|
||||||
if (data.qrcode) {
|
if (data.qrcode) {
|
||||||
setActiveInstance(newInstance);
|
setActiveInstance(newInstance);
|
||||||
setQrCodeData(data.qrcode);
|
setQrCodeData(data.qrcode.base64);
|
||||||
setQrDialogOpen(true);
|
setQrDialogOpen(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trigger a status check for all instances
|
||||||
|
checkAllInstancesStatus();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error creating WhatsApp instance:", error);
|
console.error("Error creating WhatsApp instance:", error);
|
||||||
toast({
|
toast({
|
||||||
@ -269,13 +342,20 @@ const WhatsApp = () => {
|
|||||||
<Smartphone className="h-4 w-4 mr-2 text-gray-500" />
|
<Smartphone className="h-4 w-4 mr-2 text-gray-500" />
|
||||||
<span>{instance.phoneNumber}</span>
|
<span>{instance.phoneNumber}</span>
|
||||||
</div>
|
</div>
|
||||||
{instance.status && (
|
|
||||||
<div className="flex items-center text-sm">
|
<div className="flex items-center text-sm">
|
||||||
<span className="inline-block w-2 h-2 rounded-full bg-green-500 mr-2"></span>
|
{instance.connectionState === 'open' ? (
|
||||||
<span>Status: {instance.status}</span>
|
<Badge variant="success" className="flex items-center gap-1">
|
||||||
</div>
|
<span className="inline-block w-2 h-2 rounded-full bg-green-500"></span>
|
||||||
|
Status: Conectado
|
||||||
|
</Badge>
|
||||||
|
) : (
|
||||||
|
<Badge variant="outline" className="flex items-center gap-1 text-red-500 border-red-300">
|
||||||
|
<span className="inline-block w-2 h-2 rounded-full bg-red-500"></span>
|
||||||
|
Status: Desconectado
|
||||||
|
</Badge>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
<CardFooter className="flex justify-between">
|
<CardFooter className="flex justify-between">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user