import { ArrowLeft, Check, ChevronRight, LayoutGrid, Plus, Search, X } from 'lucide-react'; import React,{ useMemo,useState } from 'react'; import { cn } from '../lib/utils'; import { Host,SSHKey } from '../types'; import { DistroAvatar } from './DistroAvatar'; import HostDetailsPanel from './HostDetailsPanel'; import { Button } from './ui/button'; import { Input } from './ui/input'; import { ScrollArea } from './ui/scroll-area'; import { SortDropdown,SortMode } from './ui/sort-dropdown'; import { TagFilterDropdown } from './ui/tag-filter-dropdown'; interface SelectHostPanelProps { hosts: Host[]; customGroups?: string[]; selectedHostIds?: string[]; multiSelect?: boolean; onSelect: (host: Host) => void; onBack: () => void; onContinue?: () => void; onNewHost?: () => void; // Props for inline host creation availableKeys?: SSHKey[]; onSaveHost?: (host: Host) => void; onCreateGroup?: (groupPath: string) => void; title?: string; subtitle?: string; className?: string; } const SelectHostPanel: React.FC = ({ hosts, customGroups = [], selectedHostIds = [], multiSelect = false, onSelect, onBack, onContinue, onNewHost, availableKeys = [], onSaveHost, onCreateGroup, title = 'Select Host', subtitle, className, }) => { const [searchQuery, setSearchQuery] = useState(''); const [currentPath, setCurrentPath] = useState(null); const [sortMode, setSortMode] = useState('az'); const [selectedTags, setSelectedTags] = useState([]); const [showNewHostPanel, setShowNewHostPanel] = useState(false); // Get all unique tags from hosts const allTags = useMemo(() => { const tagSet = new Set(); hosts.forEach(h => { if (h.tags) { h.tags.forEach(tag => tagSet.add(tag)); } }); return Array.from(tagSet).sort(); }, [hosts]); // Get unique group paths from both hosts and customGroups const allGroupPaths = useMemo(() => { const pathSet = new Set(); hosts.forEach(h => { if (h.group) { // Add all parent paths as well const parts = h.group.split('/'); for (let i = 1; i <= parts.length; i++) { pathSet.add(parts.slice(0, i).join('/')); } } }); customGroups.forEach(g => pathSet.add(g)); return Array.from(pathSet).sort(); }, [hosts, customGroups]); // Get groups at current level const groupsWithCounts = useMemo(() => { const prefix = currentPath ? `${currentPath}/` : ''; const groups: { path: string; name: string; count: number }[] = []; const seen = new Set(); allGroupPaths.forEach(path => { if (currentPath === null) { // Root level - get top-level groups const topLevel = path.split('/')[0]; if (!seen.has(topLevel)) { seen.add(topLevel); const count = hosts.filter(h => h.group && (h.group === topLevel || h.group.startsWith(`${topLevel}/`))).length; groups.push({ path: topLevel, name: topLevel, count }); } } else if (path.startsWith(prefix) && path !== currentPath) { // Subgroups const rest = path.slice(prefix.length); const nextLevel = rest.split('/')[0]; const fullPath = `${prefix}${nextLevel}`; if (!seen.has(fullPath)) { seen.add(fullPath); const count = hosts.filter(h => h.group && (h.group === fullPath || h.group.startsWith(`${fullPath}/`))).length; groups.push({ path: fullPath, name: nextLevel, count }); } } }); return groups; }, [allGroupPaths, currentPath, hosts]); // Get hosts at current level with filtering and sorting const filteredHosts = useMemo(() => { let result = hosts; // Filter by current path if (currentPath) { result = result.filter(h => h.group === currentPath || h.group?.startsWith(`${currentPath}/`)); } // Filter by search if (searchQuery) { const q = searchQuery.toLowerCase(); result = result.filter(h => h.label.toLowerCase().includes(q) || h.hostname.toLowerCase().includes(q) || h.username.toLowerCase().includes(q) ); } // Filter by tags if (selectedTags.length > 0) { result = result.filter(h => h.tags && selectedTags.some(tag => h.tags.includes(tag)) ); } // Sort hosts result = [...result].sort((a, b) => { switch (sortMode) { case 'az': return a.label.localeCompare(b.label); case 'za': return b.label.localeCompare(a.label); case 'newest': // Use id as proxy for creation time (UUIDs are time-sortable or fall back to label) return b.id.localeCompare(a.id); case 'oldest': return a.id.localeCompare(b.id); default: return 0; } }); return result; }, [hosts, currentPath, searchQuery, selectedTags, sortMode]); // Build breadcrumb from current path const breadcrumbs = useMemo(() => { if (!currentPath) return []; const parts = currentPath.split('/'); return parts.map((part, index) => ({ name: part, path: parts.slice(0, index + 1).join('/'), })); }, [currentPath]); const handleBack = () => { if (currentPath) { const parts = currentPath.split('/'); if (parts.length > 1) { setCurrentPath(parts.slice(0, -1).join('/')); } else { setCurrentPath(null); } } else { onBack(); } }; return (
{/* Header */}

{title}

{subtitle &&

{subtitle}

}
{/* Toolbar */}
{(onNewHost || onSaveHost) && ( )}
setSearchQuery(e.target.value)} />
{/* Content */}
{/* Breadcrumbs */} {currentPath && (
{breadcrumbs.map((crumb, index) => ( ))}
)} {groupsWithCounts.length > 0 && (

Groups

{groupsWithCounts.map(group => (
setCurrentPath(group.path)} >
{group.name}
{group.count} Host{group.count !== 1 ? 's' : ''}
))}
)} {/* Hosts Section */} {filteredHosts.length > 0 && (

Hosts

{filteredHosts.map(host => { const isSelected = selectedHostIds.includes(host.id); return (
onSelect(host)} >
{host.label}
{host.protocol || 'ssh'}, {host.username}
{isSelected && ( )}
); })}
)} {/* Empty state */} {groupsWithCounts.length === 0 && filteredHosts.length === 0 && (

No hosts found

)}
{/* Footer */}
{/* New Host Panel Overlay */} {showNewHostPanel && onSaveHost && ( { onSaveHost(host); setShowNewHostPanel(false); }} onCancel={() => setShowNewHostPanel(false)} onCreateGroup={onCreateGroup} /> )}
); }; export default SelectHostPanel;