feishin/src/renderer/features/songs/routes/song-list-route.tsx

93 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-12-27 13:52:50 -08:00
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
import { useCallback, useRef } from 'react';
2023-03-05 18:28:26 -08:00
import { useParams, useSearchParams } from 'react-router-dom';
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';
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';
import { Play } from '/@/renderer/types';
2022-12-19 15:59:14 -08:00
const TrackListRoute = () => {
2023-07-01 19:10:05 -07:00
const tableRef = useRef<AgGridReactType | null>(null);
const server = useCurrentServer();
const [searchParams] = useSearchParams();
const { albumArtistId } = useParams();
const pageKey = generatePageKey(
'song',
albumArtistId ? `${albumArtistId}_${server?.id}` : undefined,
);
2023-01-15 20:39:43 -08:00
2023-07-01 19:10:05 -07:00
const handlePlayQueueAdd = usePlayQueueAdd();
const songListFilter = useSongListFilter({ id: albumArtistId, key: pageKey });
const itemCountCheck = useSongList({
options: {
cacheTime: 1000 * 60,
staleTime: 1000 * 60,
},
query: {
limit: 1,
startIndex: 0,
...songListFilter,
},
serverId: server?.id,
});
2023-07-01 19:10:05 -07:00
const itemCount =
itemCountCheck.data?.totalRecordCount === null
? undefined
: itemCountCheck.data?.totalRecordCount;
2023-07-01 19:10:05 -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 };
2023-07-01 19:10:05 -07:00
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],
);
2023-07-01 19:10:05 -07:00
return (
<AnimatedPage>
<SongListContext.Provider value={{ handlePlay, id: albumArtistId, pageKey }}>
<SongListHeader
itemCount={itemCount}
tableRef={tableRef}
title={searchParams.get('artistName') || undefined}
/>
<SongListContent
itemCount={itemCount}
tableRef={tableRef}
/>
</SongListContext.Provider>
</AnimatedPage>
);
2022-12-19 15:59:14 -08:00
};
export default TrackListRoute;