From 4da7b28d1ae4d9bd3f3795d0c2a4592e8e543e78 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Tue, 20 May 2025 17:03:04 +0000 Subject: [PATCH] Refactor: Split InstanceCard into smaller components Refactors the InstanceCard component into smaller, more manageable components to improve code organization and readability. This includes extracting parts of the UI and logic into separate components. --- .../whatsapp/ConfirmationDialog.tsx | 55 +++++++ src/components/whatsapp/InstanceActions.tsx | 104 +++++++++++++ src/components/whatsapp/InstanceCard.tsx | 146 ++++-------------- src/components/whatsapp/LoadingState.tsx | 26 +++- src/components/whatsapp/StatusBadge.tsx | 34 ++++ src/hooks/whatsApp/usePeriodicStatusCheck.ts | 21 ++- 6 files changed, 256 insertions(+), 130 deletions(-) create mode 100644 src/components/whatsapp/ConfirmationDialog.tsx create mode 100644 src/components/whatsapp/InstanceActions.tsx create mode 100644 src/components/whatsapp/StatusBadge.tsx diff --git a/src/components/whatsapp/ConfirmationDialog.tsx b/src/components/whatsapp/ConfirmationDialog.tsx new file mode 100644 index 0000000..308336c --- /dev/null +++ b/src/components/whatsapp/ConfirmationDialog.tsx @@ -0,0 +1,55 @@ + +import React from "react"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from "@/components/ui/alert-dialog"; + +interface ConfirmationDialogProps { + open: boolean; + onOpenChange: (open: boolean) => void; + title: string; + description: string; + actionLabel: string; + onAction: () => Promise; + loading: boolean; +} + +const ConfirmationDialog = ({ + open, + onOpenChange, + title, + description, + actionLabel, + onAction, + loading, +}: ConfirmationDialogProps) => { + return ( + + + + {title} + {description} + + + Cancelar + + {loading ? "Processando..." : actionLabel} + + + + + ); +}; + +export default ConfirmationDialog; diff --git a/src/components/whatsapp/InstanceActions.tsx b/src/components/whatsapp/InstanceActions.tsx new file mode 100644 index 0000000..c15148a --- /dev/null +++ b/src/components/whatsapp/InstanceActions.tsx @@ -0,0 +1,104 @@ + +import React from "react"; +import { Button } from "@/components/ui/button"; +import { + QrCode, + RefreshCw, + PowerOff, + CircleDot, + CircleOff, + X +} from "lucide-react"; +import { WhatsAppInstance } from "@/types/whatsAppTypes"; + +interface InstanceActionsProps { + instance: WhatsAppInstance; + loading: string | null; + onViewQrCode: (instance: WhatsAppInstance) => void; + onRestart: () => void; + onLogout: () => void; + onDelete: () => void; + onSetOnline: () => void; + onSetOffline: () => void; +} + +const InstanceActions = ({ + instance, + loading, + onViewQrCode, + onRestart, + onLogout, + onDelete, + onSetOnline, + onSetOffline, +}: InstanceActionsProps) => { + return ( + <> +
+ + +
+
+ + +
+
+ + +
+ + ); +}; + +export default InstanceActions; diff --git a/src/components/whatsapp/InstanceCard.tsx b/src/components/whatsapp/InstanceCard.tsx index bca7224..d25ab80 100644 --- a/src/components/whatsapp/InstanceCard.tsx +++ b/src/components/whatsapp/InstanceCard.tsx @@ -1,28 +1,11 @@ import { useState } from 'react'; import { Card, CardHeader, CardTitle, CardContent, CardFooter } from '@/components/ui/card'; -import { Button } from '@/components/ui/button'; -import { Badge } from '@/components/ui/badge'; -import { - QrCode, - Smartphone, - RefreshCw, - X, - PowerOff, - CircleDot, - CircleOff -} from 'lucide-react'; +import { Smartphone } from 'lucide-react'; import { WhatsAppInstance } from '@/types/whatsAppTypes'; -import { - AlertDialog, - AlertDialogAction, - AlertDialogCancel, - AlertDialogContent, - AlertDialogDescription, - AlertDialogFooter, - AlertDialogHeader, - AlertDialogTitle, -} from "@/components/ui/alert-dialog"; +import StatusBadge from './StatusBadge'; +import InstanceActions from './InstanceActions'; +import ConfirmationDialog from './ConfirmationDialog'; interface InstanceCardProps { instance: WhatsAppInstance; @@ -175,112 +158,35 @@ const InstanceCard = ({ {instance.phoneNumber}
- {connectionStatus === 'open' ? ( - - - Status: Conectado - - ) : connectionStatus === 'connecting' ? ( - - - Status: Conectando - - ) : ( - - - Status: Desconectado - - )} +
-
- - -
-
- - -
-
- - -
+
- !open && setConfirmAction(null)}> - - - {confirmAction?.title} - - {confirmAction?.description} - - - - Cancelar - - {loading === confirmAction?.title ? "Processando..." : confirmAction?.actionLabel} - - - - + {confirmAction && ( + !open && setConfirmAction(null)} + title={confirmAction.title} + description={confirmAction.description} + actionLabel={confirmAction.actionLabel} + onAction={executeAction} + loading={loading !== null} + /> + )} ); }; diff --git a/src/components/whatsapp/LoadingState.tsx b/src/components/whatsapp/LoadingState.tsx index 024e498..c49d24a 100644 --- a/src/components/whatsapp/LoadingState.tsx +++ b/src/components/whatsapp/LoadingState.tsx @@ -3,13 +3,31 @@ import React from 'react'; interface LoadingStateProps { message?: string; + size?: 'small' | 'medium' | 'large'; + className?: string; } -const LoadingState = ({ message = "Buscando instância..." }: LoadingStateProps) => { +const LoadingState = ({ + message = "Buscando instância...", + size = 'medium', + className = '' +}: LoadingStateProps) => { + const spinnerSizes = { + small: 'h-6 w-6', + medium: 'h-12 w-12', + large: 'h-16 w-16' + }; + + const textSizes = { + small: 'text-sm', + medium: 'text-base', + large: 'text-lg' + }; + return ( -
-
-

{message}

+
+
+ {message &&

{message}

}
); }; diff --git a/src/components/whatsapp/StatusBadge.tsx b/src/components/whatsapp/StatusBadge.tsx new file mode 100644 index 0000000..1f25bc6 --- /dev/null +++ b/src/components/whatsapp/StatusBadge.tsx @@ -0,0 +1,34 @@ + +import React from "react"; +import { Badge } from "@/components/ui/badge"; + +interface StatusBadgeProps { + connectionStatus: string; +} + +const StatusBadge = ({ connectionStatus }: StatusBadgeProps) => { + if (connectionStatus === 'open') { + return ( + + + Status: Conectado + + ); + } else if (connectionStatus === 'connecting') { + return ( + + + Status: Conectando + + ); + } else { + return ( + + + Status: Desconectado + + ); + } +}; + +export default StatusBadge; diff --git a/src/hooks/whatsApp/usePeriodicStatusCheck.ts b/src/hooks/whatsApp/usePeriodicStatusCheck.ts index db9fc61..54e4ab2 100644 --- a/src/hooks/whatsApp/usePeriodicStatusCheck.ts +++ b/src/hooks/whatsApp/usePeriodicStatusCheck.ts @@ -1,10 +1,14 @@ -import { useEffect } from 'react'; +import { useEffect, useRef } from 'react'; export const usePeriodicStatusCheck = ( instancesCount: number, - checkAllInstancesStatus: () => Promise + checkAllInstancesStatus: () => Promise, + initialDelay: number = 1000, + checkInterval: number = 30000 ) => { + const isFirstRun = useRef(true); + // Set up periodic status checks useEffect(() => { console.log("Setting up periodic status checks, current instances:", instancesCount); @@ -15,16 +19,17 @@ export const usePeriodicStatusCheck = ( initialCheck = setTimeout(() => { console.log("Running initial status check"); checkAllInstancesStatus(); - }, 1000); + isFirstRun.current = false; + }, initialDelay); } - // Set up interval for periodic checks (every 30 seconds) + // Set up interval for periodic checks const interval = setInterval(() => { if (instancesCount > 0) { console.log("Running periodic status check"); checkAllInstancesStatus(); } - }, 30000); // 30 seconds + }, checkInterval); // Clean up interval and timeout when component unmounts return () => { @@ -33,5 +38,9 @@ export const usePeriodicStatusCheck = ( clearTimeout(initialCheck); } }; - }, [instancesCount, checkAllInstancesStatus]); + }, [instancesCount, checkAllInstancesStatus, initialDelay, checkInterval]); + + return { + isFirstRun: isFirstRun.current + }; };