2022-12-27 13:52:50 -08:00
|
|
|
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
2023-05-20 21:31:00 -07:00
|
|
|
import { useCallback, useRef } from 'react';
|
2023-03-05 18:28:26 -08:00
|
|
|
import { useParams, useSearchParams } from 'react-router-dom';
|
2023-05-20 21:31:00 -07:00
|
|
|
import { SongListQuery, LibraryItem } from '/@/renderer/api/types';
|
|
|
|
|
import { usePlayQueueAdd } from '/@/renderer/features/player';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { AnimatedPage } from '/@/renderer/features/shared';
|
2022-12-27 13:52:50 -08:00
|
|
|
import { SongListContent } from '/@/renderer/features/songs/components/song-list-content';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { SongListHeader } from '/@/renderer/features/songs/components/song-list-header';
|
2023-03-05 18:28:26 -08:00
|
|
|
import { SongListContext } from '/@/renderer/features/songs/context/song-list-context';
|
2023-01-07 03:28:03 -08:00
|
|
|
import { useSongList } from '/@/renderer/features/songs/queries/song-list-query';
|
2023-03-05 18:28:26 -08:00
|
|
|
import { generatePageKey, useCurrentServer, useSongListFilter } from '/@/renderer/store';
|
2023-05-20 21:31:00 -07:00
|
|
|
import { Play } from '/@/renderer/types';
|
2022-12-19 15:59:14 -08:00
|
|
|
|
|
|
|
|
const TrackListRoute = () => {
|
2022-12-27 13:52:50 -08:00
|
|
|
const tableRef = useRef<AgGridReactType | null>(null);
|
2023-03-05 18:28:26 -08:00
|
|
|
const server = useCurrentServer();
|
2023-01-15 20:39:43 -08:00
|
|
|
const [searchParams] = useSearchParams();
|
2023-03-05 18:28:26 -08:00
|
|
|
const { albumArtistId } = useParams();
|
|
|
|
|
const pageKey = generatePageKey(
|
|
|
|
|
'song',
|
|
|
|
|
albumArtistId ? `${albumArtistId}_${server?.id}` : undefined,
|
|
|
|
|
);
|
2023-01-15 20:39:43 -08:00
|
|
|
|
2023-05-20 21:31:00 -07:00
|
|
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
2023-03-05 18:28:26 -08:00
|
|
|
const songListFilter = useSongListFilter({ id: albumArtistId, key: pageKey });
|
2023-04-30 22:01:52 -07:00
|
|
|
const itemCountCheck = useSongList({
|
|
|
|
|
options: {
|
|
|
|
|
cacheTime: 1000 * 60,
|
|
|
|
|
staleTime: 1000 * 60,
|
|
|
|
|
},
|
|
|
|
|
query: {
|
2023-01-07 03:28:03 -08:00
|
|
|
limit: 1,
|
|
|
|
|
startIndex: 0,
|
2023-03-05 18:28:26 -08:00
|
|
|
...songListFilter,
|
2023-01-07 03:28:03 -08:00
|
|
|
},
|
2023-04-30 22:01:52 -07:00
|
|
|
serverId: server?.id,
|
|
|
|
|
});
|
2023-01-07 03:28:03 -08:00
|
|
|
|
|
|
|
|
const itemCount =
|
|
|
|
|
itemCountCheck.data?.totalRecordCount === null
|
|
|
|
|
? undefined
|
|
|
|
|
: itemCountCheck.data?.totalRecordCount;
|
|
|
|
|
|
2023-05-20 21:31:00 -07:00
|
|
|
const handlePlay = useCallback(
|
|
|
|
|
async (args: { initialSongId?: string; playType: Play }) => {
|
|
|
|
|
if (!itemCount || itemCount === 0) return;
|
|
|
|
|
const { initialSongId, playType } = args;
|
|
|
|
|
const query: SongListQuery = { startIndex: 0, ...songListFilter };
|
|
|
|
|
|
|
|
|
|
if (albumArtistId) {
|
|
|
|
|
handlePlayQueueAdd?.({
|
|
|
|
|
byItemType: {
|
|
|
|
|
id: [albumArtistId],
|
|
|
|
|
type: LibraryItem.ALBUM_ARTIST,
|
|
|
|
|
},
|
|
|
|
|
initialSongId,
|
|
|
|
|
playType,
|
|
|
|
|
query,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
handlePlayQueueAdd?.({
|
|
|
|
|
byItemType: {
|
|
|
|
|
id: [],
|
|
|
|
|
type: LibraryItem.SONG,
|
|
|
|
|
},
|
|
|
|
|
initialSongId,
|
|
|
|
|
playType,
|
|
|
|
|
query,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[albumArtistId, handlePlayQueueAdd, itemCount, songListFilter],
|
|
|
|
|
);
|
|
|
|
|
|
2022-12-19 15:59:14 -08:00
|
|
|
return (
|
|
|
|
|
<AnimatedPage>
|
2023-05-20 21:31:00 -07:00
|
|
|
<SongListContext.Provider value={{ handlePlay, id: albumArtistId, pageKey }}>
|
2023-03-05 18:28:26 -08:00
|
|
|
<SongListHeader
|
|
|
|
|
itemCount={itemCount}
|
|
|
|
|
tableRef={tableRef}
|
|
|
|
|
title={searchParams.get('artistName') || undefined}
|
|
|
|
|
/>
|
|
|
|
|
<SongListContent
|
|
|
|
|
itemCount={itemCount}
|
|
|
|
|
tableRef={tableRef}
|
|
|
|
|
/>
|
|
|
|
|
</SongListContext.Provider>
|
2022-12-19 15:59:14 -08:00
|
|
|
</AnimatedPage>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default TrackListRoute;
|