Disabling search temporarily when suggestions are fetched to prevent accidental clicks.
Some checks are pending
CodeQL / Analyze (push) Waiting to run

This commit is contained in:
Roland 2025-08-26 17:44:31 -06:00
parent cd2fdce90a
commit add6bfb1b3
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 28 additions and 1 deletions

View file

@ -208,6 +208,10 @@ export function useSearchSuggestions(defaultSearchStr, defaultTagNames, anyTag)
// The results summarized.
const [suggestionsSums, setSuggestionsSums] = useState({});
// Disable selecting a result briefly after new suggestions arrive.
const [selectDisabled, setSelectDisabled] = React.useState(false);
const selectDisableTimerRef = React.useRef(null);
const noResults = [{title: 'No results'}];
const normalizeSuggestionsResults = (newSuggestions) => {
@ -303,17 +307,40 @@ export function useSearchSuggestions(defaultSearchStr, defaultTagNames, anyTag)
});
}
const suggestionSelectDelay = 250;
React.useEffect(() => {
setSuggestionsSums({});
setSuggestionsResults({});
if (!_.isEmpty(suggestions)) {
// Start a brief window where selecting results is disabled to prevent accidental clicks
// when the dropdown re-renders with new suggestions.
if (selectDisableTimerRef.current) {
clearTimeout(selectDisableTimerRef.current);
}
setSelectDisabled(true);
selectDisableTimerRef.current = setTimeout(() => setSelectDisabled(false), suggestionSelectDelay);
normalizeSuggestionsResults(suggestions);
}
}, [JSON.stringify(suggestions)]);
// Clear timer on unmount.
React.useEffect(() => {
return () => {
if (selectDisableTimerRef.current) {
clearTimeout(selectDisableTimerRef.current);
}
};
}, []);
// User clicked on a result in the dropdown.
const handleResultSelect = ({result}) => {
if (selectDisabled) {
console.debug('Selection temporarily disabled after suggestions update');
return;
}
if (result.location) {
console.info(`useSearchSuggestions Navigating: ${result.location}`)
navigate(result.location);

View file

@ -29,4 +29,4 @@ tqdm==4.67.1
vininfo[cli]==1.8.0
websockets==13.1
webvtt-py==0.5.1
yt-dlp==2025.8.11
yt-dlp==2025.8.22