import React, { useState, useEffect } from "react"; import { useToast } from "../ui/toast"; import Loader from "../ui/loader"; import { Button } from "../ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/card"; import Table from "../ui/table"; // Import your custom Table component import { useIsLoggedIn } from '../../lib/isLoggedIn'; export default function HetznerServicesList() { const { isLoggedIn, loading, error, sessionData } = useIsLoggedIn(); const { showToast } = useToast(); const [isLoading, setIsLoading] = useState(true); const [servers, setServers] = useState([]); const PUBLIC_HETZNER_API_KEY = import.meta.env.PUBLIC_HETZNER_API_KEY; // Define fetchServers outside useEffect so it can be reused const fetchServers = async () => { try { const response = await fetch("https://api.hetzner.cloud/v1/servers", { headers: { "Authorization": `Bearer ${PUBLIC_HETZNER_API_KEY}` } }); const data = await response.json(); setServers(data.servers || []); } catch (error) { showToast({ title: "Error", description: "Failed to load servers", variant: "destructive" }); } finally { setIsLoading(false); } }; useEffect(() => { fetchServers(); }, []); const handlePowerAction = async (serverId, action) => { try { setIsLoading(true); const response = await fetch(`https://api.hetzner.cloud/v1/servers/${serverId}/actions/${action}`, { method: "POST", headers: { "Authorization": `Bearer ${PUBLIC_HETZNER_API_KEY}`, "Content-Type": "application/json" } }); const data = await response.json(); if (data.action) { showToast({ title: "Success", description: `Server ${action} action initiated`, variant: "success" }); // Refresh server list after a short delay setTimeout(() => { fetchServers(); }, 3000); } } catch (error) { showToast({ title: "Error", description: `Failed to ${action} server`, variant: "destructive" }); } finally { setIsLoading(false); } }; const handleDeleteServer = async (serverId) => { try { setIsLoading(true); const response = await fetch(`https://api.hetzner.cloud/v1/servers/${serverId}`, { method: "DELETE", headers: { "Authorization": `Bearer ${PUBLIC_HETZNER_API_KEY}`, "Content-Type": "application/json" } }); if (response.ok) { showToast({ title: "Success", description: "Server deleted successfully", variant: "success" }); // Remove the server from local state setServers(prev => prev.filter(server => server.id !== serverId)); } } catch (error) { showToast({ title: "Error", description: "Failed to delete server", variant: "destructive" }); } finally { setIsLoading(false); } }; const getStatusBadge = (status) => { let colorClass = ""; switch (status) { case "running": colorClass = "bg-green-100 text-green-800"; break; case "off": colorClass = "bg-gray-100 text-gray-800"; break; case "starting": case "stopping": colorClass = "bg-yellow-100 text-yellow-800"; break; default: colorClass = "bg-blue-100 text-blue-800"; } return ( {status} ); }; // Prepare data for your Table component const tableData = servers.map(server => ({ name: server.name, status: getStatusBadge(server.status), type: server.server_type.name, location: server.datacenter.location.city, ip: server.public_net.ipv4 ? server.public_net.ipv4.ip : "None", backups: server.backup_window ? "Enabled" : "Disabled", actions: (
{server.status === "running" ? ( <> ) : ( )}
) })); const tableHeaders = ["Name", "Status", "Type", "Location", "IPv4", "Backups", "Actions"]; if (loading || (isLoggedIn && sessionData?.user_type === 'admin' && isLoading && servers.length === 0)) { return ; } if (!isLoggedIn || sessionData?.user_type !== 'admin') { return

You are not authorized to view this page. Click Here to go to the homepage.

; } return ( Your Hetzner Cloud Servers {servers.length === 0 ? (

No servers found. Create your first server to get started.

) : ( )} ); }