fix: Restore playlist switching functionality - Add previousPlaylistRef to properly detect playlist changes - Fix playlist change detection logic that was broken by ref updates - Ensure playlist switching works while maintaining infinite scroll fix
This commit is contained in:
parent
08de2afa0e
commit
e8bb2a4326
@ -24,6 +24,7 @@ export const usePaginatedSongs = (options: UsePaginatedSongsOptions = {}) => {
|
||||
const loadingRef = useRef(false);
|
||||
const currentPlaylistRef = useRef(playlistName);
|
||||
const currentSearchQueryRef = useRef(searchQuery);
|
||||
const previousPlaylistRef = useRef(playlistName);
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
|
||||
// Cleanup function to prevent memory leaks
|
||||
@ -134,31 +135,24 @@ export const usePaginatedSongs = (options: UsePaginatedSongsOptions = {}) => {
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Update refs when values change
|
||||
useEffect(() => {
|
||||
currentPlaylistRef.current = playlistName;
|
||||
}, [playlistName]);
|
||||
|
||||
useEffect(() => {
|
||||
currentSearchQueryRef.current = searchQuery;
|
||||
}, [searchQuery]);
|
||||
|
||||
// Handle playlist changes
|
||||
useEffect(() => {
|
||||
if (currentPlaylistRef.current !== playlistName) {
|
||||
if (!isInitialLoad && previousPlaylistRef.current !== playlistName) {
|
||||
// Update refs
|
||||
currentPlaylistRef.current = playlistName;
|
||||
if (!isInitialLoad) {
|
||||
// Clear songs for new playlist to replace them
|
||||
setSongs([]);
|
||||
setHasMore(true);
|
||||
setCurrentPage(1);
|
||||
setSearchQuery(initialSearch);
|
||||
setError(null);
|
||||
// Use setTimeout to avoid the dependency issue
|
||||
setTimeout(() => {
|
||||
loadPage(1, initialSearch, playlistName);
|
||||
}, 0);
|
||||
}
|
||||
currentSearchQueryRef.current = searchQuery;
|
||||
previousPlaylistRef.current = playlistName;
|
||||
|
||||
// Clear songs for new playlist to replace them
|
||||
setSongs([]);
|
||||
setHasMore(true);
|
||||
setCurrentPage(1);
|
||||
setSearchQuery(initialSearch);
|
||||
setError(null);
|
||||
// Use setTimeout to avoid the dependency issue
|
||||
setTimeout(() => {
|
||||
loadPage(1, initialSearch, playlistName);
|
||||
}, 0);
|
||||
}
|
||||
}, [playlistName, isInitialLoad, initialSearch, loadPage]);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user