2021-05-24 07:09:06 -08:00
|
|
|
import React, { useCallback, useMemo } from 'react'
|
2020-05-15 09:48:33 -08:00
|
|
|
import {
|
|
|
|
|
BulkActionsToolbar,
|
|
|
|
|
ListToolbar,
|
|
|
|
|
TextField,
|
2021-05-05 17:35:01 -08:00
|
|
|
NumberField,
|
2020-05-18 09:05:54 -08:00
|
|
|
useRefresh,
|
2020-06-04 15:05:41 -08:00
|
|
|
useDataProvider,
|
|
|
|
|
useNotify,
|
2020-11-26 13:00:53 -09:00
|
|
|
useVersion,
|
|
|
|
|
useListContext,
|
2021-06-28 05:45:30 -08:00
|
|
|
FunctionField,
|
2020-05-15 09:48:33 -08:00
|
|
|
} from 'react-admin'
|
2020-11-27 14:27:32 -09:00
|
|
|
import clsx from 'clsx'
|
2020-06-09 04:54:11 -08:00
|
|
|
import { useDispatch } from 'react-redux'
|
2020-05-15 09:48:33 -08:00
|
|
|
import { Card, useMediaQuery } from '@material-ui/core'
|
|
|
|
|
import { makeStyles } from '@material-ui/core/styles'
|
2020-06-04 15:05:41 -08:00
|
|
|
import ReactDragListView from 'react-drag-listview'
|
2020-05-22 16:15:58 -08:00
|
|
|
import {
|
|
|
|
|
DurationField,
|
2021-09-20 16:30:43 -08:00
|
|
|
SongInfo,
|
2020-05-22 16:15:58 -08:00
|
|
|
SongContextMenu,
|
2020-05-26 12:18:28 -08:00
|
|
|
SongDatagrid,
|
2020-06-19 05:12:45 -08:00
|
|
|
SongTitleField,
|
2021-05-26 05:35:13 -08:00
|
|
|
QualityInfo,
|
|
|
|
|
useSelectedFields,
|
2021-06-10 08:20:52 -08:00
|
|
|
useResourceRefresh,
|
2021-10-20 05:37:25 -08:00
|
|
|
DateField,
|
2020-05-22 16:15:58 -08:00
|
|
|
} from '../common'
|
2021-02-03 15:08:03 -09:00
|
|
|
import { AddToPlaylistDialog } from '../dialogs'
|
2020-05-30 07:17:33 -08:00
|
|
|
import { AlbumLinkField } from '../song/AlbumLinkField'
|
2020-11-08 09:15:46 -09:00
|
|
|
import { playTracks } from '../actions'
|
2020-11-26 13:00:53 -09:00
|
|
|
import PlaylistSongBulkActions from './PlaylistSongBulkActions'
|
2021-09-20 16:30:43 -08:00
|
|
|
import ExpandInfoDialog from '../dialogs/ExpandInfoDialog'
|
2020-05-15 09:48:33 -08:00
|
|
|
|
|
|
|
|
const useStyles = makeStyles(
|
|
|
|
|
(theme) => ({
|
|
|
|
|
root: {},
|
|
|
|
|
main: {
|
|
|
|
|
display: 'flex',
|
|
|
|
|
},
|
|
|
|
|
content: {
|
|
|
|
|
marginTop: 0,
|
|
|
|
|
transition: theme.transitions.create('margin-top'),
|
|
|
|
|
position: 'relative',
|
|
|
|
|
flex: '1 1 auto',
|
|
|
|
|
[theme.breakpoints.down('xs')]: {
|
|
|
|
|
boxShadow: 'none',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
bulkActionsDisplayed: {
|
|
|
|
|
marginTop: -theme.spacing(8),
|
|
|
|
|
transition: theme.transitions.create('margin-top'),
|
|
|
|
|
},
|
|
|
|
|
actions: {
|
|
|
|
|
zIndex: 2,
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'flex-end',
|
|
|
|
|
flexWrap: 'wrap',
|
|
|
|
|
},
|
|
|
|
|
noResults: { padding: 20 },
|
2020-11-26 13:00:53 -09:00
|
|
|
toolbar: {
|
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
|
},
|
|
|
|
|
row: {
|
|
|
|
|
'&:hover': {
|
|
|
|
|
'& $contextMenu': {
|
|
|
|
|
visibility: 'visible',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
contextMenu: {
|
2020-11-27 19:52:23 -09:00
|
|
|
visibility: (props) => (props.isDesktop ? 'hidden' : 'visible'),
|
2020-11-26 13:00:53 -09:00
|
|
|
},
|
2020-05-15 09:48:33 -08:00
|
|
|
}),
|
|
|
|
|
{ name: 'RaList' }
|
|
|
|
|
)
|
|
|
|
|
|
2020-06-05 06:22:31 -08:00
|
|
|
const ReorderableList = ({ readOnly, children, ...rest }) => {
|
|
|
|
|
if (readOnly) {
|
|
|
|
|
return children
|
|
|
|
|
}
|
|
|
|
|
return <ReactDragListView {...rest}>{children}</ReactDragListView>
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 14:21:47 -08:00
|
|
|
const PlaylistSongs = ({ playlistId, readOnly, actions, ...props }) => {
|
|
|
|
|
const listContext = useListContext()
|
|
|
|
|
const { data, ids, onUnselectItems } = listContext
|
2020-05-18 14:00:55 -08:00
|
|
|
const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('md'))
|
2020-11-27 19:52:23 -09:00
|
|
|
const classes = useStyles({ isDesktop })
|
|
|
|
|
const dispatch = useDispatch()
|
2020-06-04 15:05:41 -08:00
|
|
|
const dataProvider = useDataProvider()
|
2020-05-18 09:05:54 -08:00
|
|
|
const refresh = useRefresh()
|
2020-06-04 15:05:41 -08:00
|
|
|
const notify = useNotify()
|
2020-11-26 13:00:53 -09:00
|
|
|
const version = useVersion()
|
2021-06-10 08:20:52 -08:00
|
|
|
useResourceRefresh('song', 'playlist')
|
2020-05-15 09:48:33 -08:00
|
|
|
|
2020-11-26 13:00:53 -09:00
|
|
|
const onAddToPlaylist = useCallback(
|
|
|
|
|
(pls) => {
|
|
|
|
|
if (pls.id === playlistId) {
|
2020-06-04 15:05:41 -08:00
|
|
|
refresh()
|
2020-11-26 13:00:53 -09:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[playlistId, refresh]
|
|
|
|
|
)
|
2020-05-15 16:47:15 -08:00
|
|
|
|
2020-11-26 13:00:53 -09:00
|
|
|
const reorder = useCallback(
|
|
|
|
|
(playlistId, id, newPos) => {
|
|
|
|
|
dataProvider
|
|
|
|
|
.update('playlistTrack', {
|
|
|
|
|
id,
|
|
|
|
|
data: { insert_before: newPos },
|
|
|
|
|
filter: { playlist_id: playlistId },
|
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
refresh()
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
notify('ra.page.error', 'warning')
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
[dataProvider, notify, refresh]
|
|
|
|
|
)
|
2020-05-18 09:05:54 -08:00
|
|
|
|
2020-11-26 13:00:53 -09:00
|
|
|
const handleDragEnd = useCallback(
|
|
|
|
|
(from, to) => {
|
|
|
|
|
const toId = ids[to]
|
|
|
|
|
const fromId = ids[from]
|
|
|
|
|
reorder(playlistId, fromId, toId)
|
|
|
|
|
},
|
|
|
|
|
[playlistId, reorder, ids]
|
|
|
|
|
)
|
2020-06-04 15:05:41 -08:00
|
|
|
|
2021-05-24 07:09:06 -08:00
|
|
|
const toggleableFields = useMemo(() => {
|
|
|
|
|
return {
|
|
|
|
|
trackNumber: isDesktop && <TextField source="id" label={'#'} />,
|
|
|
|
|
title: <SongTitleField source="title" showTrackNumbers={false} />,
|
|
|
|
|
album: isDesktop && <AlbumLinkField source="album" />,
|
|
|
|
|
artist: isDesktop && <TextField source="artist" />,
|
|
|
|
|
duration: (
|
|
|
|
|
<DurationField source="duration" className={classes.draggable} />
|
|
|
|
|
),
|
2021-06-28 05:45:30 -08:00
|
|
|
year: isDesktop && (
|
|
|
|
|
<FunctionField
|
|
|
|
|
source="year"
|
|
|
|
|
render={(r) => r.year || ''}
|
|
|
|
|
sortByOrder={'DESC'}
|
|
|
|
|
/>
|
|
|
|
|
),
|
2021-10-20 05:37:25 -08:00
|
|
|
playCount: isDesktop && (
|
|
|
|
|
<NumberField source="playCount" sortByOrder={'DESC'} />
|
|
|
|
|
),
|
|
|
|
|
playDate: <DateField source="playDate" sortByOrder={'DESC'} showTime />,
|
2021-05-24 07:09:06 -08:00
|
|
|
quality: isDesktop && <QualityInfo source="quality" sortable={false} />,
|
2021-08-20 16:35:59 -08:00
|
|
|
channels: isDesktop && <NumberField source="channels" sortable={true} />,
|
2021-05-24 07:09:06 -08:00
|
|
|
bpm: isDesktop && <NumberField source="bpm" />,
|
|
|
|
|
}
|
|
|
|
|
}, [isDesktop, classes.draggable])
|
|
|
|
|
|
|
|
|
|
const columns = useSelectedFields({
|
|
|
|
|
resource: 'playlistTrack',
|
|
|
|
|
columns: toggleableFields,
|
2021-10-20 05:37:25 -08:00
|
|
|
defaultOff: ['channels', 'bpm', 'year', 'playCount', 'playDate'],
|
2021-05-24 07:09:06 -08:00
|
|
|
})
|
|
|
|
|
|
2020-05-15 09:48:33 -08:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ListToolbar
|
2020-11-26 13:00:53 -09:00
|
|
|
classes={{ toolbar: classes.toolbar }}
|
2020-05-15 09:48:33 -08:00
|
|
|
filters={props.filters}
|
2021-04-05 14:21:47 -08:00
|
|
|
actions={actions}
|
|
|
|
|
{...listContext}
|
2020-05-15 09:48:33 -08:00
|
|
|
/>
|
|
|
|
|
<div className={classes.main}>
|
|
|
|
|
<Card
|
2020-11-27 14:27:32 -09:00
|
|
|
className={clsx(classes.content, {
|
2021-04-05 14:21:47 -08:00
|
|
|
[classes.bulkActionsDisplayed]: listContext.selectedIds.length > 0,
|
2020-05-15 09:48:33 -08:00
|
|
|
})}
|
|
|
|
|
key={version}
|
|
|
|
|
>
|
2021-04-05 14:21:47 -08:00
|
|
|
<BulkActionsToolbar {...listContext}>
|
2021-03-25 17:40:31 -08:00
|
|
|
<PlaylistSongBulkActions
|
|
|
|
|
playlistId={playlistId}
|
|
|
|
|
onUnselectItems={onUnselectItems}
|
2021-10-22 17:11:44 -08:00
|
|
|
readOnly={readOnly}
|
2021-03-25 17:40:31 -08:00
|
|
|
/>
|
2020-11-26 13:00:53 -09:00
|
|
|
</BulkActionsToolbar>
|
|
|
|
|
<ReorderableList
|
|
|
|
|
readOnly={readOnly}
|
|
|
|
|
onDragEnd={handleDragEnd}
|
|
|
|
|
nodeSelector={'tr'}
|
|
|
|
|
>
|
|
|
|
|
<SongDatagrid
|
|
|
|
|
rowClick={(id) => dispatch(playTracks(data, ids, id))}
|
2021-04-05 14:21:47 -08:00
|
|
|
{...listContext}
|
2021-10-22 17:11:44 -08:00
|
|
|
hasBulkActions={!readOnly}
|
2020-11-26 13:00:53 -09:00
|
|
|
contextAlwaysVisible={!isDesktop}
|
|
|
|
|
classes={{ row: classes.row }}
|
2020-06-05 06:22:31 -08:00
|
|
|
>
|
2021-05-24 07:09:06 -08:00
|
|
|
{columns}
|
2020-11-26 13:00:53 -09:00
|
|
|
<SongContextMenu
|
|
|
|
|
onAddToPlaylist={onAddToPlaylist}
|
2021-04-05 18:32:25 -08:00
|
|
|
showLove={false}
|
2020-11-26 13:00:53 -09:00
|
|
|
className={classes.contextMenu}
|
|
|
|
|
/>
|
|
|
|
|
</SongDatagrid>
|
|
|
|
|
</ReorderableList>
|
2020-05-15 09:48:33 -08:00
|
|
|
</Card>
|
|
|
|
|
</div>
|
2020-05-28 04:16:31 -08:00
|
|
|
<AddToPlaylistDialog />
|
2021-09-20 16:30:43 -08:00
|
|
|
<ExpandInfoDialog content={<SongInfo />} />
|
2021-04-05 14:21:47 -08:00
|
|
|
{React.cloneElement(props.pagination, listContext)}
|
2020-05-15 09:48:33 -08:00
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 13:00:53 -09:00
|
|
|
const SanitizedPlaylistSongs = (props) => {
|
2021-04-05 14:21:47 -08:00
|
|
|
const { loaded, ...rest } = props
|
2020-11-26 13:00:53 -09:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{loaded && (
|
2021-10-19 08:54:05 -08:00
|
|
|
<PlaylistSongs
|
|
|
|
|
playlistId={props.id}
|
|
|
|
|
actions={props.actions}
|
|
|
|
|
pagination={props.pagination}
|
|
|
|
|
{...rest}
|
|
|
|
|
/>
|
2020-11-26 13:00:53 -09:00
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SanitizedPlaylistSongs
|