diff --git a/src/remote/components/remote-container.tsx b/src/remote/components/remote-container.tsx index 24a0154a..2213fcbb 100644 --- a/src/remote/components/remote-container.tsx +++ b/src/remote/components/remote-container.tsx @@ -1,5 +1,5 @@ import { useCallback } from 'react'; -import { Group, Image, Rating, Text, Title } from '@mantine/core'; +import { Group, Image, Text, Title } from '@mantine/core'; import { useInfo, useSend, useShowImage } from '/@/remote/store'; import { RemoteButton } from '/@/remote/components/buttons/remote-button'; import formatDuration from 'format-duration'; @@ -18,6 +18,7 @@ import { import { PlayerRepeat, PlayerStatus } from '/@/renderer/types'; import { WrapperSlider } from '/@/remote/components/wrapped-slider'; import { Tooltip } from '/@/renderer/components/tooltip'; +import { Rating } from '/@/renderer/components'; export const RemoteContainer = () => { const { repeat, shuffle, song, status, volume } = useInfo(); diff --git a/src/renderer/components/rating/index.tsx b/src/renderer/components/rating/index.tsx index 9f308a14..002b3cf7 100644 --- a/src/renderer/components/rating/index.tsx +++ b/src/renderer/components/rating/index.tsx @@ -1,12 +1,8 @@ /* eslint-disable jsx-a11y/no-static-element-interactions */ -import { MouseEvent } from 'react'; -import { Rating as MantineRating, RatingProps as MantineRatingProps } from '@mantine/core'; +import { useCallback } from 'react'; +import { Rating as MantineRating, RatingProps } from '@mantine/core'; +import debounce from 'lodash/debounce'; import styled from 'styled-components'; -import { Tooltip } from '/@/renderer/components/tooltip'; - -interface RatingProps extends Omit { - onClick: (e: MouseEvent, value: number | undefined) => void; -} const StyledRating = styled(MantineRating)` & .mantine-Rating-symbolBody { @@ -16,18 +12,28 @@ const StyledRating = styled(MantineRating)` } `; -export const Rating = ({ onClick, ...props }: RatingProps) => { - // const debouncedOnClick = debounce(onClick, 100); +export const Rating = ({ onChange, ...props }: RatingProps) => { + const valueChange = useCallback( + (rating: number) => { + if (onChange) { + if (rating === props.value) { + onChange(0); + } else { + onChange(rating); + } + } + }, + [onChange, props.value], + ); + + const debouncedOnChange = debounce(valueChange, 100); return ( - - onClick(e, props.value)} - /> - + { + debouncedOnChange(e); + }} + /> ); }; diff --git a/src/renderer/components/virtual-table/cells/rating-cell.tsx b/src/renderer/components/virtual-table/cells/rating-cell.tsx index 596d1c87..5663cf50 100644 --- a/src/renderer/components/virtual-table/cells/rating-cell.tsx +++ b/src/renderer/components/virtual-table/cells/rating-cell.tsx @@ -1,5 +1,4 @@ /* eslint-disable import/no-cycle */ -import { MouseEvent } from 'react'; import type { ICellRendererParams } from '@ag-grid-community/core'; import { Rating } from '/@/renderer/components/rating'; import { CellContainer } from '/@/renderer/components/virtual-table/cells/generic-cell'; @@ -9,8 +8,6 @@ export const RatingCell = ({ value, node }: ICellRendererParams) => { const updateRatingMutation = useSetRating({}); const handleUpdateRating = (rating: number) => { - if (!value) return; - updateRatingMutation.mutate( { query: { @@ -27,32 +24,12 @@ export const RatingCell = ({ value, node }: ICellRendererParams) => { ); }; - const handleClearRating = (e: MouseEvent) => { - e.preventDefault(); - e.stopPropagation(); - updateRatingMutation.mutate( - { - query: { - item: [value], - rating: 0, - }, - serverId: value?.serverId, - }, - { - onSuccess: () => { - node.setData({ ...node.data, userRating: 0 }); - }, - }, - ); - }; - return ( ); diff --git a/src/renderer/features/albums/components/album-detail-header.tsx b/src/renderer/features/albums/components/album-detail-header.tsx index 7c648d14..157778f2 100644 --- a/src/renderer/features/albums/components/album-detail-header.tsx +++ b/src/renderer/features/albums/components/album-detail-header.tsx @@ -55,18 +55,6 @@ export const AlbumDetailHeader = forwardRef( }); }; - const handleClearRating = () => { - if (!detailQuery?.data || !detailQuery?.data.userRating) return; - - updateRatingMutation.mutate({ - query: { - item: [detailQuery.data], - rating: 0, - }, - serverId: detailQuery.data.serverId, - }); - }; - const showRating = detailQuery?.data?.serverType === ServerType.NAVIDROME; return ( @@ -96,7 +84,6 @@ export const AlbumDetailHeader = forwardRef( } value={detailQuery?.data?.userRating || 0} onChange={handleUpdateRating} - onClick={handleClearRating} /> )} diff --git a/src/renderer/features/artists/components/album-artist-detail-header.tsx b/src/renderer/features/artists/components/album-artist-detail-header.tsx index 1131f9e8..06bdaa02 100644 --- a/src/renderer/features/artists/components/album-artist-detail-header.tsx +++ b/src/renderer/features/artists/components/album-artist-detail-header.tsx @@ -1,4 +1,4 @@ -import { forwardRef, Fragment, Ref, MouseEvent } from 'react'; +import { forwardRef, Fragment, Ref } from 'react'; import { Group, Rating, Stack } from '@mantine/core'; import { useParams } from 'react-router'; import { LibraryItem, ServerType } from '/@/renderer/api/types'; @@ -55,21 +55,6 @@ export const AlbumArtistDetailHeader = forwardRef( }); }; - const handleClearRating = (_e: MouseEvent, rating?: number) => { - if (!detailQuery?.data || !detailQuery?.data.userRating) return; - - const isSameRatingAsPrevious = rating === detailQuery.data.userRating; - if (!isSameRatingAsPrevious) return; - - updateRatingMutation.mutate({ - query: { - item: [detailQuery.data], - rating: 0, - }, - serverId: detailQuery.data.serverId, - }); - }; - const showRating = detailQuery?.data?.serverType === ServerType.NAVIDROME; return ( @@ -99,7 +84,6 @@ export const AlbumArtistDetailHeader = forwardRef( } value={detailQuery?.data?.userRating || 0} onChange={handleUpdateRating} - onClick={handleClearRating} /> )} diff --git a/src/renderer/features/context-menu/context-menu-provider.tsx b/src/renderer/features/context-menu/context-menu-provider.tsx index 50b08775..caf262da 100644 --- a/src/renderer/features/context-menu/context-menu-provider.tsx +++ b/src/renderer/features/context-menu/context-menu-provider.tsx @@ -710,7 +710,6 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => { {}} /> ), onClick: () => handleUpdateRating(0), @@ -721,7 +720,6 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => { {}} /> ), onClick: () => handleUpdateRating(1), @@ -732,7 +730,6 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => { {}} /> ), onClick: () => handleUpdateRating(2), @@ -743,7 +740,6 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => { {}} /> ), onClick: () => handleUpdateRating(3), @@ -754,7 +750,6 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => { {}} /> ), onClick: () => handleUpdateRating(4), @@ -765,7 +760,6 @@ export const ContextMenuProvider = ({ children }: ContextMenuProviderProps) => { {}} /> ), onClick: () => handleUpdateRating(5), diff --git a/src/renderer/features/player/components/right-controls.tsx b/src/renderer/features/player/components/right-controls.tsx index dcf137e9..84d1738d 100644 --- a/src/renderer/features/player/components/right-controls.tsx +++ b/src/renderer/features/player/components/right-controls.tsx @@ -212,7 +212,6 @@ export const RightControls = () => { size="sm" value={currentSong?.userRating || 0} onChange={handleUpdateRating} - onClick={handleClearRating} /> )}