From 3bc0ea16bcca40de58b016a643a31fd8e0d4af06 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Tue, 8 Nov 2022 15:16:22 -0800 Subject: [PATCH] Wrap queue handler in callback --- .../player/hooks/use-playqueue-handler.ts | 112 ++++++++++-------- 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/src/renderer/features/player/hooks/use-playqueue-handler.ts b/src/renderer/features/player/hooks/use-playqueue-handler.ts index 4ffe49c66..21da61749 100644 --- a/src/renderer/features/player/hooks/use-playqueue-handler.ts +++ b/src/renderer/features/player/hooks/use-playqueue-handler.ts @@ -1,3 +1,4 @@ +import { useCallback } from 'react'; import { useQueryClient } from '@tanstack/react-query'; import { api } from '@/renderer/api'; import { queryKeys } from '@/renderer/api/query-keys'; @@ -20,64 +21,75 @@ export const usePlayQueueHandler = () => { const addToQueue = usePlayerStore((state) => state.addToQueue); const playerType = useSettingsStore((state) => state.player.type); - const handlePlayQueueAdd = async (options: PlayQueueAddOptions) => { - if (options.byData) { - // dispatchSongsToQueue(options.byData, options.play); - } - - if (options.byItemType) { - const deviceId = localStorage.getItem('device_id'); - - if (!deviceId || !options.byItemType.id) return; - - let songs = null; - if (options.byItemType.type === LibraryItem.ALBUM) { - const albumDetail = await queryClient.fetchQuery( - queryKeys.albums.detail(options.byItemType.id), - async () => - api.albums.getAlbumDetail({ - albumId: options.byItemType!.id, - serverId, - }) - ); - - songs = albumDetail.data.songs; + const handlePlayQueueAdd = useCallback( + async (options: PlayQueueAddOptions) => { + if (options.byData) { + // dispatchSongsToQueue(options.byData, options.play); } - if (!songs) return; + if (options.byItemType) { + const deviceId = localStorage.getItem('device_id'); - // * Adds server token - if (serverToken) { - songs = songs.map((song) => { - return { - ...song, - imageUrl: - song.imageUrl && isImageTokenRequired - ? `${song.imageUrl}${serverToken}` - : song.imageUrl, - streamUrl: `${song.streamUrl}${serverToken}`, - }; - }); - } + if (!deviceId || !options.byItemType.id) return; - const playerData = addToQueue(songs, options.play); + let songs = null; + if (options.byItemType.type === LibraryItem.ALBUM) { + const albumDetail = await queryClient.fetchQuery( + queryKeys.albums.detail(options.byItemType.id), + async () => + api.albums.getAlbumDetail({ + albumId: options.byItemType!.id, + serverId, + }) + ); - if (options.play === Play.NEXT || options.play === Play.LAST) { - if (playerType === PlaybackType.LOCAL) { - mpvPlayer.setQueueNext(playerData); - } - } - - if (options.play === Play.NOW) { - if (playerType === PlaybackType.LOCAL) { - mpvPlayer.setQueue(playerData); - mpvPlayer.play(); + songs = albumDetail.data.songs; } - play(); + if (!songs) return; + + // * Adds server token + if (serverToken) { + songs = songs.map((song) => { + return { + ...song, + imageUrl: + song.imageUrl && isImageTokenRequired + ? `${song.imageUrl}${serverToken}` + : song.imageUrl, + streamUrl: `${song.streamUrl}${serverToken}`, + }; + }); + } + + const playerData = addToQueue(songs, options.play); + + if (options.play === Play.NEXT || options.play === Play.LAST) { + if (playerType === PlaybackType.LOCAL) { + mpvPlayer.setQueueNext(playerData); + } + } + + if (options.play === Play.NOW) { + if (playerType === PlaybackType.LOCAL) { + mpvPlayer.setQueue(playerData); + mpvPlayer.play(); + } + + play(); + } } - } - }; + }, + [ + addToQueue, + isImageTokenRequired, + play, + playerType, + queryClient, + serverId, + serverToken, + ] + ); return handlePlayQueueAdd; };