diff --git a/packages/frontend/src/hooks/usePaginatedSongs.ts b/packages/frontend/src/hooks/usePaginatedSongs.ts index 537564e..620206e 100644 --- a/packages/frontend/src/hooks/usePaginatedSongs.ts +++ b/packages/frontend/src/hooks/usePaginatedSongs.ts @@ -23,6 +23,7 @@ export const usePaginatedSongs = (options: UsePaginatedSongsOptions = {}) => { const loadingRef = useRef(false); const currentPlaylistRef = useRef(playlistName); + const currentSearchQueryRef = useRef(searchQuery); const abortControllerRef = useRef(null); // Cleanup function to prevent memory leaks @@ -38,8 +39,8 @@ export const usePaginatedSongs = (options: UsePaginatedSongsOptions = {}) => { const loadPage = useCallback(async (page: number, search?: string, targetPlaylist?: string) => { if (loadingRef.current) return; - const searchToUse = search ?? searchQuery; - const playlistToUse = targetPlaylist ?? playlistName; + const searchToUse = search ?? currentSearchQueryRef.current; + const playlistToUse = targetPlaylist ?? currentPlaylistRef.current; // Cleanup previous request cleanup(); @@ -133,6 +134,15 @@ 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) {