diff --git a/web/components/routes/public/Autocomplete.tsx b/web/components/routes/public/Autocomplete.tsx index 7c022a83..6cb80f60 100644 --- a/web/components/routes/public/Autocomplete.tsx +++ b/web/components/routes/public/Autocomplete.tsx @@ -19,11 +19,11 @@ interface AutocompleteProps { export function Autocomplete({ topics = [], onSelect, onInputChange }: AutocompleteProps): JSX.Element { const inputRef = useRef(null) - const isMounted = useMountedState() const [open, setOpen] = useState(false) + const isMounted = useMountedState() const [inputValue, setInputValue] = useState("") - const [isInitialOpen, setIsInitialOpen] = useState(true) const [hasInteracted, setHasInteracted] = useState(false) + const [showDropdown, setShowDropdown] = useState(false) const initialShuffledTopics = useMemo(() => shuffleArray(topics).slice(0, 5), [topics]) @@ -46,39 +46,31 @@ export function Autocomplete({ topics = [], onSelect, onInputChange }: Autocompl const handleSelect = useCallback( (topic: GraphNode) => { - // setInputValue(topicPrettyName) setOpen(false) onSelect(topic.name) }, [onSelect] ) - const handleKeyDown = useCallback( - (e: React.KeyboardEvent) => { - if ((e.key === "Backspace" || e.key === "Delete") && inputRef.current?.value === "") { - setOpen(true) - setIsInitialOpen(true) - } - if (!hasInteracted) { - setHasInteracted(true) - } - }, - [hasInteracted] - ) - const handleInputChange = useCallback( (value: string) => { setInputValue(value) - setOpen(true) - setIsInitialOpen(false) + setShowDropdown(true) + setHasInteracted(true) onInputChange(value) - if (!hasInteracted) { - setHasInteracted(true) - } }, - [onInputChange, hasInteracted] + [onInputChange] ) + const handleFocus = useCallback(() => { + setHasInteracted(true) + }, []) + + const handleClick = useCallback(() => { + setShowDropdown(true) + setHasInteracted(true) + }, []) + const commandKey = useMemo(() => { return filteredTopics .map(topic => `${topic.name}:${topic.prettyName}:${topic.connectedTopics.join(",")}`) @@ -100,12 +92,10 @@ export function Autocomplete({ topics = [], onSelect, onInputChange }: Autocompl return (
{ - setTimeout(() => { - setOpen(false) - setIsInitialOpen(true) - }, 100) + setTimeout(() => setShowDropdown(false), 100) }} - onFocus={() => { - setOpen(true) - setIsInitialOpen(true) - if (!hasInteracted) { - setHasInteracted(true) - } - }} - placeholder="Search for a topic..." + onFocus={handleFocus} + onClick={handleClick} + placeholder={filteredTopics[0]?.prettyName} className={cn("placeholder:text-muted-foreground flex-1 bg-transparent px-2 outline-none")} + autoFocus // Add this line />
- {open && ( + {showDropdown && hasInteracted && (