2023-07-20 00:34:07 -07:00
|
|
|
import { useCallback, useMemo, useRef } from 'react';
|
2023-08-08 09:18:43 -07:00
|
|
|
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
|
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2024-04-20 06:14:31 +00:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-07-20 00:34:07 -07:00
|
|
|
import { useParams, useSearchParams } from 'react-router-dom';
|
|
|
|
|
import { api } from '/@/renderer/api';
|
|
|
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
2024-09-26 04:23:08 +00:00
|
|
|
import { AlbumListQuery, GenreListSort, LibraryItem, SortOrder } from '/@/renderer/api/types';
|
2023-05-09 02:25:57 -07:00
|
|
|
import { VirtualInfiniteGridRef } from '/@/renderer/components/virtual-grid';
|
2023-07-20 00:34:07 -07:00
|
|
|
import { ListContext } from '/@/renderer/context/list-context';
|
2022-12-24 14:07:58 -08:00
|
|
|
import { AlbumListContent } from '/@/renderer/features/albums/components/album-list-content';
|
2023-07-20 00:34:07 -07:00
|
|
|
import { AlbumListHeader } from '/@/renderer/features/albums/components/album-list-header';
|
|
|
|
|
import { usePlayQueueAdd } from '/@/renderer/features/player';
|
|
|
|
|
import { AnimatedPage } from '/@/renderer/features/shared';
|
|
|
|
|
import { queryClient } from '/@/renderer/lib/react-query';
|
|
|
|
|
import { useCurrentServer, useListFilterByKey } from '/@/renderer/store';
|
|
|
|
|
import { Play } from '/@/renderer/types';
|
2024-04-20 06:14:31 +00:00
|
|
|
import { useGenreList } from '/@/renderer/features/genres';
|
|
|
|
|
import { titleCase } from '/@/renderer/utils';
|
2024-09-26 04:23:08 +00:00
|
|
|
import { useAlbumListCount } from '/@/renderer/features/albums/queries/album-list-count-query';
|
2022-12-19 15:59:14 -08:00
|
|
|
|
|
|
|
|
const AlbumListRoute = () => {
|
2024-04-20 06:14:31 +00:00
|
|
|
const { t } = useTranslation();
|
2023-07-01 19:10:05 -07:00
|
|
|
const gridRef = useRef<VirtualInfiniteGridRef | null>(null);
|
|
|
|
|
const tableRef = useRef<AgGridReactType | null>(null);
|
|
|
|
|
const server = useCurrentServer();
|
|
|
|
|
const [searchParams] = useSearchParams();
|
2024-04-20 06:14:31 +00:00
|
|
|
const { albumArtistId, genreId } = useParams();
|
2023-07-20 00:34:07 -07:00
|
|
|
const pageKey = albumArtistId ? `albumArtistAlbum` : 'album';
|
|
|
|
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
2023-01-15 16:22:07 -08:00
|
|
|
|
2023-07-20 00:34:07 -07:00
|
|
|
const customFilters = useMemo(() => {
|
2023-08-08 09:18:43 -07:00
|
|
|
const value = {
|
2023-07-20 00:34:07 -07:00
|
|
|
...(albumArtistId && { artistIds: [albumArtistId] }),
|
2024-04-20 06:14:31 +00:00
|
|
|
...(genreId && {
|
2024-09-26 04:23:08 +00:00
|
|
|
genres: [genreId],
|
2024-04-20 06:14:31 +00:00
|
|
|
}),
|
2023-07-20 00:34:07 -07:00
|
|
|
};
|
2023-08-08 09:18:43 -07:00
|
|
|
|
|
|
|
|
if (isEmpty(value)) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
2024-04-20 06:14:31 +00:00
|
|
|
}, [albumArtistId, genreId]);
|
2023-03-05 18:28:26 -08:00
|
|
|
|
2024-09-26 04:23:08 +00:00
|
|
|
const albumListFilter = useListFilterByKey<AlbumListQuery>({
|
2023-07-20 00:34:07 -07:00
|
|
|
filter: customFilters,
|
|
|
|
|
key: pageKey,
|
|
|
|
|
});
|
2023-01-07 03:28:03 -08:00
|
|
|
|
2024-04-20 06:14:31 +00:00
|
|
|
const genreList = useGenreList({
|
|
|
|
|
options: {
|
|
|
|
|
cacheTime: 1000 * 60 * 60,
|
|
|
|
|
enabled: !!genreId,
|
|
|
|
|
},
|
|
|
|
|
query: {
|
|
|
|
|
sortBy: GenreListSort.NAME,
|
|
|
|
|
sortOrder: SortOrder.ASC,
|
|
|
|
|
startIndex: 0,
|
|
|
|
|
},
|
|
|
|
|
serverId: server?.id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const genreTitle = useMemo(() => {
|
|
|
|
|
if (!genreList.data) return '';
|
|
|
|
|
const genre = genreList.data.items.find((g) => g.id === genreId);
|
|
|
|
|
|
|
|
|
|
if (!genre) return 'Unknown';
|
|
|
|
|
|
|
|
|
|
return genre?.name;
|
|
|
|
|
}, [genreId, genreList.data]);
|
|
|
|
|
|
2024-09-26 04:23:08 +00:00
|
|
|
const itemCountCheck = useAlbumListCount({
|
2023-07-01 19:10:05 -07:00
|
|
|
options: {
|
|
|
|
|
cacheTime: 1000 * 60,
|
|
|
|
|
staleTime: 1000 * 60,
|
|
|
|
|
},
|
|
|
|
|
query: {
|
|
|
|
|
...albumListFilter,
|
|
|
|
|
},
|
|
|
|
|
serverId: server?.id,
|
|
|
|
|
});
|
2023-01-07 03:28:03 -08:00
|
|
|
|
2024-09-26 04:23:08 +00:00
|
|
|
const itemCount = itemCountCheck.data === null ? undefined : itemCountCheck.data;
|
2022-12-24 20:20:17 -08:00
|
|
|
|
2023-07-20 00:34:07 -07:00
|
|
|
const handlePlay = useCallback(
|
|
|
|
|
async (args: { initialSongId?: string; playType: Play }) => {
|
|
|
|
|
if (!itemCount || itemCount === 0) return;
|
|
|
|
|
const { playType } = args;
|
|
|
|
|
const query = {
|
|
|
|
|
...albumListFilter,
|
|
|
|
|
...customFilters,
|
2024-09-26 04:23:08 +00:00
|
|
|
startIndex: 0,
|
2023-07-20 00:34:07 -07:00
|
|
|
};
|
|
|
|
|
const queryKey = queryKeys.albums.list(server?.id || '', query);
|
|
|
|
|
|
|
|
|
|
const albumListRes = await queryClient.fetchQuery({
|
|
|
|
|
queryFn: ({ signal }) => {
|
|
|
|
|
return api.controller.getAlbumList({
|
|
|
|
|
apiClientProps: { server, signal },
|
|
|
|
|
query,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
queryKey,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const albumIds = albumListRes?.items?.map((a) => a.id) || [];
|
|
|
|
|
|
|
|
|
|
handlePlayQueueAdd?.({
|
|
|
|
|
byItemType: {
|
|
|
|
|
id: albumIds,
|
|
|
|
|
type: LibraryItem.ALBUM,
|
|
|
|
|
},
|
|
|
|
|
playType,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[albumListFilter, customFilters, handlePlayQueueAdd, itemCount, server],
|
|
|
|
|
);
|
|
|
|
|
|
2023-08-08 00:38:32 -07:00
|
|
|
const providerValue = useMemo(() => {
|
|
|
|
|
return {
|
|
|
|
|
customFilters,
|
|
|
|
|
handlePlay,
|
2024-04-20 06:14:31 +00:00
|
|
|
id: albumArtistId ?? genreId,
|
2023-08-08 00:38:32 -07:00
|
|
|
pageKey,
|
|
|
|
|
};
|
2024-04-20 06:14:31 +00:00
|
|
|
}, [albumArtistId, customFilters, genreId, handlePlay, pageKey]);
|
|
|
|
|
|
|
|
|
|
const artist = searchParams.get('artistName');
|
|
|
|
|
const title = artist
|
2025-05-07 01:15:00 -07:00
|
|
|
? t('page.albumList.artistAlbums', { artist, postProcess: 'sentenceCase' })
|
2024-04-20 06:14:31 +00:00
|
|
|
: genreId
|
2024-08-23 08:19:27 -07:00
|
|
|
? t('page.albumList.genreAlbums', { genre: titleCase(genreTitle) })
|
|
|
|
|
: undefined;
|
2023-08-08 00:38:32 -07:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
|
|
|
|
<AnimatedPage>
|
2023-08-08 00:38:32 -07:00
|
|
|
<ListContext.Provider value={providerValue}>
|
2023-07-01 19:10:05 -07:00
|
|
|
<AlbumListHeader
|
2024-04-20 06:14:31 +00:00
|
|
|
genreId={genreId}
|
2023-07-01 19:10:05 -07:00
|
|
|
gridRef={gridRef}
|
|
|
|
|
itemCount={itemCount}
|
|
|
|
|
tableRef={tableRef}
|
2024-04-20 06:14:31 +00:00
|
|
|
title={title}
|
2023-07-01 19:10:05 -07:00
|
|
|
/>
|
|
|
|
|
<AlbumListContent
|
|
|
|
|
gridRef={gridRef}
|
|
|
|
|
itemCount={itemCount}
|
|
|
|
|
tableRef={tableRef}
|
|
|
|
|
/>
|
2023-07-20 00:34:07 -07:00
|
|
|
</ListContext.Provider>
|
2023-07-01 19:10:05 -07:00
|
|
|
</AnimatedPage>
|
|
|
|
|
);
|
2022-12-19 15:59:14 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default AlbumListRoute;
|