mirror of
https://github.com/antebudimir/feishin.git
synced 2025-12-31 18:13:31 +00:00
* [scuffed bugfix]: Update table rating/favorite when updated anywhere else Modify player store to have temporary state for favorite/rating update Add effect handler for `virtual-table` to update rating/favorite for players Note that this does not handle song grid view. Using a similar handler for gird view did not work, as it appeared to result in inconsistent state. Finally, this is probably not the optimal solution. Performance appears fine for ~20k items, but no guarantees. * restore should update song * update song rating/favorite/played everywhere except playlist * special rule for playlists * use iterator instead
77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
import { MutableRefObject, useCallback, useEffect } from 'react';
|
|
import { useEventStore, UserEvent } from '/@/renderer/store/event.store';
|
|
import { RowNode } from '@ag-grid-community/core';
|
|
import { AgGridReact } from '@ag-grid-community/react';
|
|
import { Song } from '/@/renderer/api/types';
|
|
|
|
export const useSongChange = (
|
|
handler: (ids: string[], event: UserEvent) => void,
|
|
enabled: boolean,
|
|
) => {
|
|
useEffect(() => {
|
|
if (!enabled) return () => {};
|
|
|
|
const unSub = useEventStore.subscribe((state) => {
|
|
if (state.event) {
|
|
handler(state.ids, state.event);
|
|
}
|
|
});
|
|
|
|
return () => {
|
|
unSub();
|
|
};
|
|
}, [handler, enabled]);
|
|
};
|
|
|
|
export const useTableChange = (
|
|
tableRef: MutableRefObject<AgGridReact | null>,
|
|
enabled: boolean,
|
|
) => {
|
|
const handler = useCallback(
|
|
(ids: string[], event: UserEvent) => {
|
|
const api = tableRef.current?.api;
|
|
if (!api) return;
|
|
|
|
const rowNodes: RowNode[] = [];
|
|
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);
|
|
}
|
|
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) {
|
|
api.redrawRows({ rowNodes });
|
|
}
|
|
},
|
|
[tableRef],
|
|
);
|
|
|
|
useSongChange(handler, enabled);
|
|
};
|