import React from "react"; import { Button } from "@/components/ui/button"; import { MoreHorizontal, Eye, Play, Pause, Edit, Bell, BellOff, Trash2 } from "lucide-react"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, DropdownMenuSeparator, } from "@/components/ui/dropdown-menu"; import { Service } from "@/types/service.types"; import { serviceService } from "@/services/serviceService"; import { useToast } from "@/hooks/use-toast"; interface ServiceRowActionsProps { service: Service; onViewDetail: (service: Service) => void; onPauseResume: (service: Service) => Promise; onEdit: (service: Service) => void; onDelete: (service: Service) => void; onMuteAlerts?: (service: Service) => Promise; } export const ServiceRowActions = ({ service, onViewDetail, onPauseResume, onEdit, onDelete, onMuteAlerts }: ServiceRowActionsProps) => { const { toast } = useToast(); // Handle pause/resume directly from dropdown const handlePauseResume = async (e: React.MouseEvent) => { e.stopPropagation(); try { if (service.status === "paused") { // Resume monitoring // console.log(`Resuming monitoring for service ${service.id} (${service.name}) from dropdown`); // First ensure we update the status await serviceService.resumeMonitoring(service.id); // Then start monitoring service (performs an immediate check) await serviceService.startMonitoringService(service.id); toast({ title: "Monitoring resumed", description: `Monitoring for ${service.name} has been resumed. First check is running now.`, }); } else { // Pause monitoring // console.log(`Pausing monitoring for service ${service.id} (${service.name}) from dropdown`); await serviceService.pauseMonitoring(service.id); toast({ title: "Monitoring paused", description: `Monitoring for ${service.name} has been paused.`, }); } // Call the parent handler to refresh the UI onPauseResume(service); } catch (error) { // console.error("Error toggling monitoring:", error); toast({ variant: "destructive", title: "Error", description: "Failed to change monitoring status. Please try again.", }); } }; // Check alerts status - check both fields for backward compatibility const alertsMuted = service.alerts === "muted" || service.muteAlerts === true; // Handle mute/unmute alerts const handleMuteAlerts = async (e: React.MouseEvent) => { e.stopPropagation(); if (onMuteAlerts) { try { // console.log(`Attempting to ${alertsMuted ? 'unmute' : 'mute'} alerts for service ${service.id} (${service.name})`); await onMuteAlerts(service); } catch (error) { // console.error("Error toggling alerts:", error); toast({ variant: "destructive", title: "Error", description: "Failed to change alert settings. Please try again.", }); } } }; return (
{ e.stopPropagation(); onViewDetail(service); }} > View Detail {service.status === "paused" ? ( <> Resume Monitoring ) : ( <> Pause Monitoring )} { e.stopPropagation(); onEdit(service); }} > Edit {alertsMuted ? ( <> Unmute Alerts ) : ( <> Mute Alerts )} { e.stopPropagation(); onDelete(service); }} > Delete
); };