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 { useGetSensitiveCommands, useExecuteSensitiveCommand } from "@/hooks/queries/useCommandQueries"; 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", }, [CommandType.RESET]: { label : "Reset", icon: Loader2, color: "text-green-600", bgColor: "bg-green-50 hover:bg-green-100", } }; export function CommandActionButtons({ roomName, selectedDevices = [] }: CommandActionButtonsProps) { const [confirmDialog, setConfirmDialog] = useState<{ open: boolean; command: any; commandType: CommandType; isSensitive?: boolean; }>({ open: false, command: null, commandType: CommandType.RESTART, }); const [isExecuting, setIsExecuting] = useState(false); const [sensitivePassword, setSensitivePassword] = useState(""); // Query commands for each type const { data: sensitiveCommands = [] } = useGetSensitiveCommands(); // Send command mutation (sensitive) const executeSensitiveMutation = useExecuteSensitiveCommand(); // Build commands mapped by CommandType using the `command` field from sensitive data const commandsByType: Record = (Object.values(CommandType) as Array) .filter((v) => typeof v === "number") .reduce((acc: Record, type) => { acc[type as number] = (sensitiveCommands || []).filter((c: any) => Number(c.command) === Number(type)); return acc; }, {} as Record); const handleCommandClick = (command: any, commandType: CommandType) => { // When building from sensitiveCommands, all items here are sensitive setConfirmDialog({ open: true, command, commandType, isSensitive: true, }); }; const handleConfirmExecute = async () => { setIsExecuting(true); try { // All rendered commands are sourced from sensitiveCommands — send via sensitive mutation await executeSensitiveMutation.mutateAsync({ roomName, command: confirmDialog.command.commandContent, password: sensitivePassword, }); toast.success(`Đã gửi lệnh: ${confirmDialog.command.commandName}`); setConfirmDialog({ open: false, command: null, commandType: CommandType.RESTART, isSensitive: false }); setSensitivePassword(""); // 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, isSensitive: false }); setSensitivePassword(""); // 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}

)} {confirmDialog.isSensitive && (
setSensitivePassword(e.target.value)} className="w-full px-2 py-1 rounded border" placeholder="Nhập mật khẩu để xác nhận" />
)}

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.

); }