Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[slightly less scuffed bugfix]: Update table rating/favorite when updated anywhere … #707

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/renderer/components/virtual-table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import { NoteCell } from '/@/renderer/components/virtual-table/cells/note-cell';
import { RowIndexCell } from '/@/renderer/components/virtual-table/cells/row-index-cell';
import i18n from '/@/i18n/i18n';
import { formatDateAbsolute, formatDateRelative, formatSizeString } from '/@/renderer/utils/format';
import { useTableFavoriteChange } from '/@/renderer/hooks/use-favorite-change';
import { useTableRatingChange } from '/@/renderer/hooks/use-rating-change';

export * from './table-config-dropdown';
export * from './table-pagination';
Expand Down Expand Up @@ -475,6 +477,7 @@ export interface VirtualTableProps extends AgGridReactProps {
pagination: TablePaginationType;
setPagination: any;
};
shouldUpdateSong?: boolean;
stickyHeader?: boolean;
transparentHeader?: boolean;
}
Expand All @@ -492,6 +495,7 @@ export const VirtualTable = forwardRef(
onGridReady,
onGridSizeChanged,
paginationProps,
shouldUpdateSong,
...rest
}: VirtualTableProps,
ref: Ref<AgGridReactType | null>,
Expand All @@ -506,6 +510,9 @@ export const VirtualTable = forwardRef(
}
});

useTableFavoriteChange(tableRef, shouldUpdateSong || false);
useTableRatingChange(tableRef, shouldUpdateSong || false);

const defaultColumnDefs: ColDef = useMemo(() => {
return {
lockPinned: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ export const AlbumDetailContent = ({ tableRef, background }: AlbumDetailContentP
key={`table-${tableConfig.rowHeight}`}
ref={tableRef}
autoHeight
shouldUpdateSong
stickyHeader
suppressCellFocus
suppressLoadingOverlay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,15 @@ export const AlbumArtistDetailContent = ({ background }: AlbumArtistDetailConten
autoFitColumns
autoHeight
deselectOnClickOutside
shouldUpdateSong
stickyHeader
suppressCellFocus
suppressHorizontalScroll
suppressLoadingOverlay
suppressRowDrag
columnDefs={topSongsColumnDefs}
enableCellChangeFlash={false}
getRowId={(data) => data.data.uniqueId}
getRowId={(data) => data.data.id}
rowData={topSongs}
rowHeight={60}
rowSelection="multiple"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ export const AlbumArtistDetailTopSongsListContent = ({
<VirtualTable
key={`table-${tableProps.rowHeight}-${server?.id}`}
ref={tableRef}
shouldUpdateSong
{...tableProps}
getRowId={(data) => data.data.uniqueId}
getRowId={(data) => data.data.id}
rowClassRules={rowClassRules}
rowData={data}
rowModelType="clientSide"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export const PlaylistDetailContent = ({ tableRef }: PlaylistDetailContentProps)
autoFitColumns
autoHeight
deselectOnClickOutside
shouldUpdateSong
stickyHeader
suppressCellFocus
suppressHorizontalScroll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,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 @@ -248,7 +249,7 @@ export const PlaylistDetailSongListContent = ({ tableRef }: PlaylistDetailConten
onCellContextMenu: handleContextMenu,
status,
}}
getRowId={(data) => data.data.uniqueId}
getRowId={(data) => data.data.id}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate songs can exist in playlists which is why uniqueId is used here.

infiniteInitialRowCount={checkPlaylistList.data?.totalRecordCount || 100}
pagination={isPaginationEnabled}
paginationAutoPageSize={isPaginationEnabled}
Expand Down
1 change: 1 addition & 0 deletions src/renderer/features/search/components/search-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const SearchContent = ({ tableRef }: SearchContentProps) => {
getRowId={(data) => data.data.id}
infiniteInitialRowCount={25}
rowClassRules={rowClassRules}
shouldUpdateSong={itemType === LibraryItem.SONG}
onRowDoubleClicked={handleRowDoubleClick}
/>
</VirtualGridAutoSizerContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const SimilarSongsList = ({ count, fullScreen, song }: SimilarSongsListPr
<VirtualGridAutoSizerContainer>
<VirtualTable
ref={tableRef}
shouldUpdateSong
autoFitColumns={tableConfig.autoFit}
columnDefs={columnDefs}
context={{
Expand All @@ -69,7 +70,7 @@ export const SimilarSongsList = ({ count, fullScreen, song }: SimilarSongsListPr
song,
}}
deselectOnClickOutside={fullScreen}
getRowId={(data) => data.data.uniqueId}
getRowId={(data) => data.data.id}
rowBuffer={50}
rowData={songQuery.data ?? []}
rowHeight={tableConfig.rowHeight || 40}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const SongListTableView = ({ tableRef, itemCount }: SongListTableViewProp
key={`table-${tableProps.rowHeight}-${server?.id}`}
ref={tableRef}
{...tableProps}
shouldUpdateSong
context={{
...tableProps.context,
currentSong,
Expand Down
43 changes: 43 additions & 0 deletions src/renderer/hooks/use-favorite-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { MutableRefObject, useCallback, useEffect } from 'react';
import { usePlayerStore } from '/@/renderer/store';
import type { AgGridReact } from '@ag-grid-community/react';

export const useFavoriteChange = (
handler: (ids: string[], favorite: boolean) => void,
enabled: boolean,
) => {
useEffect(() => {
if (!enabled) return () => {};

const unSubChange = usePlayerStore.subscribe(
(state) => state.favorite,
(value) => value && handler(value.ids, value.favorite),
);

return () => {
unSubChange();
};
}, [handler, enabled]);
};

export const useTableFavoriteChange = (
tableRef: MutableRefObject<AgGridReact | null>,
enabled: boolean,
) => {
const handler = useCallback(
(ids: string[], favorite: boolean) => {
const api = tableRef.current?.api;
if (api) {
for (const id of ids) {
const node = api.getRowNode(id);
if (node && node.data.userFavorite !== favorite) {
node.setDataValue('userFavorite', favorite);
}
}
}
},
[tableRef],
);

useFavoriteChange(handler, enabled);
};
44 changes: 44 additions & 0 deletions src/renderer/hooks/use-rating-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { MutableRefObject, useCallback, useEffect } from 'react';
import { usePlayerStore } from '/@/renderer/store';
import type { AgGridReact } from '@ag-grid-community/react';

export const useRatingChange = (
handler: (ids: string[], rating: number | null) => void,
enabled: boolean,
) => {
useEffect(() => {
if (!enabled) return () => {};

const unSubChange = usePlayerStore.subscribe(
(state) => state.rating,
(value) => value && handler(value.ids, value.rating),
);

return () => {
unSubChange();
};
}, [enabled, handler]);
};

export const useTableRatingChange = (
tableRef: MutableRefObject<AgGridReact | null>,
enabled: boolean,
) => {
const handler = useCallback(
(ids: string[], rating: number | null) => {
const api = tableRef.current?.api;
if (api) {
for (const id of ids) {
const node = api.getRowNode(id);
if (node && node.data.userRating !== rating) {
node.setDataValue('userRating', rating);
api.redrawRows({ rowNodes: [node] });
}
}
}
},
[tableRef],
);

useRatingChange(handler, enabled);
};
19 changes: 19 additions & 0 deletions src/renderer/store/player.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ export interface PlayerState {
time: number;
};
fallback: boolean | null;
favorite?: {
favorite: boolean;
ids: string[];
};
muted: boolean;
queue: {
default: QueueSong[];
previousNode?: QueueSong;
shuffled: string[];
sorted: QueueSong[];
};
rating?: {
ids: string[];
rating: number | null;
};
repeat: PlayerRepeat;
shuffle: PlayerShuffle;
volume: number;
Expand Down Expand Up @@ -847,6 +855,13 @@ export const usePlayerStore = create<PlayerSlice>()(
});
}

set((state) => {
state.favorite = {
favorite,
ids,
};
});

return foundUniqueIds;
},
setMuted: (muted: boolean) => {
Expand Down Expand Up @@ -877,6 +892,10 @@ export const usePlayerStore = create<PlayerSlice>()(
});
}

set((state) => {
state.rating = { ids, rating };
});

return foundUniqueIds;
},
setRepeat: (type: PlayerRepeat) => {
Expand Down
Loading