2022-12-30 21:04:06 -08:00
|
|
|
import {
|
|
|
|
|
ALBUMARTIST_CARD_ROWS,
|
|
|
|
|
getColumnDefs,
|
|
|
|
|
TablePagination,
|
|
|
|
|
VirtualGridAutoSizerContainer,
|
|
|
|
|
VirtualInfiniteGrid,
|
|
|
|
|
VirtualInfiniteGridRef,
|
|
|
|
|
VirtualTable,
|
|
|
|
|
} from '/@/renderer/components';
|
|
|
|
|
import { AppRoute } from '/@/renderer/router/routes';
|
2023-01-05 21:59:07 -08:00
|
|
|
import { ListDisplayType, CardRow } from '/@/renderer/types';
|
2022-12-30 21:04:06 -08:00
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
|
|
|
|
import { MutableRefObject, useCallback, useMemo } from 'react';
|
|
|
|
|
import { ListOnScrollProps } from 'react-window';
|
|
|
|
|
import { api } from '/@/renderer/api';
|
|
|
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
2023-01-05 21:59:07 -08:00
|
|
|
import { AlbumArtist, AlbumArtistListSort, LibraryItem } from '/@/renderer/api/types';
|
2022-12-30 21:04:06 -08:00
|
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
import {
|
|
|
|
|
useCurrentServer,
|
|
|
|
|
useAlbumArtistListStore,
|
2023-01-03 17:41:03 -08:00
|
|
|
useAlbumArtistListItemData,
|
2022-12-30 21:04:06 -08:00
|
|
|
} from '/@/renderer/store';
|
|
|
|
|
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
|
|
|
|
import {
|
|
|
|
|
BodyScrollEvent,
|
|
|
|
|
ColDef,
|
|
|
|
|
GridReadyEvent,
|
|
|
|
|
IDatasource,
|
|
|
|
|
PaginationChangedEvent,
|
|
|
|
|
RowDoubleClickedEvent,
|
|
|
|
|
} from '@ag-grid-community/core';
|
|
|
|
|
import { AnimatePresence } from 'framer-motion';
|
|
|
|
|
import debounce from 'lodash/debounce';
|
2023-01-07 18:16:19 -08:00
|
|
|
import { useHandleTableContextMenu } from '/@/renderer/features/context-menu';
|
2022-12-30 21:04:06 -08:00
|
|
|
import { ALBUM_CONTEXT_MENU_ITEMS } from '/@/renderer/features/context-menu/context-menu-items';
|
|
|
|
|
import { generatePath, useNavigate } from 'react-router';
|
|
|
|
|
import { useAlbumArtistList } from '/@/renderer/features/artists/queries/album-artist-list-query';
|
2022-12-31 19:26:58 -08:00
|
|
|
import { usePlayQueueAdd } from '/@/renderer/features/player';
|
2023-03-05 21:02:05 -08:00
|
|
|
import { useAlbumArtistListFilter, useListStoreActions } from '../../../store/list.store';
|
|
|
|
|
import { useAlbumArtistListContext } from '/@/renderer/features/artists/context/album-artist-list-context';
|
2022-12-30 21:04:06 -08:00
|
|
|
|
|
|
|
|
interface AlbumArtistListContentProps {
|
|
|
|
|
gridRef: MutableRefObject<VirtualInfiniteGridRef | null>;
|
|
|
|
|
tableRef: MutableRefObject<AgGridReactType | null>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const AlbumArtistListContent = ({ gridRef, tableRef }: AlbumArtistListContentProps) => {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const server = useCurrentServer();
|
2022-12-31 19:26:58 -08:00
|
|
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
2022-12-30 21:04:06 -08:00
|
|
|
|
2023-03-05 21:02:05 -08:00
|
|
|
const { id, pageKey } = useAlbumArtistListContext();
|
|
|
|
|
const filter = useAlbumArtistListFilter({ id, key: pageKey });
|
|
|
|
|
const { table, grid, display } = useAlbumArtistListStore();
|
|
|
|
|
const { setTable, setTablePagination, setGrid } = useListStoreActions();
|
2023-01-03 17:41:03 -08:00
|
|
|
const { itemData, setItemData } = useAlbumArtistListItemData();
|
|
|
|
|
|
2023-03-05 21:02:05 -08:00
|
|
|
const isPaginationEnabled = display === ListDisplayType.TABLE_PAGINATED;
|
2022-12-30 21:04:06 -08:00
|
|
|
|
|
|
|
|
const checkAlbumArtistList = useAlbumArtistList(
|
|
|
|
|
{
|
|
|
|
|
limit: 1,
|
|
|
|
|
startIndex: 0,
|
2023-03-05 21:02:05 -08:00
|
|
|
...filter,
|
2022-12-30 21:04:06 -08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
cacheTime: Infinity,
|
|
|
|
|
staleTime: 60 * 1000 * 5,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2023-03-05 21:02:05 -08:00
|
|
|
const columnDefs: ColDef[] = useMemo(() => getColumnDefs(table.columns), [table.columns]);
|
2022-12-30 21:04:06 -08:00
|
|
|
|
|
|
|
|
const onTableReady = useCallback(
|
|
|
|
|
(params: GridReadyEvent) => {
|
|
|
|
|
const dataSource: IDatasource = {
|
|
|
|
|
getRows: async (params) => {
|
|
|
|
|
const limit = params.endRow - params.startRow;
|
|
|
|
|
const startIndex = params.startRow;
|
|
|
|
|
|
|
|
|
|
const queryKey = queryKeys.albumArtists.list(server?.id || '', {
|
|
|
|
|
limit,
|
|
|
|
|
startIndex,
|
2023-03-05 21:02:05 -08:00
|
|
|
...filter,
|
2022-12-30 21:04:06 -08:00
|
|
|
});
|
|
|
|
|
|
2023-01-06 03:33:11 -08:00
|
|
|
const albumArtistsRes = await queryClient.fetchQuery(
|
|
|
|
|
queryKey,
|
|
|
|
|
async ({ signal }) =>
|
|
|
|
|
api.controller.getAlbumArtistList({
|
|
|
|
|
query: {
|
|
|
|
|
limit,
|
|
|
|
|
startIndex,
|
2023-03-05 21:02:05 -08:00
|
|
|
...filter,
|
2023-01-06 03:33:11 -08:00
|
|
|
},
|
|
|
|
|
server,
|
|
|
|
|
signal,
|
|
|
|
|
}),
|
|
|
|
|
{ cacheTime: 1000 * 60 * 1 },
|
2022-12-30 21:04:06 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const albums = api.normalize.albumArtistList(albumArtistsRes, server);
|
2023-02-07 22:47:23 -08:00
|
|
|
params.successCallback(albums?.items || [], albumArtistsRes?.totalRecordCount || 0);
|
2022-12-30 21:04:06 -08:00
|
|
|
},
|
|
|
|
|
rowCount: undefined,
|
|
|
|
|
};
|
2023-01-03 17:41:03 -08:00
|
|
|
|
2022-12-30 21:04:06 -08:00
|
|
|
params.api.setDatasource(dataSource);
|
2023-03-05 21:02:05 -08:00
|
|
|
params.api.ensureIndexVisible(table.scrollOffset || 0, 'top');
|
2022-12-30 21:04:06 -08:00
|
|
|
},
|
2023-03-05 21:02:05 -08:00
|
|
|
[filter, table.scrollOffset, queryClient, server],
|
2022-12-30 21:04:06 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onTablePaginationChanged = useCallback(
|
|
|
|
|
(event: PaginationChangedEvent) => {
|
|
|
|
|
if (!isPaginationEnabled || !event.api) return;
|
|
|
|
|
|
2023-02-08 10:05:10 -08:00
|
|
|
try {
|
|
|
|
|
// Scroll to top of page on pagination change
|
2023-03-05 21:02:05 -08:00
|
|
|
const currentPageStartIndex = table.pagination.currentPage * table.pagination.itemsPerPage;
|
2023-02-08 10:05:10 -08:00
|
|
|
event.api?.ensureIndexVisible(currentPageStartIndex, 'top');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
2022-12-30 21:04:06 -08:00
|
|
|
|
2023-03-05 21:02:05 -08:00
|
|
|
setTablePagination({
|
|
|
|
|
data: {
|
|
|
|
|
itemsPerPage: event.api.paginationGetPageSize(),
|
|
|
|
|
totalItems: event.api.paginationGetRowCount(),
|
|
|
|
|
totalPages: event.api.paginationGetTotalPages() + 1,
|
|
|
|
|
},
|
|
|
|
|
key: pageKey,
|
2022-12-30 21:04:06 -08:00
|
|
|
});
|
|
|
|
|
},
|
2023-03-05 21:02:05 -08:00
|
|
|
[
|
|
|
|
|
isPaginationEnabled,
|
|
|
|
|
pageKey,
|
|
|
|
|
setTablePagination,
|
|
|
|
|
table.pagination.currentPage,
|
|
|
|
|
table.pagination.itemsPerPage,
|
|
|
|
|
],
|
2022-12-30 21:04:06 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleTableColumnChange = useCallback(() => {
|
|
|
|
|
const { columnApi } = tableRef?.current || {};
|
|
|
|
|
const columnsOrder = columnApi?.getAllGridColumns();
|
|
|
|
|
|
|
|
|
|
if (!columnsOrder) return;
|
|
|
|
|
|
2023-03-05 21:02:05 -08:00
|
|
|
const columnsInSettings = table.columns;
|
2022-12-30 21:04:06 -08:00
|
|
|
const updatedColumns = [];
|
|
|
|
|
for (const column of columnsOrder) {
|
|
|
|
|
const columnInSettings = columnsInSettings.find((c) => c.column === column.getColDef().colId);
|
|
|
|
|
|
|
|
|
|
if (columnInSettings) {
|
|
|
|
|
updatedColumns.push({
|
|
|
|
|
...columnInSettings,
|
2023-03-05 21:02:05 -08:00
|
|
|
...(!table.autoFit && {
|
2022-12-30 21:04:06 -08:00
|
|
|
width: column.getColDef().width,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-05 21:02:05 -08:00
|
|
|
setTable({ data: { columns: updatedColumns }, key: pageKey });
|
|
|
|
|
}, [tableRef, table.columns, table.autoFit, setTable, pageKey]);
|
2022-12-30 21:04:06 -08:00
|
|
|
|
|
|
|
|
const debouncedTableColumnChange = debounce(handleTableColumnChange, 200);
|
|
|
|
|
|
|
|
|
|
const handleTableScroll = (e: BodyScrollEvent) => {
|
2023-03-05 21:02:05 -08:00
|
|
|
const scrollOffset = Number((e.top / table.rowHeight).toFixed(0));
|
|
|
|
|
setTable({ data: { scrollOffset }, key: pageKey });
|
2022-12-30 21:04:06 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fetch = useCallback(
|
|
|
|
|
async ({ skip: startIndex, take: limit }: { skip: number; take: number }) => {
|
|
|
|
|
const queryKey = queryKeys.albumArtists.list(server?.id || '', {
|
|
|
|
|
limit,
|
|
|
|
|
startIndex,
|
2023-03-05 21:02:05 -08:00
|
|
|
...filter,
|
2022-12-30 21:04:06 -08:00
|
|
|
});
|
|
|
|
|
|
2023-01-06 03:33:11 -08:00
|
|
|
const albumArtistsRes = await queryClient.fetchQuery(
|
|
|
|
|
queryKey,
|
|
|
|
|
async ({ signal }) =>
|
|
|
|
|
api.controller.getAlbumArtistList({
|
|
|
|
|
query: {
|
|
|
|
|
limit,
|
|
|
|
|
startIndex,
|
2023-03-05 21:02:05 -08:00
|
|
|
...filter,
|
2023-01-06 03:33:11 -08:00
|
|
|
},
|
|
|
|
|
server,
|
|
|
|
|
signal,
|
|
|
|
|
}),
|
|
|
|
|
{ cacheTime: 1000 * 60 * 1 },
|
2022-12-30 21:04:06 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return api.normalize.albumArtistList(albumArtistsRes, server);
|
|
|
|
|
},
|
2023-03-05 21:02:05 -08:00
|
|
|
[filter, queryClient, server],
|
2022-12-30 21:04:06 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleGridScroll = useCallback(
|
|
|
|
|
(e: ListOnScrollProps) => {
|
2023-03-05 21:02:05 -08:00
|
|
|
setGrid({ data: { scrollOffset: e.scrollOffset }, key: pageKey });
|
2022-12-30 21:04:06 -08:00
|
|
|
},
|
2023-03-05 21:02:05 -08:00
|
|
|
[pageKey, setGrid],
|
2022-12-30 21:04:06 -08:00
|
|
|
);
|
|
|
|
|
|
2023-01-15 21:58:50 -08:00
|
|
|
const handleGridSizeChange = () => {
|
2023-03-05 21:02:05 -08:00
|
|
|
if (table.autoFit) {
|
2023-01-15 21:58:50 -08:00
|
|
|
tableRef?.current?.api.sizeColumnsToFit();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-30 21:04:06 -08:00
|
|
|
const cardRows = useMemo(() => {
|
|
|
|
|
const rows: CardRow<AlbumArtist>[] = [ALBUMARTIST_CARD_ROWS.name];
|
|
|
|
|
|
2023-03-05 21:02:05 -08:00
|
|
|
switch (filter.sortBy) {
|
2022-12-30 21:04:06 -08:00
|
|
|
case AlbumArtistListSort.DURATION:
|
|
|
|
|
rows.push(ALBUMARTIST_CARD_ROWS.duration);
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.FAVORITED:
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.NAME:
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.ALBUM_COUNT:
|
|
|
|
|
rows.push(ALBUMARTIST_CARD_ROWS.albumCount);
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.PLAY_COUNT:
|
|
|
|
|
rows.push(ALBUMARTIST_CARD_ROWS.playCount);
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.RANDOM:
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.RATING:
|
|
|
|
|
rows.push(ALBUMARTIST_CARD_ROWS.rating);
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.RECENTLY_ADDED:
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.SONG_COUNT:
|
|
|
|
|
rows.push(ALBUMARTIST_CARD_ROWS.songCount);
|
|
|
|
|
break;
|
|
|
|
|
case AlbumArtistListSort.RELEASE_DATE:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rows;
|
2023-03-05 21:02:05 -08:00
|
|
|
}, [filter.sortBy]);
|
2022-12-30 21:04:06 -08:00
|
|
|
|
2023-01-07 18:16:19 -08:00
|
|
|
const handleContextMenu = useHandleTableContextMenu(
|
|
|
|
|
LibraryItem.ALBUM_ARTIST,
|
|
|
|
|
ALBUM_CONTEXT_MENU_ITEMS,
|
|
|
|
|
);
|
2022-12-30 21:04:06 -08:00
|
|
|
|
|
|
|
|
const handleRowDoubleClick = (e: RowDoubleClickedEvent) => {
|
2023-01-12 12:45:44 -08:00
|
|
|
navigate(generatePath(AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL, { albumArtistId: e.data.id }));
|
2022-12-30 21:04:06 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<VirtualGridAutoSizerContainer>
|
2023-03-05 21:02:05 -08:00
|
|
|
{display === ListDisplayType.CARD || display === ListDisplayType.POSTER ? (
|
2022-12-30 21:04:06 -08:00
|
|
|
<AutoSizer>
|
|
|
|
|
{({ height, width }) => (
|
|
|
|
|
<VirtualInfiniteGrid
|
2023-03-05 21:02:05 -08:00
|
|
|
key={`albumartist-list-${server?.id}-${display}`}
|
2022-12-30 21:04:06 -08:00
|
|
|
ref={gridRef}
|
|
|
|
|
cardRows={cardRows}
|
2023-03-05 21:02:05 -08:00
|
|
|
display={display || ListDisplayType.CARD}
|
2022-12-30 21:04:06 -08:00
|
|
|
fetchFn={fetch}
|
|
|
|
|
handlePlayQueueAdd={handlePlayQueueAdd}
|
|
|
|
|
height={height}
|
2023-03-05 21:02:05 -08:00
|
|
|
initialScrollOffset={grid?.scrollOffset || 0}
|
2022-12-30 21:04:06 -08:00
|
|
|
itemCount={checkAlbumArtistList?.data?.totalRecordCount || 0}
|
2023-01-03 17:41:03 -08:00
|
|
|
itemData={itemData}
|
2022-12-30 21:04:06 -08:00
|
|
|
itemGap={20}
|
2023-03-28 15:37:50 -07:00
|
|
|
itemSize={grid?.itemsPerRow || 5}
|
2022-12-30 21:04:06 -08:00
|
|
|
itemType={LibraryItem.ALBUM_ARTIST}
|
2023-01-05 11:03:24 -08:00
|
|
|
loading={checkAlbumArtistList.isLoading}
|
2022-12-30 21:04:06 -08:00
|
|
|
minimumBatchSize={40}
|
|
|
|
|
route={{
|
2023-01-12 12:45:44 -08:00
|
|
|
route: AppRoute.LIBRARY_ALBUM_ARTISTS_DETAIL,
|
2022-12-30 21:04:06 -08:00
|
|
|
slugs: [{ idProperty: 'id', slugProperty: 'albumArtistId' }],
|
|
|
|
|
}}
|
2023-01-03 17:41:03 -08:00
|
|
|
setItemData={setItemData}
|
2022-12-30 21:04:06 -08:00
|
|
|
width={width}
|
|
|
|
|
onScroll={handleGridScroll}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</AutoSizer>
|
|
|
|
|
) : (
|
|
|
|
|
<VirtualTable
|
|
|
|
|
// https://github.com/ag-grid/ag-grid/issues/5284
|
|
|
|
|
// Key is used to force remount of table when display, rowHeight, or server changes
|
2023-03-05 21:02:05 -08:00
|
|
|
key={`table-${display}-${table.rowHeight}-${server?.id}`}
|
2022-12-30 21:04:06 -08:00
|
|
|
ref={tableRef}
|
|
|
|
|
alwaysShowHorizontalScroll
|
2023-01-15 21:58:50 -08:00
|
|
|
suppressRowDrag
|
2023-03-05 21:02:05 -08:00
|
|
|
autoFitColumns={table.autoFit}
|
2022-12-30 21:04:06 -08:00
|
|
|
columnDefs={columnDefs}
|
|
|
|
|
getRowId={(data) => data.data.id}
|
2023-01-06 18:24:31 -08:00
|
|
|
infiniteInitialRowCount={checkAlbumArtistList.data?.totalRecordCount || 1}
|
2022-12-30 21:04:06 -08:00
|
|
|
pagination={isPaginationEnabled}
|
|
|
|
|
paginationAutoPageSize={isPaginationEnabled}
|
2023-03-05 21:02:05 -08:00
|
|
|
paginationPageSize={table.pagination.itemsPerPage || 100}
|
|
|
|
|
rowHeight={table.rowHeight || 40}
|
2022-12-30 21:04:06 -08:00
|
|
|
rowModelType="infinite"
|
|
|
|
|
onBodyScrollEnd={handleTableScroll}
|
|
|
|
|
onCellContextMenu={handleContextMenu}
|
|
|
|
|
onColumnMoved={handleTableColumnChange}
|
|
|
|
|
onColumnResized={debouncedTableColumnChange}
|
|
|
|
|
onGridReady={onTableReady}
|
2023-01-15 21:58:50 -08:00
|
|
|
onGridSizeChanged={handleGridSizeChange}
|
2022-12-30 21:04:06 -08:00
|
|
|
onPaginationChanged={onTablePaginationChanged}
|
|
|
|
|
onRowDoubleClicked={handleRowDoubleClick}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</VirtualGridAutoSizerContainer>
|
|
|
|
|
{isPaginationEnabled && (
|
|
|
|
|
<AnimatePresence
|
|
|
|
|
presenceAffectsLayout
|
|
|
|
|
initial={false}
|
|
|
|
|
mode="wait"
|
|
|
|
|
>
|
2023-03-05 21:02:05 -08:00
|
|
|
{display === ListDisplayType.TABLE_PAGINATED && (
|
2022-12-30 21:04:06 -08:00
|
|
|
<TablePagination
|
2023-03-05 21:02:05 -08:00
|
|
|
pageKey={pageKey}
|
|
|
|
|
pagination={table.pagination}
|
|
|
|
|
setPagination={setTablePagination}
|
2022-12-30 21:04:06 -08:00
|
|
|
tableRef={tableRef}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|