feat: Cancel search request when menu is closed
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
@@ -310,6 +310,7 @@ function Menu({data, duration, setDuration, end, setEnd, slider, setSlider, subm
|
||||
const [isSearching, setIsSearching] = useState(false);
|
||||
const [searchResults, setSearchResults] = useState(null);
|
||||
const [activeSearchResult, setActiveSearchResult] = useState(null);
|
||||
const cancelTokenSourceRef = useRef(null);
|
||||
const scrollContainerRef = useRef(null);
|
||||
const scrollPositionRef = useRef(0);
|
||||
const [lastDrawnItemsForExclusion, setLastDrawnItemsForExclusion] = useState(null);
|
||||
@@ -332,6 +333,12 @@ function Menu({data, duration, setDuration, end, setEnd, slider, setSlider, subm
|
||||
};
|
||||
}, [searchResults]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!submenu && isSearching && cancelTokenSourceRef.current) {
|
||||
cancelTokenSourceRef.current.cancel('Search cancelled because menu was closed.');
|
||||
}
|
||||
}, [submenu, isSearching]);
|
||||
|
||||
const handleSliderChange = (newSliderValue) => {
|
||||
setActiveSearchResult(null);
|
||||
setSlider(newSliderValue);
|
||||
@@ -574,6 +581,7 @@ function Menu({data, duration, setDuration, end, setEnd, slider, setSlider, subm
|
||||
}));
|
||||
|
||||
setIsSearching(true);
|
||||
cancelTokenSourceRef.current = axios.CancelToken.source();
|
||||
|
||||
try {
|
||||
const api_key = localStorage.getItem('api_key');
|
||||
@@ -584,17 +592,25 @@ function Menu({data, duration, setDuration, end, setEnd, slider, setSlider, subm
|
||||
const res = await axios.post(
|
||||
'https://sensors-api.dns.t0.vc/search/owntracks/OwnTracks',
|
||||
{ areas: areas },
|
||||
{ params: params }
|
||||
{
|
||||
params: params,
|
||||
cancelToken: cancelTokenSourceRef.current.token,
|
||||
}
|
||||
);
|
||||
|
||||
setActiveSearchResult(null);
|
||||
const sortedData = res.data.sort((a, b) => b.start - a.start);
|
||||
setSearchResults(sortedData);
|
||||
} catch (error) {
|
||||
if (axios.isCancel(error)) {
|
||||
// Request was canceled, do nothing
|
||||
} else {
|
||||
console.error('Error during area search:', error);
|
||||
alert('An error occurred during the search.');
|
||||
}
|
||||
} finally {
|
||||
setIsSearching(false);
|
||||
cancelTokenSourceRef.current = null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user