2023-10-31 02:50:48 +01:00
|
|
|
import { useEffect } from 'react';
|
2023-01-30 02:39:25 -08:00
|
|
|
import { Flex, Group } from '@mantine/core';
|
2023-06-04 16:02:40 -07:00
|
|
|
import { useHotkeys, useMediaQuery } from '@mantine/hooks';
|
2023-07-23 12:23:18 +00:00
|
|
|
import isElectron from 'is-electron';
|
2023-10-30 19:22:45 -07:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { HiOutlineQueueList } from 'react-icons/hi2';
|
2023-01-30 02:39:25 -08:00
|
|
|
import {
|
2023-07-01 19:10:05 -07:00
|
|
|
RiVolumeUpFill,
|
|
|
|
|
RiVolumeDownFill,
|
|
|
|
|
RiVolumeMuteFill,
|
|
|
|
|
RiHeartLine,
|
|
|
|
|
RiHeartFill,
|
2023-01-30 02:39:25 -08:00
|
|
|
} from 'react-icons/ri';
|
|
|
|
|
import {
|
2023-07-01 19:10:05 -07:00
|
|
|
useAppStoreActions,
|
|
|
|
|
useCurrentServer,
|
|
|
|
|
useCurrentSong,
|
|
|
|
|
useHotkeySettings,
|
|
|
|
|
useMuted,
|
2023-10-28 03:22:16 +02:00
|
|
|
usePreviousSong,
|
2024-08-22 21:57:58 -07:00
|
|
|
useSettingsStore,
|
2023-07-01 19:10:05 -07:00
|
|
|
useSidebarStore,
|
2023-10-23 00:47:44 +00:00
|
|
|
useSpeed,
|
2023-07-01 19:10:05 -07:00
|
|
|
useVolume,
|
2023-01-30 02:39:25 -08:00
|
|
|
} from '/@/renderer/store';
|
2022-12-19 15:59:14 -08:00
|
|
|
import { useRightControls } from '../hooks/use-right-controls';
|
|
|
|
|
import { PlayerButton } from './player-button';
|
2023-10-28 03:22:16 +02:00
|
|
|
import { LibraryItem, QueueSong, ServerType, Song } from '/@/renderer/api/types';
|
2023-04-30 22:01:52 -07:00
|
|
|
import { useCreateFavorite, useDeleteFavorite, useSetRating } from '/@/renderer/features/shared';
|
2023-10-23 00:47:44 +00:00
|
|
|
import { DropdownMenu, Rating } from '/@/renderer/components';
|
2023-01-30 21:34:27 -08:00
|
|
|
import { PlayerbarSlider } from '/@/renderer/features/player/components/playerbar-slider';
|
2024-01-22 05:06:46 +01:00
|
|
|
import { Slider } from '/@/renderer/components/slider';
|
2022-12-19 15:59:14 -08:00
|
|
|
|
2023-07-23 12:23:18 +00:00
|
|
|
const ipc = isElectron() ? window.electron.ipc : null;
|
|
|
|
|
const remote = isElectron() ? window.electron.remote : null;
|
|
|
|
|
|
2022-12-19 15:59:14 -08:00
|
|
|
export const RightControls = () => {
|
2023-10-30 19:22:45 -07:00
|
|
|
const { t } = useTranslation();
|
2023-07-01 19:10:05 -07:00
|
|
|
const isMinWidth = useMediaQuery('(max-width: 480px)');
|
|
|
|
|
const volume = useVolume();
|
|
|
|
|
const muted = useMuted();
|
|
|
|
|
const server = useCurrentServer();
|
|
|
|
|
const currentSong = useCurrentSong();
|
2023-10-28 03:22:16 +02:00
|
|
|
const previousSong = usePreviousSong();
|
2023-07-01 19:10:05 -07:00
|
|
|
const { setSideBar } = useAppStoreActions();
|
|
|
|
|
const { rightExpanded: isQueueExpanded } = useSidebarStore();
|
|
|
|
|
const { bindings } = useHotkeySettings();
|
2023-10-23 00:47:44 +00:00
|
|
|
const {
|
|
|
|
|
handleVolumeSlider,
|
|
|
|
|
handleVolumeWheel,
|
|
|
|
|
handleMute,
|
|
|
|
|
handleVolumeDown,
|
|
|
|
|
handleVolumeUp,
|
|
|
|
|
handleSpeed,
|
|
|
|
|
} = useRightControls();
|
|
|
|
|
|
|
|
|
|
const speed = useSpeed();
|
2024-08-22 21:57:58 -07:00
|
|
|
const volumeWidth = useSettingsStore((state) => state.general.volumeWidth);
|
2023-07-01 19:10:05 -07:00
|
|
|
|
|
|
|
|
const updateRatingMutation = useSetRating({});
|
|
|
|
|
const addToFavoritesMutation = useCreateFavorite({});
|
|
|
|
|
const removeFromFavoritesMutation = useDeleteFavorite({});
|
|
|
|
|
|
2023-10-28 03:22:16 +02:00
|
|
|
const handleAddToFavorites = (song: QueueSong | undefined) => {
|
|
|
|
|
if (!song?.id) return;
|
2023-07-01 19:10:05 -07:00
|
|
|
|
|
|
|
|
addToFavoritesMutation.mutate({
|
|
|
|
|
query: {
|
2023-10-28 03:22:16 +02:00
|
|
|
id: [song.id],
|
2023-07-01 19:10:05 -07:00
|
|
|
type: LibraryItem.SONG,
|
2023-01-30 21:34:27 -08:00
|
|
|
},
|
2023-10-28 03:22:16 +02:00
|
|
|
serverId: song?.serverId,
|
2023-07-01 19:10:05 -07:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleUpdateRating = (rating: number) => {
|
|
|
|
|
if (!currentSong) return;
|
|
|
|
|
|
|
|
|
|
updateRatingMutation.mutate({
|
|
|
|
|
query: {
|
|
|
|
|
item: [currentSong],
|
|
|
|
|
rating,
|
|
|
|
|
},
|
|
|
|
|
serverId: currentSong?.serverId,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-28 03:22:16 +02:00
|
|
|
const handleRemoveFromFavorites = (song: QueueSong | undefined) => {
|
|
|
|
|
if (!song?.id) return;
|
2023-07-01 19:10:05 -07:00
|
|
|
|
|
|
|
|
removeFromFavoritesMutation.mutate({
|
|
|
|
|
query: {
|
2023-10-28 03:22:16 +02:00
|
|
|
id: [song.id],
|
2023-07-01 19:10:05 -07:00
|
|
|
type: LibraryItem.SONG,
|
|
|
|
|
},
|
2023-10-28 03:22:16 +02:00
|
|
|
serverId: song?.serverId,
|
2023-07-01 19:10:05 -07:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-28 03:22:16 +02:00
|
|
|
const handleToggleFavorite = (song: QueueSong | undefined) => {
|
|
|
|
|
if (!song?.id) return;
|
2023-07-01 19:10:05 -07:00
|
|
|
|
2023-10-28 03:22:16 +02:00
|
|
|
if (song.userFavorite) {
|
|
|
|
|
handleRemoveFromFavorites(song);
|
2023-07-01 19:10:05 -07:00
|
|
|
} else {
|
2023-10-28 03:22:16 +02:00
|
|
|
handleAddToFavorites(song);
|
2023-07-01 19:10:05 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleToggleQueue = () => {
|
|
|
|
|
setSideBar({ rightExpanded: !isQueueExpanded });
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-22 05:06:46 +01:00
|
|
|
const formatPlaybackSpeedSliderLabel = (value: number) => {
|
|
|
|
|
const bpm = Number(currentSong?.bpm);
|
|
|
|
|
if (bpm > 0) {
|
|
|
|
|
return `${value} x / ${(bpm * value).toFixed(1)} BPM`;
|
|
|
|
|
}
|
|
|
|
|
return `${value} x`;
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const isSongDefined = Boolean(currentSong?.id);
|
|
|
|
|
const showRating = isSongDefined && server?.type === ServerType.NAVIDROME;
|
|
|
|
|
|
|
|
|
|
useHotkeys([
|
|
|
|
|
[bindings.volumeDown.isGlobal ? '' : bindings.volumeDown.hotkey, handleVolumeDown],
|
|
|
|
|
[bindings.volumeUp.isGlobal ? '' : bindings.volumeUp.hotkey, handleVolumeUp],
|
|
|
|
|
[bindings.volumeMute.isGlobal ? '' : bindings.volumeMute.hotkey, handleMute],
|
|
|
|
|
[bindings.toggleQueue.isGlobal ? '' : bindings.toggleQueue.hotkey, handleToggleQueue],
|
2023-10-28 03:22:16 +02:00
|
|
|
[
|
|
|
|
|
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),
|
|
|
|
|
],
|
2023-10-31 02:50:48 +01:00
|
|
|
[bindings.rate0.isGlobal ? '' : bindings.rate0.hotkey, () => handleUpdateRating(0)],
|
2023-09-23 03:20:04 -07:00
|
|
|
[bindings.rate1.isGlobal ? '' : bindings.rate1.hotkey, () => handleUpdateRating(1)],
|
|
|
|
|
[bindings.rate2.isGlobal ? '' : bindings.rate2.hotkey, () => handleUpdateRating(2)],
|
|
|
|
|
[bindings.rate3.isGlobal ? '' : bindings.rate3.hotkey, () => handleUpdateRating(3)],
|
|
|
|
|
[bindings.rate4.isGlobal ? '' : bindings.rate4.hotkey, () => handleUpdateRating(4)],
|
|
|
|
|
[bindings.rate5.isGlobal ? '' : bindings.rate5.hotkey, () => handleUpdateRating(5)],
|
2023-07-01 19:10:05 -07:00
|
|
|
]);
|
|
|
|
|
|
2023-07-23 12:23:18 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (remote) {
|
|
|
|
|
remote.requestFavorite((_event, { favorite, id, serverId }) => {
|
|
|
|
|
const mutator = favorite ? addToFavoritesMutation : removeFromFavoritesMutation;
|
|
|
|
|
mutator.mutate({
|
|
|
|
|
query: {
|
|
|
|
|
id: [id],
|
|
|
|
|
type: LibraryItem.SONG,
|
|
|
|
|
},
|
|
|
|
|
serverId,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
remote.requestRating((_event, { id, rating, serverId }) => {
|
|
|
|
|
updateRatingMutation.mutate({
|
|
|
|
|
query: {
|
|
|
|
|
item: [
|
|
|
|
|
{
|
|
|
|
|
id,
|
|
|
|
|
itemType: LibraryItem.SONG,
|
|
|
|
|
serverId,
|
|
|
|
|
} as Song, // This is not a type-safe cast, but it works because those are all the prop
|
|
|
|
|
],
|
|
|
|
|
rating,
|
|
|
|
|
},
|
|
|
|
|
serverId,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
ipc?.removeAllListeners('request-favorite');
|
|
|
|
|
ipc?.removeAllListeners('request-rating');
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {};
|
|
|
|
|
}, [addToFavoritesMutation, removeFromFavoritesMutation, updateRatingMutation]);
|
|
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
|
|
|
|
<Flex
|
|
|
|
|
align="flex-end"
|
|
|
|
|
direction="column"
|
|
|
|
|
h="100%"
|
|
|
|
|
px="1rem"
|
|
|
|
|
py="0.5rem"
|
2023-01-30 21:34:27 -08:00
|
|
|
>
|
2023-07-01 19:10:05 -07:00
|
|
|
<Group h="calc(100% / 3)">
|
|
|
|
|
{showRating && (
|
|
|
|
|
<Rating
|
|
|
|
|
size="sm"
|
|
|
|
|
value={currentSong?.userRating || 0}
|
|
|
|
|
onChange={handleUpdateRating}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Group>
|
|
|
|
|
<Group
|
|
|
|
|
noWrap
|
|
|
|
|
align="center"
|
|
|
|
|
spacing="xs"
|
|
|
|
|
>
|
2024-01-22 05:06:46 +01:00
|
|
|
<DropdownMenu
|
|
|
|
|
withArrow
|
|
|
|
|
arrowOffset={12}
|
|
|
|
|
offset={0}
|
|
|
|
|
position="top-end"
|
|
|
|
|
width={425}
|
|
|
|
|
>
|
2023-10-23 00:47:44 +00:00
|
|
|
<DropdownMenu.Target>
|
|
|
|
|
<PlayerButton
|
|
|
|
|
icon={<>{speed} x</>}
|
|
|
|
|
tooltip={{
|
2023-10-30 19:22:45 -07:00
|
|
|
label: t('player.playbackSpeed', { postProcess: 'sentenceCase' }),
|
2023-10-23 00:47:44 +00:00
|
|
|
openDelay: 500,
|
|
|
|
|
}}
|
|
|
|
|
variant="secondary"
|
|
|
|
|
/>
|
|
|
|
|
</DropdownMenu.Target>
|
|
|
|
|
<DropdownMenu.Dropdown>
|
2024-01-22 05:06:46 +01:00
|
|
|
<Slider
|
|
|
|
|
label={formatPlaybackSpeedSliderLabel}
|
|
|
|
|
marks={[
|
|
|
|
|
{ label: '0.5', value: 0.5 },
|
|
|
|
|
{ label: '0.75', value: 0.75 },
|
|
|
|
|
{ label: '1', value: 1 },
|
|
|
|
|
{ label: '1.25', value: 1.25 },
|
|
|
|
|
{ label: '1.5', value: 1.5 },
|
|
|
|
|
]}
|
|
|
|
|
max={1.5}
|
|
|
|
|
min={0.5}
|
|
|
|
|
step={0.01}
|
|
|
|
|
styles={{
|
|
|
|
|
markLabel: {
|
|
|
|
|
paddingTop: '0.5rem',
|
|
|
|
|
},
|
|
|
|
|
root: {
|
|
|
|
|
margin: '1rem 1rem 2rem 1rem',
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
value={speed}
|
|
|
|
|
onChange={handleSpeed}
|
|
|
|
|
onDoubleClick={() => handleSpeed(1)}
|
|
|
|
|
/>
|
2023-10-23 00:47:44 +00:00
|
|
|
</DropdownMenu.Dropdown>
|
|
|
|
|
</DropdownMenu>
|
2023-07-01 19:10:05 -07:00
|
|
|
<PlayerButton
|
|
|
|
|
icon={
|
|
|
|
|
currentSong?.userFavorite ? (
|
|
|
|
|
<RiHeartFill
|
|
|
|
|
color="var(--primary-color)"
|
|
|
|
|
size="1.1rem"
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<RiHeartLine size="1.1rem" />
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
sx={{
|
|
|
|
|
svg: {
|
|
|
|
|
fill: !currentSong?.userFavorite
|
|
|
|
|
? undefined
|
|
|
|
|
: 'var(--primary-color) !important',
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
tooltip={{
|
2023-10-30 19:22:45 -07:00
|
|
|
label: currentSong?.userFavorite
|
|
|
|
|
? t('player.unfavorite', { postProcess: 'titleCase' })
|
|
|
|
|
: t('player.favorite', { postProcess: 'titleCase' }),
|
2023-07-01 19:10:05 -07:00
|
|
|
openDelay: 500,
|
|
|
|
|
}}
|
|
|
|
|
variant="secondary"
|
2023-10-28 03:22:16 +02:00
|
|
|
onClick={() => handleToggleFavorite(currentSong)}
|
2023-07-01 19:10:05 -07:00
|
|
|
/>
|
2023-10-23 00:47:44 +00:00
|
|
|
{!isMinWidth ? (
|
|
|
|
|
<PlayerButton
|
|
|
|
|
icon={<HiOutlineQueueList size="1.1rem" />}
|
2024-09-26 12:42:41 +08:00
|
|
|
tooltip={{
|
|
|
|
|
label: t('player.viewQueue', { postProcess: 'titleCase' }),
|
|
|
|
|
openDelay: 500,
|
|
|
|
|
}}
|
2023-10-23 00:47:44 +00:00
|
|
|
variant="secondary"
|
|
|
|
|
onClick={handleToggleQueue}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
2023-07-01 19:10:05 -07:00
|
|
|
<Group
|
|
|
|
|
noWrap
|
|
|
|
|
spacing="xs"
|
|
|
|
|
>
|
|
|
|
|
<PlayerButton
|
|
|
|
|
icon={
|
|
|
|
|
muted ? (
|
|
|
|
|
<RiVolumeMuteFill size="1.2rem" />
|
|
|
|
|
) : volume > 50 ? (
|
|
|
|
|
<RiVolumeUpFill size="1.2rem" />
|
|
|
|
|
) : (
|
|
|
|
|
<RiVolumeDownFill size="1.2rem" />
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-10-30 19:22:45 -07:00
|
|
|
tooltip={{
|
|
|
|
|
label: muted ? t('player.muted', { postProcess: 'titleCase' }) : volume,
|
|
|
|
|
openDelay: 500,
|
|
|
|
|
}}
|
2023-07-01 19:10:05 -07:00
|
|
|
variant="secondary"
|
|
|
|
|
onClick={handleMute}
|
|
|
|
|
onWheel={handleVolumeWheel}
|
|
|
|
|
/>
|
|
|
|
|
{!isMinWidth ? (
|
|
|
|
|
<PlayerbarSlider
|
|
|
|
|
max={100}
|
|
|
|
|
min={0}
|
|
|
|
|
size={6}
|
|
|
|
|
value={volume}
|
2024-08-22 21:57:58 -07:00
|
|
|
w={volumeWidth}
|
2023-07-01 19:10:05 -07:00
|
|
|
onChange={handleVolumeSlider}
|
|
|
|
|
onWheel={handleVolumeWheel}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</Group>
|
|
|
|
|
</Group>
|
|
|
|
|
<Group h="calc(100% / 3)" />
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
2022-12-19 15:59:14 -08:00
|
|
|
};
|