mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-15 04:51:06 +02:00
handle favorite/rating events for all tables
This commit is contained in:
@@ -18,6 +18,16 @@ interface UseDefaultItemListControlsArgs {
|
||||
onColumnResized?: (columnId: TableColumn, width: number) => void;
|
||||
}
|
||||
|
||||
const itemTypeMapping = {
|
||||
[LibraryItem.ALBUM]: LibraryItem.ALBUM,
|
||||
[LibraryItem.ALBUM_ARTIST]: LibraryItem.ALBUM_ARTIST,
|
||||
[LibraryItem.ARTIST]: LibraryItem.ARTIST,
|
||||
[LibraryItem.GENRE]: LibraryItem.GENRE,
|
||||
[LibraryItem.PLAYLIST]: LibraryItem.PLAYLIST,
|
||||
[LibraryItem.PLAYLIST_SONG]: LibraryItem.SONG,
|
||||
[LibraryItem.QUEUE_SONG]: LibraryItem.SONG,
|
||||
};
|
||||
|
||||
export const useDefaultItemListControls = (args?: UseDefaultItemListControlsArgs) => {
|
||||
const player = usePlayer();
|
||||
const navigate = useNavigate();
|
||||
@@ -228,7 +238,13 @@ export const useDefaultItemListControls = (args?: UseDefaultItemListControlsArgs
|
||||
return;
|
||||
}
|
||||
|
||||
player.setFavorite(item._serverId, [item.id], itemType, favorite);
|
||||
const apiItemType = itemTypeMapping[itemType] || itemType;
|
||||
|
||||
if (!item.id || !item._serverId) {
|
||||
return;
|
||||
}
|
||||
|
||||
player.setFavorite(item._serverId, [item.id], apiItemType, favorite);
|
||||
},
|
||||
|
||||
onMore: ({ event, internalState, item, itemType }: DefaultItemControlProps) => {
|
||||
@@ -294,6 +310,12 @@ export const useDefaultItemListControls = (args?: UseDefaultItemListControlsArgs
|
||||
return;
|
||||
}
|
||||
|
||||
const apiItemType = itemTypeMapping[itemType] || itemType;
|
||||
|
||||
if (!item.id || !item._serverId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const previousRating = (item as { userRating: number }).userRating || 0;
|
||||
|
||||
let newRating = rating;
|
||||
@@ -302,7 +324,7 @@ export const useDefaultItemListControls = (args?: UseDefaultItemListControlsArgs
|
||||
newRating = 0;
|
||||
}
|
||||
|
||||
player.setRating(item._serverId, [item.id], itemType, newRating);
|
||||
player.setRating(item._serverId, [item.id], apiItemType, newRating);
|
||||
},
|
||||
};
|
||||
}, [onColumnReordered, onColumnResized, navigate, player]);
|
||||
|
||||
@@ -264,6 +264,10 @@ export const useItemListInfiniteLoader = ({
|
||||
|
||||
useEffect(() => {
|
||||
const handleFavorite = (payload: UserFavoriteEventPayload) => {
|
||||
if (payload.itemType !== itemType || payload.serverId !== serverId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const idToIndexMap = data.data
|
||||
.filter(Boolean)
|
||||
.reduce((acc: Record<string, number>, item: any, index: number) => {
|
||||
@@ -271,7 +275,9 @@ export const useItemListInfiniteLoader = ({
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const dataIndexes = payload.id.map((id: string) => idToIndexMap[id]);
|
||||
const dataIndexes = payload.id
|
||||
.map((id: string) => idToIndexMap[id])
|
||||
.filter((idx) => idx !== undefined);
|
||||
|
||||
if (dataIndexes.length === 0) {
|
||||
return;
|
||||
@@ -281,6 +287,10 @@ export const useItemListInfiniteLoader = ({
|
||||
};
|
||||
|
||||
const handleRating = (payload: UserRatingEventPayload) => {
|
||||
if (payload.itemType !== itemType || payload.serverId !== serverId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const idToIndexMap = data.data
|
||||
.filter(Boolean)
|
||||
.reduce((acc: Record<string, number>, item: any, index: number) => {
|
||||
@@ -288,7 +298,9 @@ export const useItemListInfiniteLoader = ({
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const dataIndexes = payload.id.map((id: string) => idToIndexMap[id]);
|
||||
const dataIndexes = payload.id
|
||||
.map((id: string) => idToIndexMap[id])
|
||||
.filter((idx) => idx !== undefined);
|
||||
|
||||
if (dataIndexes.length === 0) {
|
||||
return;
|
||||
@@ -304,7 +316,7 @@ export const useItemListInfiniteLoader = ({
|
||||
eventEmitter.off('USER_FAVORITE', handleFavorite);
|
||||
eventEmitter.off('USER_RATING', handleRating);
|
||||
};
|
||||
}, [data, eventKey, updateItems]);
|
||||
}, [data, eventKey, itemType, serverId, updateItems]);
|
||||
|
||||
return { data: data.data, onRangeChanged, refresh, updateItems };
|
||||
};
|
||||
|
||||
@@ -145,6 +145,10 @@ export const useItemListPaginatedLoader = ({
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.itemType !== itemType || payload.serverId !== serverId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const idToIndexMap = data
|
||||
.filter(Boolean)
|
||||
.reduce((acc: Record<string, number>, item: any, index: number) => {
|
||||
@@ -152,7 +156,9 @@ export const useItemListPaginatedLoader = ({
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
const dataIndexes = payload.id.map((id: string) => idToIndexMap[id]);
|
||||
const dataIndexes = payload.id
|
||||
.map((id: string) => idToIndexMap[id])
|
||||
.filter((idx) => idx !== undefined);
|
||||
|
||||
if (dataIndexes.length === 0) {
|
||||
return;
|
||||
@@ -166,6 +172,10 @@ export const useItemListPaginatedLoader = ({
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.itemType !== itemType || payload.serverId !== serverId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const idToIndexMap = data.reduce(
|
||||
(acc: Record<string, number>, item: any, index: number) => {
|
||||
acc[item.id] = index;
|
||||
@@ -174,7 +184,9 @@ export const useItemListPaginatedLoader = ({
|
||||
{},
|
||||
);
|
||||
|
||||
const dataIndexes = payload.id.map((id: string) => idToIndexMap[id]);
|
||||
const dataIndexes = payload.id
|
||||
.map((id: string) => idToIndexMap[id])
|
||||
.filter((idx) => idx !== undefined);
|
||||
|
||||
if (dataIndexes.length === 0) {
|
||||
return;
|
||||
@@ -192,7 +204,7 @@ export const useItemListPaginatedLoader = ({
|
||||
eventEmitter.off('USER_FAVORITE', handleFavorite);
|
||||
eventEmitter.off('USER_RATING', handleRating);
|
||||
};
|
||||
}, [data, eventKey, refresh, updateItems]);
|
||||
}, [data, eventKey, itemType, serverId, refresh, updateItems]);
|
||||
|
||||
return { data, pageCount, totalItemCount };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user