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-08-24 13:50:33 -08:00
|
|
|
import { useTranslate } 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-09-21 16:10:52 -08:00
|
|
|
import { playNext, addTracks, setTrack } from '../audioplayer'
|
2020-05-25 10:50:46 -08:00
|
|
|
import { openAddToPlaylist } from '../dialogs/dialogState'
|
2020-08-14 08:11:35 -08:00
|
|
|
import subsonic from '../subsonic'
|
2020-08-24 13:50:33 -08:00
|
|
|
import StarButton from './StarButton'
|
2020-10-12 17:21:28 -08:00
|
|
|
import { formatBytes } from './SizeField'
|
2020-05-22 11:23:42 -08:00
|
|
|
|
|
|
|
|
const useStyles = makeStyles({
|
|
|
|
|
noWrap: {
|
|
|
|
|
whiteSpace: 'nowrap',
|
|
|
|
|
},
|
2020-05-22 16:15:58 -08:00
|
|
|
menu: {
|
|
|
|
|
visibility: (props) => (props.visible ? 'visible' : 'hidden'),
|
|
|
|
|
},
|
2020-05-22 11:23:42 -08:00
|
|
|
})
|
2020-05-01 06:22:24 -08:00
|
|
|
|
2020-05-29 07:21:53 -08:00
|
|
|
const SongContextMenu = ({
|
|
|
|
|
resource,
|
|
|
|
|
record,
|
|
|
|
|
showStar,
|
|
|
|
|
onAddToPlaylist,
|
|
|
|
|
visible,
|
|
|
|
|
}) => {
|
2020-08-24 13:50:33 -08:00
|
|
|
const classes = useStyles({ visible })
|
2020-05-01 06:22:24 -08:00
|
|
|
const dispatch = useDispatch()
|
|
|
|
|
const translate = useTranslate()
|
|
|
|
|
const [anchorEl, setAnchorEl] = useState(null)
|
|
|
|
|
const options = {
|
|
|
|
|
playNow: {
|
2020-10-12 17:21:28 -08:00
|
|
|
label: translate('resources.song.actions.playNow'),
|
2020-08-14 08:11:35 -08:00
|
|
|
action: (record) => dispatch(setTrack(record)),
|
2020-05-01 06:22:24 -08:00
|
|
|
},
|
2020-09-21 16:10:52 -08:00
|
|
|
playNext: {
|
2020-10-12 17:21:28 -08:00
|
|
|
label: translate('resources.song.actions.playNext'),
|
2020-09-21 16:10:52 -08:00
|
|
|
action: (record) => dispatch(playNext({ [record.id]: record })),
|
|
|
|
|
},
|
2020-05-01 06:22:24 -08:00
|
|
|
addToQueue: {
|
2020-10-12 17:21:28 -08:00
|
|
|
label: translate('resources.song.actions.addToQueue'),
|
2020-08-14 08:11:35 -08:00
|
|
|
action: (record) => dispatch(addTracks({ [record.id]: record })),
|
2020-05-01 06:22:24 -08:00
|
|
|
},
|
2020-05-25 10:50:46 -08:00
|
|
|
addToPlaylist: {
|
2020-10-12 17:21:28 -08:00
|
|
|
label: translate('resources.song.actions.addToPlaylist'),
|
2020-05-25 10:50:46 -08:00
|
|
|
action: (record) =>
|
2020-08-14 08:11:35 -08:00
|
|
|
dispatch(
|
|
|
|
|
openAddToPlaylist({
|
|
|
|
|
selectedIds: [record.mediaFileId || record.id],
|
|
|
|
|
onSuccess: (id) => onAddToPlaylist(id),
|
|
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
download: {
|
2020-10-12 17:21:28 -08:00
|
|
|
label: `${translate('resources.song.actions.download')} (${formatBytes(
|
|
|
|
|
record.size
|
|
|
|
|
)})`,
|
2020-08-14 08:11:35 -08:00
|
|
|
action: (record) => subsonic.download(record.id),
|
2020-05-25 10:50:46 -08:00
|
|
|
},
|
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')
|
2020-08-14 08:11:35 -08:00
|
|
|
options[key].action(record)
|
2020-05-01 06:22:24 -08:00
|
|
|
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 16:15:58 -08:00
|
|
|
<span className={classes.noWrap}>
|
2020-06-11 18:11:59 -08:00
|
|
|
{showStar && (
|
2020-08-24 13:50:33 -08:00
|
|
|
<StarButton record={record} resource={resource} visible={visible} />
|
2020-05-22 11:23:42 -08:00
|
|
|
)}
|
2020-08-24 13:50:33 -08:00
|
|
|
<IconButton onClick={handleClick} size={'small'} className={classes.menu}>
|
2020-05-29 18:50:33 -08:00
|
|
|
<MoreVertIcon fontSize={'small'} />
|
2020-05-01 06:22:24 -08:00
|
|
|
</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}>
|
2020-10-12 17:21:28 -08:00
|
|
|
{options[key].label}
|
2020-05-01 06:22:24 -08:00
|
|
|
</MenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</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 = {
|
2020-09-02 08:41:21 -08:00
|
|
|
resource: PropTypes.string.isRequired,
|
|
|
|
|
record: PropTypes.object.isRequired,
|
2020-05-18 09:05:54 -08:00
|
|
|
onAddToPlaylist: PropTypes.func,
|
2020-05-22 16:15:58 -08:00
|
|
|
visible: PropTypes.bool,
|
2020-05-23 10:11:39 -08:00
|
|
|
showStar: PropTypes.bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SongContextMenu.defaultProps = {
|
2020-05-25 10:50:46 -08:00
|
|
|
onAddToPlaylist: () => {},
|
2020-09-02 08:41:21 -08:00
|
|
|
record: {},
|
|
|
|
|
resource: 'song',
|
2020-05-23 10:11:39 -08:00
|
|
|
visible: true,
|
|
|
|
|
showStar: true,
|
2020-05-24 07:48:48 -08:00
|
|
|
addLabel: true,
|
2020-05-18 09:05:54 -08:00
|
|
|
}
|
2020-05-22 16:15:58 -08:00
|
|
|
|
|
|
|
|
export default SongContextMenu
|