Skip to content

Commit

Permalink
use iterator instead
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarner7 committed Sep 3, 2024
1 parent 98a7123 commit c7ba49b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MutableRefObject, useCallback, useMemo, useRef } from 'react';
import { ColDef, GetRowIdParams, RowDoubleClickedEvent } from '@ag-grid-community/core';
import { MutableRefObject, useMemo, useRef } from 'react';
import { ColDef, RowDoubleClickedEvent } from '@ag-grid-community/core';
import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact';
import { Box, Group } from '@mantine/core';
import { closeAllModals, openModal } from '@mantine/modals';
Expand All @@ -9,7 +9,7 @@ import { generatePath, useNavigate, useParams } from 'react-router';
import { Link } from 'react-router-dom';
import styled from 'styled-components';
import { useListStoreByKey } from '../../../store/list.store';
import { LibraryItem, QueueSong, ServerType } from '/@/renderer/api/types';
import { LibraryItem, QueueSong } from '/@/renderer/api/types';
import { Button, ConfirmModal, DropdownMenu, MotionGroup, toast } from '/@/renderer/components';
import { getColumnDefs, VirtualTable } from '/@/renderer/components/virtual-table';
import { useCurrentSongRowStyles } from '/@/renderer/components/virtual-table/hooks/use-current-song-row-styles';
Expand All @@ -28,7 +28,6 @@ import { AppRoute } from '/@/renderer/router/routes';
import { useCurrentServer } from '/@/renderer/store';
import { usePlayButtonBehavior } from '/@/renderer/store/settings.store';
import { Play } from '/@/renderer/types';
import { useScanUpdate } from '/@/renderer/features/playlists/hooks/use-scan-update';

const ContentContainer = styled.div`
position: relative;
Expand Down Expand Up @@ -157,18 +156,6 @@ export const PlaylistDetailContent = ({ tableRef }: PlaylistDetailContentProps)

const loadMoreRef = useRef<HTMLButtonElement | null>(null);

// Duplicates are only present if on Navidrome
const getId = useCallback(
(data: GetRowIdParams<any>): string => {
return server?.type === ServerType.JELLYFIN
? data.data.id
: `${data.data.id}-${data.data.pageIndex}`;
},
[server?.type],
);

useScanUpdate(server, tableRef);

return (
<ContentContainer>
<Group
Expand Down Expand Up @@ -228,18 +215,18 @@ export const PlaylistDetailContent = ({ tableRef }: PlaylistDetailContentProps)
autoFitColumns
autoHeight
deselectOnClickOutside
shouldUpdateSong
stickyHeader
suppressCellFocus
suppressHorizontalScroll
suppressLoadingOverlay
suppressRowDrag
columnDefs={columnDefs}
getRowId={getId}
getRowId={(data) => `${data.data.id}-${data.data.pageIndex}`}
rowClassRules={rowClassRules}
rowData={playlistSongData}
rowHeight={60}
rowSelection="multiple"
shouldUpdateSong={server?.type === ServerType.JELLYFIN}
onCellContextMenu={handleContextMenu}
onRowDoubleClicked={handleRowDoubleClick}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
BodyScrollEvent,
ColDef,
GetRowIdParams,
GridReadyEvent,
IDatasource,
PaginationChangedEvent,
Expand All @@ -20,7 +19,6 @@ import {
LibraryItem,
PlaylistSongListQuery,
QueueSong,
ServerType,
Song,
SongListSort,
SortOrder,
Expand Down Expand Up @@ -49,7 +47,6 @@ import { usePlayButtonBehavior } from '/@/renderer/store/settings.store';
import { ListDisplayType } from '/@/renderer/types';
import { useAppFocus } from '/@/renderer/hooks';
import { toast } from '/@/renderer/components';
import { useScanUpdate } from '/@/renderer/features/playlists/hooks/use-scan-update';

interface PlaylistDetailContentProps {
tableRef: MutableRefObject<AgGridReactType | null>;
Expand Down Expand Up @@ -273,16 +270,6 @@ export const PlaylistDetailSongListContent = ({ tableRef }: PlaylistDetailConten

const { rowClassRules } = useCurrentSongRowStyles({ tableRef });

// Duplicates are only present if on Navidrome
const getId = useCallback(
(data: GetRowIdParams<Song>): string => {
return server?.type === ServerType.JELLYFIN ? data.data.id : data.data.uniqueId;
},
[server?.type],
);

useScanUpdate(server, tableRef);

return (
<>
<VirtualGridAutoSizerContainer>
Expand All @@ -292,6 +279,7 @@ export const PlaylistDetailSongListContent = ({ tableRef }: PlaylistDetailConten
key={`table-${page.display}-${page.table.rowHeight}-${server?.id}`}
ref={tableRef}
alwaysShowHorizontalScroll
shouldUpdateSong
autoFitColumns={page.table.autoFit}
columnDefs={columnDefs}
context={{
Expand All @@ -300,7 +288,7 @@ export const PlaylistDetailSongListContent = ({ tableRef }: PlaylistDetailConten
onCellContextMenu: handleContextMenu,
status,
}}
getRowId={getId}
getRowId={(data) => data.data.uniqueId}
infiniteInitialRowCount={checkPlaylistList.data?.totalRecordCount || 100}
pagination={isPaginationEnabled}
paginationAutoPageSize={isPaginationEnabled}
Expand All @@ -311,7 +299,6 @@ export const PlaylistDetailSongListContent = ({ tableRef }: PlaylistDetailConten
}
rowHeight={page.table.rowHeight || 40}
rowModelType="infinite"
shouldUpdateSong={server?.type === ServerType.JELLYFIN}
onBodyScrollEnd={handleScroll}
onCellContextMenu={handleContextMenu}
onColumnMoved={handleColumnChange}
Expand Down
60 changes: 0 additions & 60 deletions src/renderer/features/playlists/hooks/use-scan-update.ts

This file was deleted.

53 changes: 27 additions & 26 deletions src/renderer/hooks/use-song-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,37 @@ export const useTableChange = (
if (!api) return;

const rowNodes: RowNode[] = [];
for (const id of ids) {
const node: RowNode<Song> | undefined = api.getRowNode(id);
if (node && node.data) {
switch (event.event) {
case 'favorite': {
if (node.data.userFavorite !== event.favorite) {
node.setDataValue('userFavorite', event.favorite);
}
break;
const idSet = new Set(ids);

api.forEachNode((node: RowNode<Song>) => {
if (!node.data || !idSet.has(node.data.id)) return;

switch (event.event) {
case 'favorite': {
if (node.data.userFavorite !== event.favorite) {
node.setDataValue('userFavorite', event.favorite);
}
case 'play':
if (node.data.lastPlayedAt !== event.timestamp) {
node.setData({
...node.data,
lastPlayedAt: event.timestamp,
playCount: node.data.playCount + 1,
});
}
node.data.lastPlayedAt = event.timestamp;
break;
case 'rating': {
if (node.data.userRating !== event.rating) {
node.setDataValue('userRating', event.rating);
rowNodes.push(node);
}
break;
break;
}
case 'play':
if (node.data.lastPlayedAt !== event.timestamp) {
node.setData({
...node.data,
lastPlayedAt: event.timestamp,
playCount: node.data.playCount + 1,
});
}
node.data.lastPlayedAt = event.timestamp;
break;
case 'rating': {
if (node.data.userRating !== event.rating) {
node.setDataValue('userRating', event.rating);
rowNodes.push(node);
}
break;
}
}
}
});

// This is required to redraw star rows
if (rowNodes.length > 0) {
Expand Down

0 comments on commit c7ba49b

Please sign in to comment.