mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-17 04:50:52 +00:00
Enhances SFTP host picker with keyboard navigation
Improves user experience by adding keyboard support and visual selection for SFTP host selection. Refactors UI to include a scrollable host list, clearer section labeling, and streamlined search input with focus handling. Aims to make host selection faster and more accessible.
This commit is contained in:
1 parent
cd367fa44a
commit
65f428a9dc
1 file changed
+113
-47
@@ -2,14 +2,14 @@
|
||||
* SFTP Host Picker Dialog
|
||||
*/
|
||||
|
||||
import { Monitor } from 'lucide-react';
|
||||
import React,{ useMemo } from 'react';
|
||||
import { Monitor, Search } from 'lucide-react';
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useI18n } from '../../application/i18n/I18nProvider';
|
||||
import { Host } from '../../types';
|
||||
import { DistroAvatar } from '../DistroAvatar';
|
||||
import { Badge } from '../ui/badge';
|
||||
import { Dialog,DialogContent,DialogDescription,DialogHeader,DialogTitle } from '../ui/dialog';
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '../ui/dialog';
|
||||
import { Input } from '../ui/input';
|
||||
import { ScrollArea } from '../ui/scroll-area';
|
||||
|
||||
interface SftpHostPickerProps {
|
||||
open: boolean;
|
||||
@@ -33,6 +33,8 @@ export const SftpHostPicker: React.FC<SftpHostPickerProps> = ({
|
||||
onSelectHost,
|
||||
}) => {
|
||||
const { t } = useI18n();
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [selectedIndex, setSelectedIndex] = useState(0);
|
||||
const filteredHosts = useMemo(() => {
|
||||
const term = hostSearch.trim().toLowerCase();
|
||||
return hosts.filter(h =>
|
||||
@@ -41,70 +43,134 @@ export const SftpHostPicker: React.FC<SftpHostPickerProps> = ({
|
||||
h.hostname.toLowerCase().includes(term)
|
||||
).sort((a, b) => a.label.localeCompare(b.label));
|
||||
}, [hosts, hostSearch]);
|
||||
const sideLabel = side === 'left' ? t('common.left') : t('common.right');
|
||||
const items = useMemo(() => {
|
||||
return [{ type: 'local' as const, id: 'local' }].concat(
|
||||
filteredHosts.map((host) => ({ type: 'host' as const, id: host.id, host }))
|
||||
);
|
||||
}, [filteredHosts]);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setSelectedIndex(0);
|
||||
setTimeout(() => inputRef.current?.focus(), 50);
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
setSelectedIndex(0);
|
||||
}, [hostSearch, open]);
|
||||
|
||||
const handleSelect = (item: typeof items[number]) => {
|
||||
if (item.type === 'local') {
|
||||
onSelectLocal();
|
||||
} else {
|
||||
onSelectHost(item.host);
|
||||
}
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
setSelectedIndex((prev) => Math.min(prev + 1, items.length - 1));
|
||||
} else if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
setSelectedIndex((prev) => Math.max(prev - 1, 0));
|
||||
} else if (e.key === 'Enter' && items.length > 0) {
|
||||
e.preventDefault();
|
||||
handleSelect(items[selectedIndex]);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogContent className="max-w-lg p-0 overflow-hidden gap-0">
|
||||
<DialogHeader className="sr-only">
|
||||
<DialogTitle>{t('sftp.picker.title')}</DialogTitle>
|
||||
<DialogDescription>
|
||||
{t('sftp.picker.desc', { side: side === 'left' ? t('common.left') : t('common.right') })}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-3">
|
||||
<div className="flex items-center gap-3 px-4 py-3 pr-12 border-b border-border">
|
||||
<Search size={16} className="text-muted-foreground" />
|
||||
<Input
|
||||
ref={inputRef}
|
||||
value={hostSearch}
|
||||
onChange={e => onHostSearchChange(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder={t('sftp.picker.searchPlaceholder')}
|
||||
className="h-9"
|
||||
className="flex-1 h-8 border-0 bg-transparent focus-visible:ring-0 focus-visible:ring-offset-0 px-0 text-sm"
|
||||
/>
|
||||
<span className="ml-auto mr-1 text-[11px] text-muted-foreground bg-muted px-2 py-0.5 rounded whitespace-nowrap">
|
||||
{sideLabel}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Local option */}
|
||||
<div
|
||||
className="flex items-center justify-between px-3 py-2.5 rounded-lg border border-border/70 bg-secondary/30 cursor-pointer hover:border-primary/50 hover:bg-secondary/50 transition-colors"
|
||||
onClick={() => { onSelectLocal(); onOpenChange(false); }}
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="h-9 w-9 rounded-lg bg-primary/10 border border-primary/30 flex items-center justify-center text-primary">
|
||||
<Monitor size={16} />
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-sm font-medium">{t('sftp.picker.local.title')}</div>
|
||||
<div className="text-xs text-muted-foreground">{t('sftp.picker.local.desc')}</div>
|
||||
</div>
|
||||
<ScrollArea className="max-h-[360px]">
|
||||
<div className="py-2">
|
||||
<div className="px-4 py-1.5">
|
||||
<span className="text-xs font-medium text-muted-foreground">
|
||||
{t('sftp.picker.local.badge')}
|
||||
</span>
|
||||
</div>
|
||||
<Badge variant="outline" className="text-[10px]">{t('sftp.picker.local.badge')}</Badge>
|
||||
</div>
|
||||
|
||||
{/* Remote hosts */}
|
||||
<div className="max-h-64 overflow-auto space-y-2">
|
||||
{filteredHosts.map(host => (
|
||||
<div
|
||||
key={host.id}
|
||||
className="flex items-center justify-between px-3 py-2.5 rounded-lg border border-border/70 bg-secondary/30 cursor-pointer hover:border-primary/50 hover:bg-secondary/50 transition-colors"
|
||||
onClick={() => { onSelectHost(host); onOpenChange(false); }}
|
||||
>
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
<DistroAvatar host={host} fallback={host.label[0].toUpperCase()} className="h-9 w-9" />
|
||||
<div className="min-w-0">
|
||||
<div className="text-sm font-medium truncate">{host.label}</div>
|
||||
<div className="text-xs text-muted-foreground truncate">
|
||||
{host.username}@{host.hostname}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
className={`flex items-center justify-between px-4 py-2.5 cursor-pointer transition-colors ${selectedIndex === 0 ? 'bg-primary/15' : 'hover:bg-muted/50'
|
||||
}`}
|
||||
onClick={() => handleSelect(items[0])}
|
||||
onMouseEnter={() => setSelectedIndex(0)}
|
||||
>
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
<div className="h-6 w-6 rounded-md bg-primary/10 border border-primary/30 flex items-center justify-center text-primary">
|
||||
<Monitor size={14} />
|
||||
</div>
|
||||
<div className="min-w-0">
|
||||
<div className="text-sm font-medium truncate">{t('sftp.picker.local.title')}</div>
|
||||
<div className="text-xs text-muted-foreground truncate">{t('sftp.picker.local.desc')}</div>
|
||||
</div>
|
||||
<Badge variant="secondary" className="text-[10px] bg-primary/10 text-primary border-primary/30">
|
||||
SSH
|
||||
</Badge>
|
||||
</div>
|
||||
))}
|
||||
{filteredHosts.length === 0 && (
|
||||
<div className="text-xs text-muted-foreground text-center py-6 border border-dashed border-border/60 rounded-lg">
|
||||
<span className="text-[11px] text-muted-foreground">
|
||||
{t('sftp.picker.local.badge')}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="px-4 pt-3 pb-1.5">
|
||||
<span className="text-xs font-medium text-muted-foreground">
|
||||
{t('vault.nav.hosts')}
|
||||
</span>
|
||||
</div>
|
||||
{filteredHosts.length > 0 ? (
|
||||
filteredHosts.map((host, index) => {
|
||||
const itemIndex = index + 1;
|
||||
return (
|
||||
<div
|
||||
key={host.id}
|
||||
className={`flex items-center justify-between px-4 py-2.5 cursor-pointer transition-colors ${selectedIndex === itemIndex ? 'bg-primary/15' : 'hover:bg-muted/50'
|
||||
}`}
|
||||
onClick={() => handleSelect(items[itemIndex])}
|
||||
onMouseEnter={() => setSelectedIndex(itemIndex)}
|
||||
>
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
<DistroAvatar host={host} fallback={host.label[0].toUpperCase()} size="sm" />
|
||||
<div className="min-w-0">
|
||||
<div className="text-sm font-medium truncate">{host.label}</div>
|
||||
<div className="text-xs text-muted-foreground truncate">
|
||||
{host.username}@{host.hostname}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<span className="text-[11px] text-muted-foreground">SSH</span>
|
||||
</div>
|
||||
);
|
||||
})
|
||||
) : (
|
||||
<div className="px-4 py-6 text-xs text-muted-foreground text-center">
|
||||
{t('sftp.picker.noMatch')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user