diff --git a/ui/src/audioplayer/PlayerToolbar.js b/ui/src/audioplayer/PlayerToolbar.js index 00925918..5cc381d4 100644 --- a/ui/src/audioplayer/PlayerToolbar.js +++ b/ui/src/audioplayer/PlayerToolbar.js @@ -1,26 +1,33 @@ import React from 'react' import { useLocation } from 'react-router-dom' import { useGetOne } from 'react-admin' -import IconButton from '@material-ui/core/IconButton' -import StarBorderIcon from '@material-ui/icons/StarBorder' -import { StarButton } from '../common' +import { StarButton, useToggleStar } from '../common' +import { useHotkeys } from 'react-hotkeys-hook' -const Placeholder = () => ( - - - -) +const Placeholder = () => const Toolbar = ({ id }) => { const location = useLocation() const resource = location.pathname.startsWith('/song') ? 'song' : 'albumSong' const { data, loading } = useGetOne(resource, id) + const [toggleStar, toggling] = useToggleStar(resource, data) - if (loading) { - return - } + useHotkeys( + 's', + () => { + toggleStar() + }, + {}, + [toggleStar] + ) - return + return ( + + ) } const PlayerToolbar = ({ id }) => (id ? : ) diff --git a/ui/src/common/StarButton.js b/ui/src/common/StarButton.js index bf68655c..add4d6bb 100644 --- a/ui/src/common/StarButton.js +++ b/ui/src/common/StarButton.js @@ -1,11 +1,10 @@ -import React, { useCallback, useEffect, useRef, useState } from 'react' +import React, { useCallback } from 'react' import PropTypes from 'prop-types' -import { useNotify, useDataProvider } from 'react-admin' import StarIcon from '@material-ui/icons/Star' import StarBorderIcon from '@material-ui/icons/StarBorder' import IconButton from '@material-ui/core/IconButton' import { makeStyles } from '@material-ui/core/styles' -import subsonic from '../subsonic' +import { useToggleStar } from './useToggleStar' const useStyles = makeStyles({ star: { @@ -27,52 +26,26 @@ export const StarButton = ({ size, component: Button, addLabel, + disabled, ...rest }) => { - const [loading, setLoading] = useState(false) const classes = useStyles({ color, visible, starred: record.starred }) - const notify = useNotify() + const [toggleStar, loading] = useToggleStar(resource, record) - const mountedRef = useRef(false) - useEffect(() => { - mountedRef.current = true - return () => { - mountedRef.current = false - } - }, []) - - const dataProvider = useDataProvider() - - const refreshRecord = useCallback(() => { - dataProvider.getOne(resource, { id: record.id }).then(() => { - if (mountedRef.current) { - setLoading(false) - } - }) - }, [dataProvider, record.id, resource]) - - const handleToggleStar = (e) => { - e.preventDefault() - const toggleStar = record.starred ? subsonic.unstar : subsonic.star - - setLoading(true) - toggleStar(record.id) - .then(refreshRecord) - .catch((e) => { - console.log('Error toggling star: ', e) - notify('ra.page.error', 'warning') - if (mountedRef.current) { - setLoading(false) - } - }) - e.stopPropagation() - } + const handleToggleStar = useCallback( + (e) => { + e.preventDefault() + toggleStar() + e.stopPropagation() + }, + [toggleStar] + ) return (