From e2d56c70b1cdc1f12bb5a653b0ac3642512a9faa Mon Sep 17 00:00:00 2001 From: jeffvli Date: Sun, 28 Dec 2025 03:47:01 -0800 Subject: [PATCH] add optimistic update for top songs list (#1414) --- .../mutations/favorite-optimistic-updates.ts | 35 +++++++++++++++++++ .../mutations/rating-optimistic-updates.ts | 34 ++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/renderer/features/shared/mutations/favorite-optimistic-updates.ts b/src/renderer/features/shared/mutations/favorite-optimistic-updates.ts index cbd1c26b2..05a48c556 100644 --- a/src/renderer/features/shared/mutations/favorite-optimistic-updates.ts +++ b/src/renderer/features/shared/mutations/favorite-optimistic-updates.ts @@ -15,6 +15,7 @@ import { PlaylistSongListResponse, Song, SongDetailResponse, + TopSongListResponse, } from '/@/shared/types/domain-types'; export interface PreviousQueryData { @@ -610,6 +611,40 @@ export const applyFavoriteOptimisticUpdates = ( }); } + const topSongsQueryKey = queryKeys.albumArtists.topSongs( + variables.apiClientProps.serverId, + ); + + const topSongsQueries = queryClient.getQueriesData({ + exact: false, + queryKey: topSongsQueryKey, + }); + + if (topSongsQueries.length) { + topSongsQueries.forEach(([queryKey, data]) => { + if (data) { + previousQueries.push({ data, queryKey }); + queryClient.setQueryData( + queryKey, + (prev: TopSongListResponse | undefined) => { + if (prev) { + return { + ...prev, + items: prev.items.map((item: Song) => + itemIdSet.has(item.id) + ? { ...item, userFavorite: isFavorite } + : item, + ), + }; + } + + return prev; + }, + ); + } + }); + } + break; } } diff --git a/src/renderer/features/shared/mutations/rating-optimistic-updates.ts b/src/renderer/features/shared/mutations/rating-optimistic-updates.ts index 78fdf8d4c..3585c2d9b 100644 --- a/src/renderer/features/shared/mutations/rating-optimistic-updates.ts +++ b/src/renderer/features/shared/mutations/rating-optimistic-updates.ts @@ -15,6 +15,7 @@ import { SetRatingArgs, Song, SongDetailResponse, + TopSongListResponse, } from '/@/shared/types/domain-types'; export const applyRatingOptimisticUpdates = ( @@ -495,6 +496,39 @@ export const applyRatingOptimisticUpdates = ( }); } + const topSongsQueryKey = queryKeys.albumArtists.topSongs( + variables.apiClientProps.serverId, + ); + + const topSongsQueries = queryClient.getQueriesData({ + exact: false, + queryKey: topSongsQueryKey, + }); + + if (topSongsQueries.length) { + topSongsQueries.forEach(([queryKey, data]) => { + if (data) { + previousQueries.push({ data, queryKey }); + queryClient.setQueryData( + queryKey, + (prev: TopSongListResponse | undefined) => { + if (prev) { + return { + ...prev, + items: prev.items.map((item: Song) => + itemIdSet.has(item.id) + ? { ...item, userRating: rating } + : item, + ), + }; + } + return prev; + }, + ); + } + }); + } + break; } }