import React, {useState, useEffect, useMemo} from "react"; import {Card, CardContent} from "@/components/ui/card"; import {Button} from "@/components/ui/button"; import {Badge} from "@/components/ui/badge"; import {ScrollArea} from "@/components/ui/scroll-area"; import {Input} from "@/components/ui/input"; import {Accordion, AccordionContent, AccordionItem, AccordionTrigger} from "@/components/ui/accordion"; import {Tooltip, TooltipContent, TooltipProvider, TooltipTrigger} from "@/components/ui/tooltip"; import {getSSHHosts, deleteSSHHost, bulkImportSSHHosts} from "@/ui/SSH/ssh-axios"; import { Edit, Trash2, Server, Folder, Tag, Pin, Terminal, Network, FileEdit, Search, Upload, Info } from "lucide-react"; import {Separator} from "@/components/ui/separator.tsx"; interface SSHHost { id: number; name: string; ip: string; port: number; username: string; folder: string; tags: string[]; pin: boolean; authType: string; enableTerminal: boolean; enableTunnel: boolean; enableConfigEditor: boolean; defaultPath: string; tunnelConnections: any[]; createdAt: string; updatedAt: string; } interface SSHManagerHostViewerProps { onEditHost?: (host: SSHHost) => void; } export function SSHManagerHostViewer({onEditHost}: SSHManagerHostViewerProps) { const [hosts, setHosts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchQuery, setSearchQuery] = useState(""); const [importing, setImporting] = useState(false); useEffect(() => { fetchHosts(); }, []); const fetchHosts = async () => { try { setLoading(true); const data = await getSSHHosts(); setHosts(data); setError(null); } catch (err) { setError('Failed to load hosts'); } finally { setLoading(false); } }; const handleDelete = async (hostId: number, hostName: string) => { if (window.confirm(`Are you sure you want to delete "${hostName}"?`)) { try { await deleteSSHHost(hostId); await fetchHosts(); } catch (err) { alert('Failed to delete host'); } } }; const handleEdit = (host: SSHHost) => { if (onEditHost) { onEditHost(host); } }; const handleJsonImport = async (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (!file) return; try { setImporting(true); const text = await file.text(); const data = JSON.parse(text); if (!Array.isArray(data.hosts) && !Array.isArray(data)) { throw new Error('JSON must contain a "hosts" array or be an array of hosts'); } const hostsArray = Array.isArray(data.hosts) ? data.hosts : data; if (hostsArray.length === 0) { throw new Error('No hosts found in JSON file'); } if (hostsArray.length > 100) { throw new Error('Maximum 100 hosts allowed per import'); } const result = await bulkImportSSHHosts(hostsArray); if (result.success > 0) { alert(`Import completed: ${result.success} successful, ${result.failed} failed${result.errors.length > 0 ? '\n\nErrors:\n' + result.errors.join('\n') : ''}`); await fetchHosts(); } else { alert(`Import failed: ${result.errors.join('\n')}`); } } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Failed to import JSON file'; alert(`Import error: ${errorMessage}`); } finally { setImporting(false); event.target.value = ''; } }; const filteredAndSortedHosts = useMemo(() => { let filtered = hosts; if (searchQuery.trim()) { const query = searchQuery.toLowerCase(); filtered = hosts.filter(host => { const searchableText = [ host.name || '', host.username, host.ip, host.folder || '', ...(host.tags || []), host.authType, host.defaultPath || '' ].join(' ').toLowerCase(); return searchableText.includes(query); }); } return filtered.sort((a, b) => { if (a.pin && !b.pin) return -1; if (!a.pin && b.pin) return 1; const aName = a.name || a.username; const bName = b.name || b.username; return aName.localeCompare(bName); }); }, [hosts, searchQuery]); const hostsByFolder = useMemo(() => { const grouped: { [key: string]: SSHHost[] } = {}; filteredAndSortedHosts.forEach(host => { const folder = host.folder || 'Uncategorized'; if (!grouped[folder]) { grouped[folder] = []; } grouped[folder].push(host); }); const sortedFolders = Object.keys(grouped).sort((a, b) => { if (a === 'Uncategorized') return -1; if (b === 'Uncategorized') return 1; return a.localeCompare(b); }); const sortedGrouped: { [key: string]: SSHHost[] } = {}; sortedFolders.forEach(folder => { sortedGrouped[folder] = grouped[folder]; }); return sortedGrouped; }, [filteredAndSortedHosts]); if (loading) { return (

Loading hosts...

); } if (error) { return (

{error}

); } if (hosts.length === 0) { return (

No SSH Hosts

You haven't added any SSH hosts yet. Click "Add Host" to get started.

); } return (

SSH Hosts

{filteredAndSortedHosts.length} hosts

Import SSH Hosts from JSON

Upload a JSON file to bulk import multiple SSH hosts (max 100).

port - SSH port (number, 1-65535)
username - SSH username (string)
authType - "password" or "key"

Authentication Fields

password - Required if authType is "password"
key - SSH private key content (string) if authType is "key"
keyPassword - Optional key passphrase
keyType - Key type (auto, ssh-rsa, ssh-ed25519, etc.)

Optional Fields

name - Display name (string)
folder - Organization folder (string)
tags - Array of tag strings
pin - Pin to top (boolean)
enableTerminal - Show in Terminal tab (boolean, default: true)
enableTunnel - Show in Tunnel tab (boolean, default: true)
enableConfigEditor - Show in Config Editor tab (boolean, default: true)
defaultPath - Default directory path (string)

Tunnel Configuration

tunnelConnections - Array of tunnel objects
sourcePort - Local port (number)
endpointPort - Remote port (number)
endpointHost - Target host name (string)
maxRetries - Retry attempts (number, default: 3)
retryInterval - Retry delay in seconds (number, default: 10)
autoStart - Auto-start on launch (boolean, default: false)

Example JSON Structure

{
  "hosts": [
    {
      "name": "Web Server",
      "ip": "192.168.1.100",
      "port": 22,
      "username": "admin",
      "authType": "password",
      "password": "your_password",
      "folder": "Production",
      "tags": ["web", "production"],
      "pin": true,
      "enableTerminal": true,
      "enableTunnel": false,
      "enableConfigEditor": true,
      "defaultPath": "/var/www"
    }
  ]
}

Important Notes

  • Maximum 100 hosts per import
  • File should contain a "hosts" array or be an array of host objects
  • All fields are copyable for easy reference
  • Use the Download Sample button to get a complete example file
`); newWindow.document.close(); } }} > Format Guide
setSearchQuery(e.target.value)} className="pl-10" />
{Object.entries(hostsByFolder).map(([folder, folderHosts]) => (
{folder} {folderHosts.length}
{folderHosts.map((host) => (
handleEdit(host)} >
{host.pin && }

{host.name || `${host.username}@${host.ip}`}

{host.ip}:{host.port}

{host.username}

{host.tags && host.tags.length > 0 && (
{host.tags.slice(0, 6).map((tag, index) => ( {tag} ))} {host.tags.length > 6 && ( +{host.tags.length - 6} )}
)}
{host.enableTerminal && ( Terminal )} {host.enableTunnel && ( Tunnel {host.tunnelConnections && host.tunnelConnections.length > 0 && ( ({host.tunnelConnections.length}) )} )} {host.enableConfigEditor && ( Config )}
))}
))}
); }