Add favoriting from table rows

This commit is contained in:
jeffvli 2023-01-07 16:33:14 -08:00
parent cfa4e5e45c
commit 2edffa02d0
3 changed files with 120 additions and 3 deletions

View file

@ -75,7 +75,9 @@ export interface PlayerSlice extends PlayerState {
setCurrentIndex: (index: number) => PlayerData;
setCurrentTime: (time: number) => void;
setCurrentTrack: (uniqueId: string) => PlayerData;
setFavorite: (ids: string[], favorite: boolean) => string[];
setMuted: (muted: boolean) => void;
setRating: (ids: string[], rating: number | null) => string[];
setRepeat: (type: PlayerRepeat) => PlayerData;
setShuffle: (type: PlayerShuffle) => PlayerData;
setShuffledIndex: (index: number) => PlayerData;
@ -643,11 +645,43 @@ export const usePlayerStore = create<PlayerSlice>()(
return get().actions.getPlayerData();
},
setFavorite: (ids, favorite) => {
const { default: queue } = get().queue;
const foundUniqueIds = [];
for (const id of ids) {
const foundIndex = queue.findIndex((song) => song.id === id);
if (foundIndex !== -1) {
foundUniqueIds.push(queue[foundIndex].uniqueId);
set((state) => {
state.queue.default[foundIndex].userFavorite = favorite;
});
}
}
return foundUniqueIds;
},
setMuted: (muted: boolean) => {
set((state) => {
state.muted = muted;
});
},
setRating: (ids, rating) => {
const { default: queue } = get().queue;
const foundUniqueIds = [];
for (const id of ids) {
const foundIndex = queue.findIndex((song) => song.id === id);
if (foundIndex !== -1) {
foundUniqueIds.push(queue[foundIndex].uniqueId);
set((state) => {
state.queue.default[foundIndex].userRating = rating;
});
}
}
return foundUniqueIds;
},
setRepeat: (type: PlayerRepeat) => {
set((state) => {
state.repeat = type;
@ -825,3 +859,7 @@ export const useCurrentTime = () => usePlayerStore((state) => state.current.time
export const useVolume = () => usePlayerStore((state) => state.volume);
export const useMuted = () => usePlayerStore((state) => state.muted);
export const useSetQueueFavorite = () => usePlayerStore((state) => state.actions.setFavorite);
export const useSetQueueRating = () => usePlayerStore((state) => state.actions.setRating);