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

Add favorite hotkey options #326

Merged
merged 2 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 41 additions & 15 deletions src/renderer/features/player/components/right-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand All @@ -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,
});
};

Expand Down Expand Up @@ -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);
}
};

Expand All @@ -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)],
Expand Down Expand Up @@ -240,7 +266,7 @@ export const RightControls = () => {
openDelay: 500,
}}
variant="secondary"
onClick={handleToggleFavorite}
onClick={() => handleToggleFavorite(currentSong)}
/>
{!isMinWidth ? (
<PlayerButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const ipc = isElectron() ? window.electron.ipc : null;
const BINDINGS_MAP: Record<BindingActions, string> = {
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',
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/store/player.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,15 @@ export const usePlayerStore = create<PlayerSlice>()(
});
}

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) => {
Expand Down
12 changes: 12 additions & 0 deletions src/renderer/store/settings.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 },
Expand Down