2020-05-01 06:22:24 -08:00
|
|
|
import React, { useState } from 'react'
|
2020-05-22 11:23:42 -08:00
|
|
|
import PropTypes from 'prop-types'
|
2020-05-01 06:22:24 -08:00
|
|
|
import { useDispatch } from 'react-redux'
|
2020-05-22 11:23:42 -08:00
|
|
|
import { useUpdate, useTranslate, useRefresh, useNotify } from 'react-admin'
|
2020-05-01 06:22:24 -08:00
|
|
|
import { IconButton, Menu, MenuItem } from '@material-ui/core'
|
2020-05-22 11:23:42 -08:00
|
|
|
import { makeStyles } from '@material-ui/core/styles'
|
2020-05-01 06:22:24 -08:00
|
|
|
import MoreVertIcon from '@material-ui/icons/MoreVert'
|
2020-05-22 11:23:42 -08:00
|
|
|
import StarIcon from '@material-ui/icons/Star'
|
|
|
|
|
import StarBorderIcon from '@material-ui/icons/StarBorder'
|
|
|
|
|
import NestedMenuItem from 'material-ui-nested-menu-item'
|
2020-05-05 08:07:50 -08:00
|
|
|
import { addTracks, setTrack } from '../audioplayer'
|
2020-05-17 14:55:17 -08:00
|
|
|
import { AddToPlaylistMenu } from '../common'
|
2020-05-22 11:23:42 -08:00
|
|
|
import config from '../config'
|
|
|
|
|
|
|
|
|
|
const useStyles = makeStyles({
|
|
|
|
|
noWrap: {
|
|
|
|
|
whiteSpace: 'nowrap',
|
|
|
|
|
},
|
|
|
|
|
})
|
2020-05-01 06:22:24 -08:00
|
|
|
|
2020-05-22 11:23:42 -08:00
|
|
|
export const SongContextMenu = ({ className, record, onAddToPlaylist }) => {
|
|
|
|
|
const classes = useStyles()
|
2020-05-01 06:22:24 -08:00
|
|
|
const dispatch = useDispatch()
|
|
|
|
|
const translate = useTranslate()
|
2020-05-22 11:23:42 -08:00
|
|
|
const notify = useNotify()
|
|
|
|
|
const refresh = useRefresh()
|
2020-05-01 06:22:24 -08:00
|
|
|
const [anchorEl, setAnchorEl] = useState(null)
|
|
|
|
|
const options = {
|
|
|
|
|
playNow: {
|
|
|
|
|
label: translate('resources.song.actions.playNow'),
|
|
|
|
|
action: (record) => setTrack(record),
|
|
|
|
|
},
|
|
|
|
|
addToQueue: {
|
|
|
|
|
label: translate('resources.song.actions.addToQueue'),
|
2020-05-07 07:24:28 -08:00
|
|
|
action: (record) => addTracks({ [record.id]: record }),
|
2020-05-01 06:22:24 -08:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleClick = (e) => {
|
|
|
|
|
setAnchorEl(e.currentTarget)
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleClose = (e) => {
|
|
|
|
|
setAnchorEl(null)
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleItemClick = (e) => {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
setAnchorEl(null)
|
|
|
|
|
const key = e.target.getAttribute('value')
|
|
|
|
|
dispatch(options[key].action(record))
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 11:23:42 -08:00
|
|
|
const [toggleStar, { toggling: loading }] = useUpdate(
|
|
|
|
|
'albumSong',
|
|
|
|
|
record.id,
|
|
|
|
|
record,
|
|
|
|
|
{
|
|
|
|
|
undoable: false,
|
|
|
|
|
onFailure: (error) => {
|
|
|
|
|
console.log(error)
|
|
|
|
|
notify('ra.page.error', 'warning')
|
|
|
|
|
refresh()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const handleToggleStar = (e, record) => {
|
|
|
|
|
record.starred = !record.starred
|
|
|
|
|
toggleStar()
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 14:55:17 -08:00
|
|
|
const open = Boolean(anchorEl)
|
|
|
|
|
|
2020-05-01 06:22:24 -08:00
|
|
|
return (
|
2020-05-22 11:23:42 -08:00
|
|
|
<span className={`${classes.noWrap} ${className}`}>
|
|
|
|
|
{config.enableStarred && (
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={(e) => handleToggleStar(e, record)}
|
|
|
|
|
size={'small'}
|
|
|
|
|
disabled={loading}
|
|
|
|
|
>
|
|
|
|
|
{record.starred ? <StarIcon /> : <StarBorderIcon />}
|
|
|
|
|
</IconButton>
|
|
|
|
|
)}
|
2020-05-02 10:50:15 -08:00
|
|
|
<IconButton onClick={handleClick} size={'small'}>
|
2020-05-01 06:22:24 -08:00
|
|
|
<MoreVertIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
<Menu
|
|
|
|
|
id={'menu' + record.id}
|
|
|
|
|
anchorEl={anchorEl}
|
2020-05-17 14:55:17 -08:00
|
|
|
open={open}
|
2020-05-01 06:22:24 -08:00
|
|
|
onClose={handleClose}
|
|
|
|
|
>
|
|
|
|
|
{Object.keys(options).map((key) => (
|
|
|
|
|
<MenuItem value={key} key={key} onClick={handleItemClick}>
|
|
|
|
|
{options[key].label}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
))}
|
2020-05-17 14:55:17 -08:00
|
|
|
<NestedMenuItem
|
|
|
|
|
label={translate('resources.song.actions.addToPlaylist')}
|
|
|
|
|
parentMenuOpen={open}
|
|
|
|
|
>
|
|
|
|
|
<AddToPlaylistMenu
|
2020-05-18 09:05:54 -08:00
|
|
|
selectedIds={[record.mediaFileId || record.id]}
|
|
|
|
|
onClose={handleClose}
|
|
|
|
|
onItemAdded={onAddToPlaylist}
|
2020-05-17 14:55:17 -08:00
|
|
|
/>
|
|
|
|
|
</NestedMenuItem>
|
2020-05-01 06:22:24 -08:00
|
|
|
</Menu>
|
2020-05-22 11:23:42 -08:00
|
|
|
</span>
|
2020-05-01 06:22:24 -08:00
|
|
|
)
|
|
|
|
|
}
|
2020-05-18 09:05:54 -08:00
|
|
|
|
|
|
|
|
SongContextMenu.propTypes = {
|
|
|
|
|
record: PropTypes.object,
|
|
|
|
|
onAddToPlaylist: PropTypes.func,
|
|
|
|
|
}
|