import { Search } from 'lucide-react'; import React, { useMemo, useState, useEffect, useCallback } from 'react'; import { useI18n } from '../application/i18n/I18nProvider'; import { Host } from '../types'; import { DistroAvatar } from './DistroAvatar'; import { Button } from './ui/button'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from './ui/dialog'; import { Input } from './ui/input'; import { Label } from './ui/label'; import { FixedSizeVirtualList } from './ui/FixedSizeVirtualList'; const CREATE_WS_HOST_ROW_HEIGHT = 52; interface CreateWorkspaceDialogProps { isOpen: boolean; onClose: () => void; hosts: Host[]; onCreate: (name: string, selectedHosts: Host[]) => void; } export const CreateWorkspaceDialog: React.FC = ({ isOpen, onClose, hosts, onCreate, }) => { const { t } = useI18n(); const [name, setName] = useState(''); const [search, setSearch] = useState(''); const [selectedHostIds, setSelectedHostIds] = useState>(new Set()); const filteredHosts = useMemo(() => { if (!search.trim()) return hosts; const term = search.toLowerCase(); return hosts.filter(h => h.label.toLowerCase().includes(term) || h.hostname.toLowerCase().includes(term) || (h.group || '').toLowerCase().includes(term) ); }, [hosts, search]); const toggleHost = useCallback((hostId: string) => { setSelectedHostIds(prev => { const next = new Set(prev); if (next.has(hostId)) { next.delete(hostId); } else { next.add(hostId); } return next; }); }, []); const handleCreate = () => { const selected = hosts.filter(h => selectedHostIds.has(h.id)); onCreate(name, selected); onClose(); }; useEffect(() => { if (isOpen) { setName(''); setSearch(''); setSelectedHostIds(new Set()); } }, [isOpen]); const renderHostRow = useCallback((host: Host) => { const isSelected = selectedHostIds.has(host.id); return (
toggleHost(host.id)} >
{isSelected &&
}
{host.label}
{host.hostname}
); }, [selectedHostIds, toggleHost]); return ( !open && onClose()}> {t('dialog.createWorkspace.title', { defaultValue: 'Create Workspace' })}
setName(e.target.value)} placeholder={t('placeholder.workspaceName', { defaultValue: 'Workspace Name' })} autoFocus />
setSearch(e.target.value)} className="pl-8" />
{filteredHosts.length === 0 ? (
{t('common.noResults', { defaultValue: 'No hosts found' })}
) : ( host.id} renderItem={renderHostRow} /> )}
{selectedHostIds.size} {t('common.selected', { defaultValue: 'selected' })}
); };