import React, { useState } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { useToast } from "@/hooks/use-toast"; import { Server } from "lucide-react"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { getCurrentEndpoint } from "@/lib/pocketbase"; import { ServerAgentConfigForm } from "./ServerAgentConfigForm"; import { OneClickInstallTab } from "./OneClickInstallTab"; import { ManualInstallTab } from "./ManualInstallTab"; interface AddServerAgentDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onAgentAdded: () => void; } export const AddServerAgentDialog: React.FC = ({ open, onOpenChange, onAgentAdded, }) => { const { toast } = useToast(); const queryClient = useQueryClient(); const [isSubmitting, setIsSubmitting] = useState(false); const [activeTab, setActiveTab] = useState("configure"); // Get current PocketBase URL const currentPocketBaseUrl = getCurrentEndpoint(); // Form state const [formData, setFormData] = useState({ serverName: "", description: "", osType: "", checkInterval: "60", retryAttempt: "3", dockerEnabled: false, notificationEnabled: true, }); // Generated server token and agent ID const [serverToken] = useState(() => `srv_${Math.random().toString(36).substring(2, 15)}${Math.random().toString(36).substring(2, 15)}` ); const [serverId] = useState(() => `agent_${Math.random().toString(36).substring(2, 15)}${Math.random().toString(36).substring(2, 15)}` ); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (isSubmitting) return; if (!formData.serverName || !formData.osType) { toast({ title: "Validation Error", description: "Please fill in all required fields.", variant: "destructive", }); return; } setIsSubmitting(true); try { // Here you would typically create the server monitoring configuration // For now, we'll simulate the process await new Promise(resolve => setTimeout(resolve, 1500)); toast({ title: "Server Agent Created", description: `${formData.serverName} monitoring agent has been configured successfully.`, }); // Switch to one-click install tab after successful creation setActiveTab("one-click"); onAgentAdded(); } catch (error) { toast({ title: "Error", description: "Failed to create server monitoring agent.", variant: "destructive", }); } finally { setIsSubmitting(false); } }; const handleDialogClose = () => { // Reset form and tab when dialog closes setActiveTab("configure"); setFormData({ serverName: "", description: "", osType: "", checkInterval: "60", retryAttempt: "3", dockerEnabled: false, notificationEnabled: true, }); onOpenChange(false); }; return ( Add Server Monitoring Agent Configure a new server monitoring agent to track system metrics and performance. Configure Agent One-Click Install Manual Installation ); };