import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Switch } from "@/components/ui/switch"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { UseFormReturn } from "react-hook-form"; import { ServiceFormData } from "./types"; import { useQuery } from "@tanstack/react-query"; import { regionalService } from "@/services/regionalService"; import { MapPin, Loader2, X } from "lucide-react"; interface ServiceRegionalFieldsProps { form: UseFormReturn; } export function ServiceRegionalFields({ form }: ServiceRegionalFieldsProps) { const regionalMonitoringEnabled = form.watch("regionalMonitoringEnabled"); const currentRegionalAgent = form.watch("regionalAgent"); const { data: regionalAgents = [], isLoading } = useQuery({ queryKey: ['regional-services'], queryFn: regionalService.getRegionalServices, enabled: regionalMonitoringEnabled, }); // Filter only online agents const onlineAgents = regionalAgents.filter(agent => agent.connection === 'online'); // Find the current agent name for display const getCurrentAgentDisplay = () => { if (!currentRegionalAgent || currentRegionalAgent === "unassign") { return "Select a regional agent or unassign"; } const [regionName] = currentRegionalAgent.split("|"); const agent = onlineAgents.find(agent => `${agent.region_name}|${agent.agent_id}` === currentRegionalAgent ); if (agent) { return `${agent.region_name} (${agent.agent_ip_address})`; } // If agent is not found in online agents, it might be offline but still assigned return regionName || "Select a regional agent or unassign"; }; return (
(
Regional Monitoring
Assign this service to a regional monitoring agent for distributed monitoring
)} /> {regionalMonitoringEnabled && ( ( Regional Agent {regionalMonitoringEnabled && onlineAgents.length === 0 && !isLoading && (

No online regional agents found. Services will use default monitoring.

)} {currentRegionalAgent && currentRegionalAgent !== "" && currentRegionalAgent !== "unassign" && (

Currently assigned to: {getCurrentAgentDisplay()}

)} {(!currentRegionalAgent || currentRegionalAgent === "" || currentRegionalAgent === "unassign") && (

Service is unassigned and will use default monitoring.

)}
)} /> )}
); }