|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { useState, useCallback } from 'react'; |
|
|
|
|
|
import type { Suggestion } from '../components/SuggestionsDisplay.js'; |
|
|
import { MAX_SUGGESTIONS_TO_SHOW } from '../components/SuggestionsDisplay.js'; |
|
|
|
|
|
export interface UseCompletionReturn { |
|
|
suggestions: Suggestion[]; |
|
|
activeSuggestionIndex: number; |
|
|
visibleStartIndex: number; |
|
|
showSuggestions: boolean; |
|
|
isLoadingSuggestions: boolean; |
|
|
isPerfectMatch: boolean; |
|
|
setSuggestions: React.Dispatch<React.SetStateAction<Suggestion[]>>; |
|
|
setActiveSuggestionIndex: React.Dispatch<React.SetStateAction<number>>; |
|
|
setVisibleStartIndex: React.Dispatch<React.SetStateAction<number>>; |
|
|
setIsLoadingSuggestions: React.Dispatch<React.SetStateAction<boolean>>; |
|
|
setIsPerfectMatch: React.Dispatch<React.SetStateAction<boolean>>; |
|
|
setShowSuggestions: React.Dispatch<React.SetStateAction<boolean>>; |
|
|
resetCompletionState: () => void; |
|
|
navigateUp: () => void; |
|
|
navigateDown: () => void; |
|
|
} |
|
|
|
|
|
export function useCompletion(): UseCompletionReturn { |
|
|
const [suggestions, setSuggestions] = useState<Suggestion[]>([]); |
|
|
const [activeSuggestionIndex, setActiveSuggestionIndex] = |
|
|
useState<number>(-1); |
|
|
const [visibleStartIndex, setVisibleStartIndex] = useState<number>(0); |
|
|
const [showSuggestions, setShowSuggestions] = useState<boolean>(false); |
|
|
const [isLoadingSuggestions, setIsLoadingSuggestions] = |
|
|
useState<boolean>(false); |
|
|
const [isPerfectMatch, setIsPerfectMatch] = useState<boolean>(false); |
|
|
|
|
|
const resetCompletionState = useCallback(() => { |
|
|
setSuggestions([]); |
|
|
setActiveSuggestionIndex(-1); |
|
|
setVisibleStartIndex(0); |
|
|
setShowSuggestions(false); |
|
|
setIsLoadingSuggestions(false); |
|
|
setIsPerfectMatch(false); |
|
|
}, []); |
|
|
|
|
|
const navigateUp = useCallback(() => { |
|
|
if (suggestions.length === 0) return; |
|
|
|
|
|
setActiveSuggestionIndex((prevActiveIndex) => { |
|
|
|
|
|
const newActiveIndex = |
|
|
prevActiveIndex <= 0 ? suggestions.length - 1 : prevActiveIndex - 1; |
|
|
|
|
|
|
|
|
setVisibleStartIndex((prevVisibleStart) => { |
|
|
|
|
|
if ( |
|
|
newActiveIndex === suggestions.length - 1 && |
|
|
suggestions.length > MAX_SUGGESTIONS_TO_SHOW |
|
|
) { |
|
|
return Math.max(0, suggestions.length - MAX_SUGGESTIONS_TO_SHOW); |
|
|
} |
|
|
|
|
|
if (newActiveIndex < prevVisibleStart) { |
|
|
return newActiveIndex; |
|
|
} |
|
|
|
|
|
return prevVisibleStart; |
|
|
}); |
|
|
|
|
|
return newActiveIndex; |
|
|
}); |
|
|
}, [suggestions.length]); |
|
|
|
|
|
const navigateDown = useCallback(() => { |
|
|
if (suggestions.length === 0) return; |
|
|
|
|
|
setActiveSuggestionIndex((prevActiveIndex) => { |
|
|
|
|
|
const newActiveIndex = |
|
|
prevActiveIndex >= suggestions.length - 1 ? 0 : prevActiveIndex + 1; |
|
|
|
|
|
|
|
|
setVisibleStartIndex((prevVisibleStart) => { |
|
|
|
|
|
if ( |
|
|
newActiveIndex === 0 && |
|
|
suggestions.length > MAX_SUGGESTIONS_TO_SHOW |
|
|
) { |
|
|
return 0; |
|
|
} |
|
|
|
|
|
const visibleEndIndex = prevVisibleStart + MAX_SUGGESTIONS_TO_SHOW; |
|
|
if (newActiveIndex >= visibleEndIndex) { |
|
|
return newActiveIndex - MAX_SUGGESTIONS_TO_SHOW + 1; |
|
|
} |
|
|
|
|
|
return prevVisibleStart; |
|
|
}); |
|
|
|
|
|
return newActiveIndex; |
|
|
}); |
|
|
}, [suggestions.length]); |
|
|
return { |
|
|
suggestions, |
|
|
activeSuggestionIndex, |
|
|
visibleStartIndex, |
|
|
showSuggestions, |
|
|
isLoadingSuggestions, |
|
|
isPerfectMatch, |
|
|
|
|
|
setSuggestions, |
|
|
setShowSuggestions, |
|
|
setActiveSuggestionIndex, |
|
|
setVisibleStartIndex, |
|
|
setIsLoadingSuggestions, |
|
|
setIsPerfectMatch, |
|
|
|
|
|
resetCompletionState, |
|
|
navigateUp, |
|
|
navigateDown, |
|
|
}; |
|
|
} |
|
|
|