move external playback fetch to context

This commit is contained in:
jeffvli
2026-02-07 01:26:04 -08:00
parent 812ca5302a
commit bec6464a44
3 changed files with 24 additions and 34 deletions
@@ -1,11 +1,9 @@
import { useQueryClient } from '@tanstack/react-query';
import { useEffect, useMemo, useRef } from 'react';
import { useNavigate } from 'react-router';
import { getTitlePath } from '/@/renderer/components/item-list/helpers/get-title-path';
import { ItemListStateItemWithRequiredProperties } from '/@/renderer/components/item-list/helpers/item-list-state';
import { DefaultItemControlProps, ItemControls } from '/@/renderer/components/item-list/types';
import { albumQueries } from '/@/renderer/features/albums/api/album-api';
import { ContextMenuController } from '/@/renderer/features/context-menu/context-menu-controller';
import { usePlayer } from '/@/renderer/features/player/context/player-context';
import { useSetFavorite } from '/@/renderer/features/shared/hooks/use-set-favorite';
@@ -36,7 +34,6 @@ const itemTypeMapping = {
export const useDefaultItemListControls = (args?: UseDefaultItemListControlsArgs) => {
const player = usePlayer();
const queryClient = useQueryClient();
const navigate = useNavigate();
const navigateRef = useRef(navigate);
const setFavorite = useSetFavorite();
@@ -391,34 +388,9 @@ export const useDefaultItemListControls = (args?: UseDefaultItemListControlsArgs
(item as Song & { _serverType?: ServerType })._serverType ===
ServerType.EXTERNAL;
if (isExternal) {
if (
itemType === LibraryItem.SONG ||
itemType === LibraryItem.PLAYLIST_SONG ||
(item as { _itemType?: LibraryItem })._itemType === LibraryItem.SONG
) {
player.addToQueueByData([item as Song], playType, item.id);
return;
}
if (itemType === LibraryItem.ALBUM) {
(async () => {
try {
const album = await queryClient.fetchQuery(
albumQueries.detail({
query: { id: item.id },
serverId: 'musicbrainz',
}),
);
const songs = album?.songs ?? [];
if (songs.length > 0) {
player.addToQueueByData(songs, playType);
}
} catch {
console.error('Error fetching album songs for item', item);
}
})();
return;
}
if (isExternal && itemType === LibraryItem.SONG) {
player.addToQueueByData([item as Song], playType, item.id);
return;
}
player.addToQueueByFetch(item._serverId, [item.id], itemType, playType);
@@ -458,7 +430,6 @@ export const useDefaultItemListControls = (args?: UseDefaultItemListControlsArgs
onColumnResized,
overrides,
player,
queryClient,
setFavorite,
setRating,
]);
@@ -237,7 +237,6 @@ const EmptyQueueDropZone = () => {
const sourceServerId = (
args.source.item?.[0] as unknown as { _serverId: string }
)?._serverId;
const sourceItemType = args.source.itemType as LibraryItem;
switch (args.source.type) {
@@ -297,7 +296,7 @@ const EmptyQueueDropZone = () => {
const folderIds = folders.map((folder) => folder.id);
// Handle folders: fetch and add to queue
if (folderIds.length > 0) {
if (folderIds.length > 0 && sourceServerId) {
playerContext.addToQueueByFetch(
sourceServerId,
folderIds,
@@ -890,6 +890,9 @@ export const usePlayer = () => {
* @param args - The arguments to use to fetch the data
* @returns The songs to add to the queue
*/
const EXTERNAL_SERVER_ID = 'musicbrainz';
export async function fetchSongsByItemType(
queryClient: QueryClient,
serverId: string,
@@ -901,6 +904,23 @@ export async function fetchSongsByItemType(
) {
const songs: Song[] = [];
if (serverId === EXTERNAL_SERVER_ID) {
if (args.itemType === LibraryItem.ALBUM) {
for (const albumId of args.id) {
const album = await queryClient.fetchQuery(
albumQueries.detail({
query: { id: albumId },
serverId: EXTERNAL_SERVER_ID,
}),
);
songs.push(...(album?.songs ?? []));
}
return songs;
}
return songs;
}
switch (args.itemType) {
case LibraryItem.ALBUM: {
const albumSongsResponse = await getAlbumSongsById({