|
| 1 | +import React, { useState, useEffect, useRef, useCallback } from "react"; |
| 2 | +import { useAPI } from "@/browser/contexts/API"; |
| 3 | + |
| 4 | +interface SSHHostInputProps { |
| 5 | + value: string; |
| 6 | + onChange: (value: string) => void; |
| 7 | + disabled: boolean; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * SSH host input with dropdown of hosts from SSH config. |
| 12 | + * Shows dropdown above the input when focused and there are matching hosts. |
| 13 | + */ |
| 14 | +export function SSHHostInput(props: SSHHostInputProps) { |
| 15 | + const { api } = useAPI(); |
| 16 | + const [hosts, setHosts] = useState<string[]>([]); |
| 17 | + const [showDropdown, setShowDropdown] = useState(false); |
| 18 | + const [highlightedIndex, setHighlightedIndex] = useState(-1); |
| 19 | + const inputRef = useRef<HTMLInputElement>(null); |
| 20 | + const containerRef = useRef<HTMLDivElement>(null); |
| 21 | + |
| 22 | + // Fetch SSH config hosts on mount |
| 23 | + useEffect(() => { |
| 24 | + if (!api) return; |
| 25 | + api.ssh |
| 26 | + .getConfigHosts() |
| 27 | + .then(setHosts) |
| 28 | + .catch(() => setHosts([])); |
| 29 | + }, [api]); |
| 30 | + |
| 31 | + // Filter hosts based on current input |
| 32 | + const filteredHosts = hosts.filter((host) => |
| 33 | + host.toLowerCase().includes(props.value.toLowerCase()) |
| 34 | + ); |
| 35 | + |
| 36 | + // Handle clicking outside to close dropdown |
| 37 | + useEffect(() => { |
| 38 | + function handleClickOutside(e: MouseEvent) { |
| 39 | + if (containerRef.current && !containerRef.current.contains(e.target as Node)) { |
| 40 | + setShowDropdown(false); |
| 41 | + } |
| 42 | + } |
| 43 | + document.addEventListener("mousedown", handleClickOutside); |
| 44 | + return () => document.removeEventListener("mousedown", handleClickOutside); |
| 45 | + }, []); |
| 46 | + |
| 47 | + const { onChange } = props; |
| 48 | + const selectHost = useCallback( |
| 49 | + (host: string) => { |
| 50 | + onChange(host); |
| 51 | + setShowDropdown(false); |
| 52 | + setHighlightedIndex(-1); |
| 53 | + inputRef.current?.focus(); |
| 54 | + }, |
| 55 | + [onChange] |
| 56 | + ); |
| 57 | + |
| 58 | + const handleKeyDown = useCallback( |
| 59 | + (e: React.KeyboardEvent) => { |
| 60 | + if (!showDropdown || filteredHosts.length === 0) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + switch (e.key) { |
| 65 | + case "ArrowDown": |
| 66 | + e.preventDefault(); |
| 67 | + setHighlightedIndex((prev) => (prev < filteredHosts.length - 1 ? prev + 1 : 0)); |
| 68 | + break; |
| 69 | + case "ArrowUp": |
| 70 | + e.preventDefault(); |
| 71 | + setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredHosts.length - 1)); |
| 72 | + break; |
| 73 | + case "Enter": |
| 74 | + if (highlightedIndex >= 0) { |
| 75 | + e.preventDefault(); |
| 76 | + selectHost(filteredHosts[highlightedIndex]); |
| 77 | + } |
| 78 | + break; |
| 79 | + case "Escape": |
| 80 | + e.preventDefault(); |
| 81 | + setShowDropdown(false); |
| 82 | + setHighlightedIndex(-1); |
| 83 | + break; |
| 84 | + } |
| 85 | + }, |
| 86 | + [showDropdown, filteredHosts, highlightedIndex, selectHost] |
| 87 | + ); |
| 88 | + |
| 89 | + const handleFocus = () => { |
| 90 | + if (filteredHosts.length > 0) { |
| 91 | + setShowDropdown(true); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 96 | + props.onChange(e.target.value); |
| 97 | + // Show dropdown when typing if there are matches |
| 98 | + if (hosts.length > 0) { |
| 99 | + setShowDropdown(true); |
| 100 | + } |
| 101 | + setHighlightedIndex(-1); |
| 102 | + }; |
| 103 | + |
| 104 | + // Show dropdown when there are filtered hosts |
| 105 | + const shouldShowDropdown = showDropdown && filteredHosts.length > 0 && !props.disabled; |
| 106 | + |
| 107 | + return ( |
| 108 | + <div ref={containerRef} className="relative"> |
| 109 | + <input |
| 110 | + ref={inputRef} |
| 111 | + type="text" |
| 112 | + value={props.value} |
| 113 | + onChange={handleChange} |
| 114 | + onFocus={handleFocus} |
| 115 | + onKeyDown={handleKeyDown} |
| 116 | + placeholder="user@host" |
| 117 | + disabled={props.disabled} |
| 118 | + className="bg-separator text-foreground border-border-medium focus:border-accent w-32 rounded border px-1 py-0.5 text-xs focus:outline-none disabled:opacity-50" |
| 119 | + autoComplete="off" |
| 120 | + /> |
| 121 | + {shouldShowDropdown && ( |
| 122 | + <div className="bg-separator border-border-light absolute bottom-full left-0 z-[1000] mb-1 max-h-[150px] min-w-32 overflow-y-auto rounded border shadow-[0_4px_12px_rgba(0,0,0,0.3)]"> |
| 123 | + {filteredHosts.map((host, index) => ( |
| 124 | + <div |
| 125 | + key={host} |
| 126 | + onClick={() => selectHost(host)} |
| 127 | + onMouseEnter={() => setHighlightedIndex(index)} |
| 128 | + className={`cursor-pointer px-2 py-1 text-xs ${ |
| 129 | + index === highlightedIndex |
| 130 | + ? "bg-accent text-white" |
| 131 | + : "text-foreground hover:bg-border-medium" |
| 132 | + }`} |
| 133 | + > |
| 134 | + {host} |
| 135 | + </div> |
| 136 | + ))} |
| 137 | + </div> |
| 138 | + )} |
| 139 | + </div> |
| 140 | + ); |
| 141 | +} |
0 commit comments