From 65f428a9dc5fe4f41f85a67f7848853b679689a6 Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Fri, 19 Dec 2025 03:48:38 +0800 Subject: [PATCH] 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. --- components/sftp/SftpHostPicker.tsx | 160 ++++++++++++++++++++--------- 1 file changed, 113 insertions(+), 47 deletions(-) diff --git a/components/sftp/SftpHostPicker.tsx b/components/sftp/SftpHostPicker.tsx index 1aa522103..2d28bef87 100644 --- a/components/sftp/SftpHostPicker.tsx +++ b/components/sftp/SftpHostPicker.tsx @@ -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 = ({ onSelectHost, }) => { const { t } = useI18n(); + const inputRef = useRef(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 = ({ 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) => { + 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 ( - - + + {t('sftp.picker.title')} {t('sftp.picker.desc', { side: side === 'left' ? t('common.left') : t('common.right') })} -
+
+ 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" /> + + {sideLabel} + +
- {/* Local option */} -
{ onSelectLocal(); onOpenChange(false); }} - > -
-
- -
-
-
{t('sftp.picker.local.title')}
-
{t('sftp.picker.local.desc')}
-
+ +
+
+ + {t('sftp.picker.local.badge')} +
- {t('sftp.picker.local.badge')} -
- - {/* Remote hosts */} -
- {filteredHosts.map(host => ( -
{ onSelectHost(host); onOpenChange(false); }} - > -
- -
-
{host.label}
-
- {host.username}@{host.hostname} -
-
+
handleSelect(items[0])} + onMouseEnter={() => setSelectedIndex(0)} + > +
+
+ +
+
+
{t('sftp.picker.local.title')}
+
{t('sftp.picker.local.desc')}
- - SSH -
- ))} - {filteredHosts.length === 0 && ( -
+ + {t('sftp.picker.local.badge')} + +
+ +
+ + {t('vault.nav.hosts')} + +
+ {filteredHosts.length > 0 ? ( + filteredHosts.map((host, index) => { + const itemIndex = index + 1; + return ( +
handleSelect(items[itemIndex])} + onMouseEnter={() => setSelectedIndex(itemIndex)} + > +
+ +
+
{host.label}
+
+ {host.username}@{host.hostname} +
+
+
+ SSH +
+ ); + }) + ) : ( +
{t('sftp.picker.noMatch')}
)}
-
+
);