feishin/src/renderer/features/player/components/right-controls.tsx

200 lines
5.4 KiB
TypeScript
Raw Normal View History

import { MouseEvent } from 'react';
2023-01-30 02:39:25 -08:00
import { Flex, Group } from '@mantine/core';
2023-05-13 00:59:19 -07:00
import { useHotkeys } from '@mantine/hooks';
2022-12-19 15:59:14 -08:00
import { HiOutlineQueueList } from 'react-icons/hi2';
2023-01-30 02:39:25 -08:00
import {
RiVolumeUpFill,
RiVolumeDownFill,
RiVolumeMuteFill,
RiHeartLine,
RiHeartFill,
} from 'react-icons/ri';
import {
useAppStoreActions,
useCurrentServer,
useCurrentSong,
2023-05-13 00:59:19 -07:00
useHotkeySettings,
2023-01-30 02:39:25 -08:00
useMuted,
useSidebarStore,
useVolume,
} from '/@/renderer/store';
2022-12-19 15:59:14 -08:00
import { useRightControls } from '../hooks/use-right-controls';
import { PlayerButton } from './player-button';
2023-01-30 02:39:25 -08:00
import { LibraryItem, ServerType } from '/@/renderer/api/types';
import { useCreateFavorite, useDeleteFavorite, useSetRating } from '/@/renderer/features/shared';
2023-01-30 02:39:25 -08:00
import { Rating } from '/@/renderer/components';
import { PlayerbarSlider } from '/@/renderer/features/player/components/playerbar-slider';
2022-12-19 15:59:14 -08:00
export const RightControls = () => {
const volume = useVolume();
const muted = useMuted();
2023-01-30 02:39:25 -08:00
const server = useCurrentServer();
const currentSong = useCurrentSong();
2023-03-28 23:59:51 -07:00
const { setSideBar } = useAppStoreActions();
2022-12-19 15:59:14 -08:00
const { rightExpanded: isQueueExpanded } = useSidebarStore();
2023-05-13 00:59:19 -07:00
const { bindings } = useHotkeySettings();
const { handleVolumeSlider, handleVolumeWheel, handleMute, handleVolumeDown, handleVolumeUp } =
useRightControls();
2022-12-19 15:59:14 -08:00
const updateRatingMutation = useSetRating({});
const addToFavoritesMutation = useCreateFavorite({});
const removeFromFavoritesMutation = useDeleteFavorite({});
2023-01-30 02:39:25 -08:00
const handleAddToFavorites = () => {
if (!currentSong) return;
addToFavoritesMutation.mutate({
query: {
id: [currentSong.id],
type: LibraryItem.SONG,
2023-01-30 02:39:25 -08:00
},
2023-05-09 02:25:57 -07:00
serverId: currentSong?.serverId,
});
2023-01-30 02:39:25 -08:00
};
const handleUpdateRating = (rating: number) => {
if (!currentSong) return;
updateRatingMutation.mutate({
query: {
item: [currentSong],
rating,
},
serverId: currentSong?.serverId,
});
};
const handleClearRating = (_e: MouseEvent<HTMLDivElement>, rating?: number) => {
if (!currentSong || !rating) return;
updateRatingMutation.mutate({
query: {
item: [currentSong],
rating: 0,
},
serverId: currentSong?.serverId,
});
};
2023-01-30 02:39:25 -08:00
const handleRemoveFromFavorites = () => {
if (!currentSong) return;
removeFromFavoritesMutation.mutate({
query: {
id: [currentSong.id],
type: LibraryItem.SONG,
2023-01-30 02:39:25 -08:00
},
2023-05-09 02:25:57 -07:00
serverId: currentSong?.serverId,
});
2023-01-30 02:39:25 -08:00
};
const handleToggleFavorite = () => {
if (!currentSong) return;
if (currentSong.userFavorite) {
handleRemoveFromFavorites();
} else {
handleAddToFavorites();
}
};
2023-05-13 00:59:19 -07:00
const handleToggleQueue = () => {
setSideBar({ rightExpanded: !isQueueExpanded });
};
2023-01-30 02:39:25 -08:00
const isSongDefined = Boolean(currentSong?.id);
const showRating = isSongDefined && server?.type === ServerType.NAVIDROME;
2023-05-13 00:59:19 -07:00
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],
]);
2022-12-19 15:59:14 -08:00
return (
2023-01-30 02:39:25 -08:00
<Flex
align="flex-end"
direction="column"
h="100%"
px="1rem"
py="0.5rem"
2023-01-30 02:39:25 -08:00
>
<Group h="calc(100% / 3)">
{showRating && (
2023-01-30 02:39:25 -08:00
<Rating
size="sm"
value={currentSong?.userRating || 0}
onChange={handleUpdateRating}
onClick={handleClearRating}
2023-01-30 02:39:25 -08:00
/>
)}
</Group>
<Group
noWrap
align="center"
spacing="xs"
>
<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={{
label: currentSong?.userFavorite ? 'Unfavorite' : 'Favorite',
openDelay: 500,
}}
variant="secondary"
onClick={handleToggleFavorite}
/>
<PlayerButton
icon={<HiOutlineQueueList size="1.1rem" />}
tooltip={{ label: 'View queue', openDelay: 500 }}
variant="secondary"
2023-05-13 00:59:19 -07:00
onClick={handleToggleQueue}
/>
<Group
noWrap
spacing="xs"
>
2022-12-19 15:59:14 -08:00
<PlayerButton
icon={
muted ? (
<RiVolumeMuteFill size="1.2rem" />
) : volume > 50 ? (
<RiVolumeUpFill size="1.2rem" />
2022-12-19 15:59:14 -08:00
) : (
<RiVolumeDownFill size="1.2rem" />
2022-12-19 15:59:14 -08:00
)
}
tooltip={{ label: muted ? 'Muted' : volume, openDelay: 500 }}
2022-12-19 15:59:14 -08:00
variant="secondary"
onClick={handleMute}
2022-12-19 15:59:14 -08:00
/>
<PlayerbarSlider
max={100}
min={0}
size={6}
value={volume}
w="60px"
onChange={handleVolumeSlider}
onWheel={handleVolumeWheel}
2022-12-19 15:59:14 -08:00
/>
2023-01-30 02:39:25 -08:00
</Group>
</Group>
<Group h="calc(100% / 3)" />
2023-01-30 02:39:25 -08:00
</Flex>
2022-12-19 15:59:14 -08:00
);
};