'use client'; import { useState } from 'react'; import type { Server, CreateServerData } from '../../types/server'; import { ServerForm } from './ServerForm'; import { Button } from './ui/button'; interface ServerListProps { servers: Server[]; onUpdate: (id: number, data: CreateServerData) => void; onDelete: (id: number) => void; } export function ServerList({ servers, onUpdate, onDelete }: ServerListProps) { const [editingId, setEditingId] = useState(null); const [testingConnections, setTestingConnections] = useState>(new Set()); const [connectionResults, setConnectionResults] = useState>(new Map()); const handleEdit = (server: Server) => { setEditingId(server.id); }; const handleUpdate = (data: CreateServerData) => { if (editingId) { onUpdate(editingId, data); setEditingId(null); } }; const handleCancel = () => { setEditingId(null); }; const handleDelete = (id: number) => { if (window.confirm('Are you sure you want to delete this server configuration?')) { onDelete(id); } }; const handleTestConnection = async (server: Server) => { setTestingConnections(prev => new Set(prev).add(server.id)); setConnectionResults(prev => { const newMap = new Map(prev); newMap.delete(server.id); return newMap; }); try { const response = await fetch(`/api/servers/${server.id}/test-connection`, { method: 'POST', }); const result = await response.json(); setConnectionResults(prev => new Map(prev).set(server.id, { success: result.success, message: result.message })); } catch { setConnectionResults(prev => new Map(prev).set(server.id, { success: false, message: 'Failed to test connection - network error' })); } finally { setTestingConnections(prev => { const newSet = new Set(prev); newSet.delete(server.id); return newSet; }); } }; if (servers.length === 0) { return (

No servers configured

Get started by adding a new server configuration above.

); } return (
{servers.map((server) => (
{editingId === server.id ? (

Edit Server

) : (

{server.name}

{server.ip} {server.user}
Created: {new Date(server.created_at).toLocaleDateString()} {server.updated_at !== server.created_at && ( • Updated: {new Date(server.updated_at).toLocaleDateString()} )}
{/* Connection Test Result */} {connectionResults.has(server.id) && (
{connectionResults.get(server.id)?.success ? ( ) : ( )} {connectionResults.get(server.id)?.success ? 'Connection Successful' : 'Connection Failed'}

{connectionResults.get(server.id)?.message}

)}
)}
))}
); }