import { useState } from "react"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { useGetCommandsByTypes } from "@/hooks/queries/useCommandQueries"; import { useSendCommand } from "@/hooks/queries"; import { CommandType } from "@/types/command-registry"; import { Power, PowerOff, XCircle, ShieldBan, ChevronDown, Loader2, AlertTriangle } from "lucide-react"; import { toast } from "sonner"; interface CommandActionButtonsProps { roomName: string; selectedDevices?: string[]; // Các thiết bị đã chọn } const COMMAND_TYPE_CONFIG = { [CommandType.RESTART]: { label: "Khởi động lại", icon: Power, color: "text-blue-600", bgColor: "bg-blue-50 hover:bg-blue-100", }, [CommandType.SHUTDOWN]: { label: "Tắt máy", icon: PowerOff, color: "text-red-600", bgColor: "bg-red-50 hover:bg-red-100", }, [CommandType.TASKKILL]: { label: "Kết thúc tác vụ", icon: XCircle, color: "text-orange-600", bgColor: "bg-orange-50 hover:bg-orange-100", }, [CommandType.BLOCK]: { label: "Chặn", icon: ShieldBan, color: "text-purple-600", bgColor: "bg-purple-50 hover:bg-purple-100", }, }; export function CommandActionButtons({ roomName, selectedDevices = [] }: CommandActionButtonsProps) { const [confirmDialog, setConfirmDialog] = useState<{ open: boolean; command: any; commandType: CommandType; }>({ open: false, command: null, commandType: CommandType.RESTART, }); const [isExecuting, setIsExecuting] = useState(false); // Query commands for each type const { data: restartCommands = [] } = useGetCommandsByTypes(CommandType.RESTART.toString()); const { data: shutdownCommands = [] } = useGetCommandsByTypes(CommandType.SHUTDOWN.toString()); const { data: taskkillCommands = [] } = useGetCommandsByTypes(CommandType.TASKKILL.toString()); const { data: blockCommands = [] } = useGetCommandsByTypes(CommandType.BLOCK.toString()); // Send command mutation const sendCommandMutation = useSendCommand(); const commandsByType = { [CommandType.RESTART]: restartCommands, [CommandType.SHUTDOWN]: shutdownCommands, [CommandType.TASKKILL]: taskkillCommands, [CommandType.BLOCK]: blockCommands, }; const handleCommandClick = (command: any, commandType: CommandType) => { setConfirmDialog({ open: true, command, commandType, }); }; const handleConfirmExecute = async () => { setIsExecuting(true); try { // Chuẩn bị data theo format API (PascalCase) const apiData = { Command: confirmDialog.command.commandContent, QoS: confirmDialog.command.qoS, IsRetained: confirmDialog.command.isRetained, }; // Gửi lệnh đến phòng await sendCommandMutation.mutateAsync({ roomName, data: apiData as any, }); toast.success(`Đã gửi lệnh: ${confirmDialog.command.commandName}`); setConfirmDialog({ open: false, command: null, commandType: CommandType.RESTART }); // Reload page để tránh freeze setTimeout(() => { window.location.reload(); }, 500); } catch (error) { console.error("Execute command error:", error); toast.error("Lỗi khi gửi lệnh!"); setIsExecuting(false); } }; const handleCloseDialog = () => { if (!isExecuting) { setConfirmDialog({ open: false, command: null, commandType: CommandType.RESTART }); // Reload để tránh freeze setTimeout(() => { window.location.reload(); }, 300); } }; const renderCommandButton = (commandType: CommandType) => { const config = COMMAND_TYPE_CONFIG[commandType]; const commands = commandsByType[commandType]; const Icon = config.icon; if (!commands || commands.length === 0) { return ( ); } return ( {commands.map((command: any) => ( handleCommandClick(command, commandType)} className="cursor-pointer" >
{command.commandName} {command.description && ( {command.description} )}
))}
); }; return ( <>
{Object.values(CommandType) .filter((value) => typeof value === "number") .map((commandType) => renderCommandButton(commandType as CommandType))}
{/* Confirm Dialog */} Xác nhận thực thi lệnh

Bạn có chắc chắn muốn thực thi lệnh {confirmDialog.command?.commandName}?

{confirmDialog.command?.description && (

{confirmDialog.command.description}

)}

Phòng: {roomName}

Loại lệnh:{" "} {COMMAND_TYPE_CONFIG[confirmDialog.commandType]?.label}

{selectedDevices.length > 0 && (

Thiết bị đã chọn:{" "} {selectedDevices.length} thiết bị

)}

Lệnh sẽ được thực thi ngay lập tức và không thể hoàn tác.

); }