From 7a580c2c657522946fde16b77b7b8723b16b9404 Mon Sep 17 00:00:00 2001 From: Martin Pauli Date: Sat, 28 Oct 2023 03:22:16 +0200 Subject: [PATCH] Add favorite hotkey options (#326) * Add favorite hotkey options * Update wording --------- Co-authored-by: Jeff <42182408+jeffvli@users.noreply.github.com> --- .../player/components/right-controls.tsx | 56 ++++++++++++++----- .../hotkeys/hotkey-manager-settings.tsx | 6 ++ src/renderer/store/player.store.ts | 9 +++ src/renderer/store/settings.store.ts | 12 ++++ 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/src/renderer/features/player/components/right-controls.tsx b/src/renderer/features/player/components/right-controls.tsx index b3402b7e0..dcf137e9e 100644 --- a/src/renderer/features/player/components/right-controls.tsx +++ b/src/renderer/features/player/components/right-controls.tsx @@ -16,13 +16,14 @@ import { useCurrentSong, useHotkeySettings, useMuted, + usePreviousSong, useSidebarStore, useSpeed, useVolume, } from '/@/renderer/store'; import { useRightControls } from '../hooks/use-right-controls'; import { PlayerButton } from './player-button'; -import { LibraryItem, ServerType, Song } from '/@/renderer/api/types'; +import { LibraryItem, QueueSong, ServerType, Song } from '/@/renderer/api/types'; import { useCreateFavorite, useDeleteFavorite, useSetRating } from '/@/renderer/features/shared'; import { DropdownMenu, Rating } from '/@/renderer/components'; import { PlayerbarSlider } from '/@/renderer/features/player/components/playerbar-slider'; @@ -38,6 +39,7 @@ export const RightControls = () => { const muted = useMuted(); const server = useCurrentServer(); const currentSong = useCurrentSong(); + const previousSong = usePreviousSong(); const { setSideBar } = useAppStoreActions(); const { rightExpanded: isQueueExpanded } = useSidebarStore(); const { bindings } = useHotkeySettings(); @@ -56,15 +58,15 @@ export const RightControls = () => { const addToFavoritesMutation = useCreateFavorite({}); const removeFromFavoritesMutation = useDeleteFavorite({}); - const handleAddToFavorites = () => { - if (!currentSong) return; + const handleAddToFavorites = (song: QueueSong | undefined) => { + if (!song?.id) return; addToFavoritesMutation.mutate({ query: { - id: [currentSong.id], + id: [song.id], type: LibraryItem.SONG, }, - serverId: currentSong?.serverId, + serverId: song?.serverId, }); }; @@ -92,25 +94,25 @@ export const RightControls = () => { }); }; - const handleRemoveFromFavorites = () => { - if (!currentSong) return; + const handleRemoveFromFavorites = (song: QueueSong | undefined) => { + if (!song?.id) return; removeFromFavoritesMutation.mutate({ query: { - id: [currentSong.id], + id: [song.id], type: LibraryItem.SONG, }, - serverId: currentSong?.serverId, + serverId: song?.serverId, }); }; - const handleToggleFavorite = () => { - if (!currentSong) return; + const handleToggleFavorite = (song: QueueSong | undefined) => { + if (!song?.id) return; - if (currentSong.userFavorite) { - handleRemoveFromFavorites(); + if (song.userFavorite) { + handleRemoveFromFavorites(song); } else { - handleAddToFavorites(); + handleAddToFavorites(song); } }; @@ -126,6 +128,30 @@ export const RightControls = () => { [bindings.volumeUp.isGlobal ? '' : bindings.volumeUp.hotkey, handleVolumeUp], [bindings.volumeMute.isGlobal ? '' : bindings.volumeMute.hotkey, handleMute], [bindings.toggleQueue.isGlobal ? '' : bindings.toggleQueue.hotkey, handleToggleQueue], + [ + bindings.favoriteCurrentAdd.isGlobal ? '' : bindings.favoriteCurrentAdd.hotkey, + () => handleAddToFavorites(currentSong), + ], + [ + bindings.favoriteCurrentRemove.isGlobal ? '' : bindings.favoriteCurrentRemove.hotkey, + () => handleRemoveFromFavorites(currentSong), + ], + [ + bindings.favoriteCurrentToggle.isGlobal ? '' : bindings.favoriteCurrentToggle.hotkey, + () => handleToggleFavorite(currentSong), + ], + [ + bindings.favoritePreviousAdd.isGlobal ? '' : bindings.favoritePreviousAdd.hotkey, + () => handleAddToFavorites(previousSong), + ], + [ + bindings.favoritePreviousRemove.isGlobal ? '' : bindings.favoritePreviousRemove.hotkey, + () => handleRemoveFromFavorites(previousSong), + ], + [ + bindings.favoritePreviousToggle.isGlobal ? '' : bindings.favoritePreviousToggle.hotkey, + () => handleToggleFavorite(previousSong), + ], [bindings.rate0.isGlobal ? '' : bindings.rate0.hotkey, () => handleClearRating(null, 0)], [bindings.rate1.isGlobal ? '' : bindings.rate1.hotkey, () => handleUpdateRating(1)], [bindings.rate2.isGlobal ? '' : bindings.rate2.hotkey, () => handleUpdateRating(2)], @@ -240,7 +266,7 @@ export const RightControls = () => { openDelay: 500, }} variant="secondary" - onClick={handleToggleFavorite} + onClick={() => handleToggleFavorite(currentSong)} /> {!isMinWidth ? ( = { browserBack: 'Browser back', browserForward: 'Browser forward', + favoriteCurrentAdd: 'Favorite current song', + favoriteCurrentRemove: 'Unfavorite current song', + favoriteCurrentToggle: 'Toggle current song favorite', + favoritePreviousAdd: 'Favorite previous song', + favoritePreviousRemove: 'Unfavorite previous song', + favoritePreviousToggle: 'Toggle previous song favorite', globalSearch: 'Global search', localSearch: 'In-page search', next: 'Next track', diff --git a/src/renderer/store/player.store.ts b/src/renderer/store/player.store.ts index caeab9e78..f477bb0b7 100644 --- a/src/renderer/store/player.store.ts +++ b/src/renderer/store/player.store.ts @@ -827,6 +827,15 @@ export const usePlayerStore = create()( }); } + const previousSongId = get().queue.previousNode?.id; + if (previousSongId && ids.includes(previousSongId)) { + set((state) => { + if (state.queue.previousNode) { + state.queue.previousNode.userFavorite = favorite; + } + }); + } + return foundUniqueIds; }, setMuted: (muted: boolean) => { diff --git a/src/renderer/store/settings.store.ts b/src/renderer/store/settings.store.ts index 51a876e33..10bd2a57d 100644 --- a/src/renderer/store/settings.store.ts +++ b/src/renderer/store/settings.store.ts @@ -84,6 +84,12 @@ type MpvSettings = { export enum BindingActions { BROWSER_BACK = 'browserBack', BROWSER_FORWARD = 'browserForward', + FAVORITE_CURRENT_ADD = 'favoriteCurrentAdd', + FAVORITE_CURRENT_REMOVE = 'favoriteCurrentRemove', + FAVORITE_CURRENT_TOGGLE = 'favoriteCurrentToggle', + FAVORITE_PREVIOUS_ADD = 'favoritePreviousAdd', + FAVORITE_PREVIOUS_REMOVE = 'favoritePreviousRemove', + FAVORITE_PREVIOUS_TOGGLE = 'favoritePreviousToggle', GLOBAL_SEARCH = 'globalSearch', LOCAL_SEARCH = 'localSearch', MUTE = 'volumeMute', @@ -262,6 +268,12 @@ const initialState: SettingsState = { bindings: { browserBack: { allowGlobal: false, hotkey: '', isGlobal: false }, browserForward: { allowGlobal: false, hotkey: '', isGlobal: false }, + favoriteCurrentAdd: { allowGlobal: true, hotkey: '', isGlobal: false }, + favoriteCurrentRemove: { allowGlobal: true, hotkey: '', isGlobal: false }, + favoriteCurrentToggle: { allowGlobal: true, hotkey: '', isGlobal: false }, + favoritePreviousAdd: { allowGlobal: true, hotkey: '', isGlobal: false }, + favoritePreviousRemove: { allowGlobal: true, hotkey: '', isGlobal: false }, + favoritePreviousToggle: { allowGlobal: true, hotkey: '', isGlobal: false }, globalSearch: { allowGlobal: false, hotkey: 'mod+k', isGlobal: false }, localSearch: { allowGlobal: false, hotkey: 'mod+f', isGlobal: false }, next: { allowGlobal: true, hotkey: '', isGlobal: false },