From 87e83516a1b3cf3480fc8aa7238d8e17df6f3d1b Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Mon, 11 Apr 2022 10:47:51 +0900 Subject: [PATCH] filter topSongs backup search by artistName --- app/hooks/query.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/hooks/query.ts b/app/hooks/query.ts index f4959f4..61318e3 100644 --- a/app/hooks/query.ts +++ b/app/hooks/query.ts @@ -65,14 +65,23 @@ export const useQueryArtistTopSongs = (artistName?: string) => { const querySuccess = query.isFetched && query.isSuccess && query.data && query.data.length > 0 const fetchSearchResults = useFetchSearchResults() + const [artistCount, albumCount, songCount] = [0, 0, 300] const backupQuery = useQuery( - qk.search(artistName || '', 0, 0, 50), - () => fetchSearchResults({ query: artistName as string, songCount: 50 }), + qk.search(artistName || '', artistCount, albumCount, songCount), + () => fetchSearchResults({ query: artistName as string, artistCount, albumCount, songCount }), { - enabled: !!artistName && !query.isFetching && !querySuccess, - select: data => + select: data => { + const artistNameLower = artistName?.toLowerCase() + const songs = data.songs.filter(s => s.artist?.toLowerCase() === artistNameLower) + // sortBy is a stable sort, so that this doesn't change order arbitrarily and re-render - _.sortBy(data.songs, [s => -(s.playCount || 0), s => -(s.averageRating || 0), s => -(s.userRating || 0)]), + return _.sortBy(songs, [ + s => -(s.playCount || 0), + s => -(s.averageRating || 0), + s => -(s.userRating || 0), + ]).slice(0, 50) + }, + enabled: !!artistName && !query.isFetching && !querySuccess, staleTime: Infinity, cacheTime: Infinity, notifyOnChangeProps: ['data', 'isError'],