2022-12-31 18:03:26 -08:00
|
|
|
import type {
|
2023-07-01 19:10:05 -07:00
|
|
|
BodyScrollEvent,
|
|
|
|
|
ColDef,
|
|
|
|
|
GridReadyEvent,
|
|
|
|
|
PaginationChangedEvent,
|
|
|
|
|
RowDoubleClickedEvent,
|
2024-08-25 15:21:56 -07:00
|
|
|
RowDragEvent,
|
2022-12-31 18:03:26 -08:00
|
|
|
} from '@ag-grid-community/core';
|
|
|
|
|
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
|
2025-05-18 14:03:18 -07:00
|
|
|
|
2025-11-02 01:16:53 -07:00
|
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
2022-12-31 18:03:26 -08:00
|
|
|
import debounce from 'lodash/debounce';
|
2025-06-24 00:04:36 -07:00
|
|
|
import { AnimatePresence } from 'motion/react';
|
2023-07-21 00:17:57 -07:00
|
|
|
import { MutableRefObject, useCallback, useMemo } from 'react';
|
|
|
|
|
import { useParams } from 'react-router';
|
2025-05-18 14:03:18 -07:00
|
|
|
|
2023-07-21 00:17:57 -07:00
|
|
|
import { api } from '/@/renderer/api';
|
|
|
|
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
2023-04-30 22:01:52 -07:00
|
|
|
import { VirtualGridAutoSizerContainer } from '/@/renderer/components/virtual-grid';
|
2025-05-18 14:03:18 -07:00
|
|
|
import { getColumnDefs, TablePagination, VirtualTable } from '/@/renderer/components/virtual-table';
|
2023-07-18 18:39:39 -07:00
|
|
|
import { useCurrentSongRowStyles } from '/@/renderer/components/virtual-table/hooks/use-current-song-row-styles';
|
2023-07-21 00:17:57 -07:00
|
|
|
import { useHandleTableContextMenu } from '/@/renderer/features/context-menu';
|
|
|
|
|
import {
|
|
|
|
|
PLAYLIST_SONG_CONTEXT_MENU_ITEMS,
|
|
|
|
|
SMART_PLAYLIST_SONG_CONTEXT_MENU_ITEMS,
|
|
|
|
|
} from '/@/renderer/features/context-menu/context-menu-items';
|
|
|
|
|
import { usePlayQueueAdd } from '/@/renderer/features/player';
|
2025-11-02 01:16:53 -07:00
|
|
|
import { playlistsQueries } from '/@/renderer/features/playlists/api/playlists-api';
|
2025-05-18 14:03:18 -07:00
|
|
|
import { useAppFocus } from '/@/renderer/hooks';
|
2023-07-21 00:17:57 -07:00
|
|
|
import {
|
|
|
|
|
useCurrentServer,
|
2023-10-18 19:51:55 -07:00
|
|
|
useCurrentSong,
|
2023-10-19 01:32:11 +00:00
|
|
|
useCurrentStatus,
|
2023-07-21 00:17:57 -07:00
|
|
|
usePlaylistDetailStore,
|
|
|
|
|
usePlaylistDetailTablePagination,
|
|
|
|
|
useSetPlaylistDetailTable,
|
|
|
|
|
useSetPlaylistDetailTablePagination,
|
|
|
|
|
} from '/@/renderer/store';
|
2025-05-20 19:23:36 -07:00
|
|
|
import { PersistedTableColumn, usePlayButtonBehavior } from '/@/renderer/store/settings.store';
|
2025-06-24 00:04:36 -07:00
|
|
|
import { toast } from '/@/shared/components/toast/toast';
|
2025-05-20 19:23:36 -07:00
|
|
|
import {
|
|
|
|
|
LibraryItem,
|
2025-09-18 04:06:30 +00:00
|
|
|
PlaylistSongListQueryClientSide,
|
2025-05-20 19:23:36 -07:00
|
|
|
QueueSong,
|
2025-09-18 04:06:30 +00:00
|
|
|
ServerType,
|
2025-05-20 19:23:36 -07:00
|
|
|
Song,
|
2025-09-18 04:06:30 +00:00
|
|
|
SongListResponse,
|
2025-05-20 19:23:36 -07:00
|
|
|
SongListSort,
|
|
|
|
|
SortOrder,
|
|
|
|
|
} from '/@/shared/types/domain-types';
|
2025-09-18 04:06:30 +00:00
|
|
|
import { ListDisplayType } from '/@/shared/types/types';
|
2022-12-31 18:03:26 -08:00
|
|
|
|
|
|
|
|
interface PlaylistDetailContentProps {
|
2024-09-26 04:23:08 +00:00
|
|
|
songs?: Song[];
|
2023-07-01 19:10:05 -07:00
|
|
|
tableRef: MutableRefObject<AgGridReactType | null>;
|
2022-12-31 18:03:26 -08:00
|
|
|
}
|
|
|
|
|
|
2024-09-26 04:23:08 +00:00
|
|
|
export const PlaylistDetailSongListContent = ({ songs, tableRef }: PlaylistDetailContentProps) => {
|
2023-07-01 19:10:05 -07:00
|
|
|
const { playlistId } = useParams() as { playlistId: string };
|
|
|
|
|
const queryClient = useQueryClient();
|
2023-10-19 01:32:11 +00:00
|
|
|
const status = useCurrentStatus();
|
|
|
|
|
const isFocused = useAppFocus();
|
2023-10-18 19:51:55 -07:00
|
|
|
const currentSong = useCurrentSong();
|
2023-07-01 19:10:05 -07:00
|
|
|
const server = useCurrentServer();
|
|
|
|
|
const page = usePlaylistDetailStore();
|
2025-09-18 04:06:30 +00:00
|
|
|
const filters: PlaylistSongListQueryClientSide = useMemo(() => {
|
2023-07-01 19:10:05 -07:00
|
|
|
return {
|
|
|
|
|
sortBy: page?.table.id[playlistId]?.filter?.sortBy || SongListSort.ID,
|
|
|
|
|
sortOrder: page?.table.id[playlistId]?.filter?.sortOrder || SortOrder.ASC,
|
|
|
|
|
};
|
|
|
|
|
}, [page?.table.id, playlistId]);
|
2023-01-01 13:58:05 -08:00
|
|
|
|
2025-11-02 01:16:53 -07:00
|
|
|
const detailQuery = useQuery(
|
|
|
|
|
playlistsQueries.detail({ query: { id: playlistId }, serverId: server?.id }),
|
|
|
|
|
);
|
2023-02-07 22:47:23 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const p = usePlaylistDetailTablePagination(playlistId);
|
|
|
|
|
const pagination = {
|
|
|
|
|
currentPage: p?.currentPage || 0,
|
|
|
|
|
itemsPerPage: p?.itemsPerPage || 100,
|
|
|
|
|
scrollOffset: p?.scrollOffset || 0,
|
|
|
|
|
totalItems: p?.totalItems || 1,
|
|
|
|
|
totalPages: p?.totalPages || 1,
|
|
|
|
|
};
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const setPagination = useSetPlaylistDetailTablePagination();
|
|
|
|
|
const setTable = useSetPlaylistDetailTable();
|
|
|
|
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
|
|
|
|
const playButtonBehavior = usePlayButtonBehavior();
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const isPaginationEnabled = page.display === ListDisplayType.TABLE_PAGINATED;
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const columnDefs: ColDef[] = useMemo(
|
2023-10-18 19:51:55 -07:00
|
|
|
() => getColumnDefs(page.table.columns, false, 'generic'),
|
2023-07-01 19:10:05 -07:00
|
|
|
[page.table.columns],
|
|
|
|
|
);
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const onGridReady = useCallback(
|
|
|
|
|
(params: GridReadyEvent) => {
|
|
|
|
|
params.api?.ensureIndexVisible(pagination.scrollOffset, 'top');
|
2023-01-01 13:58:05 -08:00
|
|
|
},
|
2025-09-18 04:06:30 +00:00
|
|
|
[pagination.scrollOffset],
|
2023-07-01 19:10:05 -07:00
|
|
|
);
|
2023-01-01 13:58:05 -08:00
|
|
|
|
2024-08-25 15:21:56 -07:00
|
|
|
const handleDragEnd = useCallback(
|
|
|
|
|
async (e: RowDragEvent<Song>) => {
|
|
|
|
|
if (!e.nodes.length) return;
|
|
|
|
|
|
|
|
|
|
const trackId = e.node.data?.playlistItemId;
|
|
|
|
|
if (trackId && e.node.rowIndex !== null && e.overIndex !== e.node.rowIndex) {
|
|
|
|
|
try {
|
|
|
|
|
await api.controller.movePlaylistItem({
|
|
|
|
|
apiClientProps: {
|
|
|
|
|
server,
|
|
|
|
|
},
|
|
|
|
|
query: {
|
|
|
|
|
endingIndex: e.overIndex,
|
|
|
|
|
playlistId,
|
|
|
|
|
startingIndex: e.node.rowIndex + 1,
|
|
|
|
|
trackId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-18 04:06:30 +00:00
|
|
|
queryClient.setQueryData<SongListResponse>(
|
|
|
|
|
queryKeys.playlists.songList(server?.id || '', playlistId),
|
|
|
|
|
(previous) => {
|
|
|
|
|
if (previous?.items) {
|
|
|
|
|
const from = e.node.rowIndex!;
|
|
|
|
|
const to = e.overIndex;
|
|
|
|
|
|
|
|
|
|
const item = previous.items[from];
|
|
|
|
|
const remaining = previous.items.toSpliced(from, 1);
|
|
|
|
|
remaining.splice(to, 0, item);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
error: previous.error,
|
|
|
|
|
items: remaining,
|
|
|
|
|
startIndex: previous.startIndex,
|
|
|
|
|
totalRecordCount: previous.totalRecordCount,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return previous;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Nodes have to be redrawn, otherwise the row indexes will be wrong
|
|
|
|
|
// Maybe it's possible to only redraw necessary rows to not be as expensive?
|
|
|
|
|
tableRef.current?.api.redrawRows();
|
2024-08-25 15:21:56 -07:00
|
|
|
} catch (error) {
|
|
|
|
|
toast.error({
|
|
|
|
|
message: (error as Error).message,
|
|
|
|
|
title: `Failed to move song ${e.node.data?.name} to ${e.overIndex}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-09-18 04:06:30 +00:00
|
|
|
[playlistId, queryClient, server, tableRef],
|
2024-08-25 15:21:56 -07:00
|
|
|
);
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handleGridSizeChange = () => {
|
|
|
|
|
if (page.table.autoFit) {
|
2024-01-24 21:15:11 -08:00
|
|
|
tableRef?.current?.api?.sizeColumnsToFit();
|
2023-07-01 19:10:05 -07:00
|
|
|
}
|
|
|
|
|
};
|
2023-01-01 13:58:05 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const onPaginationChanged = useCallback(
|
|
|
|
|
(event: PaginationChangedEvent) => {
|
|
|
|
|
if (!isPaginationEnabled || !event.api) return;
|
2023-01-01 13:58:05 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
try {
|
|
|
|
|
// Scroll to top of page on pagination change
|
|
|
|
|
const currentPageStartIndex = pagination.currentPage * pagination.itemsPerPage;
|
|
|
|
|
event.api?.ensureIndexVisible(currentPageStartIndex, 'top');
|
|
|
|
|
} catch (err) {
|
2025-06-26 21:17:59 -07:00
|
|
|
console.error(err);
|
2023-07-01 19:10:05 -07:00
|
|
|
}
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
setPagination(playlistId, {
|
|
|
|
|
itemsPerPage: event.api.paginationGetPageSize(),
|
|
|
|
|
totalItems: event.api.paginationGetRowCount(),
|
|
|
|
|
totalPages: event.api.paginationGetTotalPages() + 1,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
[
|
|
|
|
|
isPaginationEnabled,
|
|
|
|
|
pagination.currentPage,
|
|
|
|
|
pagination.itemsPerPage,
|
|
|
|
|
playlistId,
|
|
|
|
|
setPagination,
|
|
|
|
|
],
|
|
|
|
|
);
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handleColumnChange = useCallback(() => {
|
|
|
|
|
const { columnApi } = tableRef?.current || {};
|
|
|
|
|
const columnsOrder = columnApi?.getAllGridColumns();
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
if (!columnsOrder) return;
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const columnsInSettings = page.table.columns;
|
2025-05-20 19:23:36 -07:00
|
|
|
const updatedColumns: PersistedTableColumn[] = [];
|
2023-07-01 19:10:05 -07:00
|
|
|
for (const column of columnsOrder) {
|
|
|
|
|
const columnInSettings = columnsInSettings.find(
|
|
|
|
|
(c) => c.column === column.getColDef().colId,
|
|
|
|
|
);
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
if (columnInSettings) {
|
|
|
|
|
updatedColumns.push({
|
|
|
|
|
...columnInSettings,
|
|
|
|
|
...(!page.table.autoFit && {
|
|
|
|
|
width: column.getActualWidth(),
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
setTable({ columns: updatedColumns });
|
|
|
|
|
}, [page.table.autoFit, page.table.columns, setTable, tableRef]);
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const debouncedColumnChange = debounce(handleColumnChange, 200);
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handleScroll = (e: BodyScrollEvent) => {
|
|
|
|
|
const scrollOffset = Number((e.top / page.table.rowHeight).toFixed(0));
|
|
|
|
|
setPagination(playlistId, { scrollOffset });
|
|
|
|
|
};
|
2023-02-07 22:47:23 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const contextMenuItems = useMemo(() => {
|
|
|
|
|
if (detailQuery?.data?.rules) {
|
|
|
|
|
return SMART_PLAYLIST_SONG_CONTEXT_MENU_ITEMS;
|
|
|
|
|
}
|
2023-02-07 22:47:23 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return PLAYLIST_SONG_CONTEXT_MENU_ITEMS;
|
|
|
|
|
}, [detailQuery?.data?.rules]);
|
2022-12-31 18:03:26 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handleContextMenu = useHandleTableContextMenu(LibraryItem.SONG, contextMenuItems, {
|
|
|
|
|
playlistId,
|
|
|
|
|
tableRef,
|
2022-12-31 18:03:26 -08:00
|
|
|
});
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handleRowDoubleClick = (e: RowDoubleClickedEvent<QueueSong>) => {
|
|
|
|
|
if (!e.data) return;
|
|
|
|
|
handlePlayQueueAdd?.({
|
|
|
|
|
byItemType: {
|
|
|
|
|
id: [playlistId],
|
|
|
|
|
type: LibraryItem.PLAYLIST,
|
|
|
|
|
},
|
|
|
|
|
initialSongId: e.data.id,
|
|
|
|
|
playType: playButtonBehavior,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-18 18:39:39 -07:00
|
|
|
const { rowClassRules } = useCurrentSongRowStyles({ tableRef });
|
|
|
|
|
|
2024-09-26 04:23:08 +00:00
|
|
|
const canDrag =
|
2025-09-18 04:06:30 +00:00
|
|
|
filters.sortBy === SongListSort.ID &&
|
|
|
|
|
!detailQuery?.data?.rules &&
|
|
|
|
|
server?.type !== ServerType.SUBSONIC;
|
2024-09-26 04:23:08 +00:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<VirtualGridAutoSizerContainer>
|
|
|
|
|
<VirtualTable
|
|
|
|
|
alwaysShowHorizontalScroll
|
|
|
|
|
autoFitColumns={page.table.autoFit}
|
|
|
|
|
columnDefs={columnDefs}
|
2023-10-19 01:32:11 +00:00
|
|
|
context={{
|
2023-10-18 19:51:55 -07:00
|
|
|
currentSong,
|
2023-10-19 01:32:11 +00:00
|
|
|
isFocused,
|
2024-10-10 03:40:30 +03:00
|
|
|
itemType: LibraryItem.SONG,
|
2023-10-19 01:32:11 +00:00
|
|
|
onCellContextMenu: handleContextMenu,
|
|
|
|
|
status,
|
|
|
|
|
}}
|
2023-07-01 19:10:05 -07:00
|
|
|
getRowId={(data) => data.data.uniqueId}
|
2025-05-18 14:03:18 -07:00
|
|
|
// https://github.com/ag-grid/ag-grid/issues/5284
|
|
|
|
|
// Key is used to force remount of table when display, rowHeight, or server changes
|
|
|
|
|
key={`table-${page.display}-${page.table.rowHeight}-${server?.id}`}
|
2023-07-01 19:10:05 -07:00
|
|
|
onBodyScrollEnd={handleScroll}
|
|
|
|
|
onCellContextMenu={handleContextMenu}
|
|
|
|
|
onColumnMoved={handleColumnChange}
|
|
|
|
|
onColumnResized={debouncedColumnChange}
|
|
|
|
|
onGridReady={onGridReady}
|
|
|
|
|
onGridSizeChanged={handleGridSizeChange}
|
|
|
|
|
onPaginationChanged={onPaginationChanged}
|
|
|
|
|
onRowDoubleClicked={handleRowDoubleClick}
|
2024-08-25 15:21:56 -07:00
|
|
|
onRowDragEnd={handleDragEnd}
|
2025-05-18 14:03:18 -07:00
|
|
|
pagination={isPaginationEnabled}
|
|
|
|
|
paginationAutoPageSize={isPaginationEnabled}
|
|
|
|
|
paginationPageSize={pagination.itemsPerPage || 100}
|
|
|
|
|
ref={tableRef}
|
|
|
|
|
rowClassRules={rowClassRules}
|
|
|
|
|
rowData={songs}
|
|
|
|
|
rowDragEntireRow={canDrag}
|
|
|
|
|
rowHeight={page.table.rowHeight || 40}
|
2025-09-18 04:06:30 +00:00
|
|
|
rowModelType="clientSide"
|
2025-05-18 14:03:18 -07:00
|
|
|
shouldUpdateSong
|
2023-07-01 19:10:05 -07:00
|
|
|
/>
|
|
|
|
|
</VirtualGridAutoSizerContainer>
|
|
|
|
|
{isPaginationEnabled && (
|
2025-07-12 11:17:54 -07:00
|
|
|
<AnimatePresence initial={false} mode="wait" presenceAffectsLayout>
|
2023-07-01 19:10:05 -07:00
|
|
|
{page.display === ListDisplayType.TABLE_PAGINATED && (
|
|
|
|
|
<TablePagination
|
|
|
|
|
pageKey={playlistId}
|
|
|
|
|
pagination={pagination}
|
|
|
|
|
setIdPagination={setPagination}
|
|
|
|
|
tableRef={tableRef}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2022-12-31 18:03:26 -08:00
|
|
|
};
|