navidrome/ui/src/playlist/PlaylistSongs.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

192 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-11-26 23:00:53 +01:00
import React, { useCallback } from 'react'
2020-05-15 19:48:33 +02:00
import {
BulkActionsToolbar,
ListToolbar,
TextField,
2020-05-18 19:05:54 +02:00
useRefresh,
2020-06-05 01:05:41 +02:00
useDataProvider,
useNotify,
2020-11-26 23:00:53 +01:00
useVersion,
useListContext,
2020-05-15 19:48:33 +02:00
} from 'react-admin'
2020-11-28 00:27:32 +01:00
import clsx from 'clsx'
import { useDispatch } from 'react-redux'
2020-05-15 19:48:33 +02:00
import { Card, useMediaQuery } from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles'
2020-06-05 01:05:41 +02:00
import ReactDragListView from 'react-drag-listview'
2020-05-23 02:15:58 +02:00
import {
DurationField,
SongDetails,
SongContextMenu,
SongDatagrid,
SongTitleField,
2020-05-23 02:15:58 +02:00
} from '../common'
import AddToPlaylistDialog from '../dialogs/AddToPlaylistDialog'
2020-05-30 17:17:33 +02:00
import { AlbumLinkField } from '../song/AlbumLinkField'
import { playTracks } from '../actions'
2020-11-26 23:00:53 +01:00
import PlaylistSongBulkActions from './PlaylistSongBulkActions'
2020-05-15 19:48:33 +02: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 23:00:53 +01:00
toolbar: {
justifyContent: 'flex-start',
},
row: {
'&:hover': {
'& $contextMenu': {
visibility: 'visible',
},
},
},
contextMenu: {
visibility: (props) => (props.isDesktop ? 'hidden' : 'visible'),
2020-11-26 23:00:53 +01:00
},
2020-05-15 19:48:33 +02:00
}),
{ name: 'RaList' }
)
2020-06-05 16:22:31 +02:00
const ReorderableList = ({ readOnly, children, ...rest }) => {
if (readOnly) {
return children
}
return <ReactDragListView {...rest}>{children}</ReactDragListView>
}
2020-11-26 23:00:53 +01:00
const PlaylistSongs = ({ playlistId, readOnly, ...props }) => {
const { data, ids } = props
2020-05-15 19:48:33 +02:00
const isXsmall = useMediaQuery((theme) => theme.breakpoints.down('xs'))
const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('md'))
const classes = useStyles({ isDesktop })
const dispatch = useDispatch()
2020-06-05 01:05:41 +02:00
const dataProvider = useDataProvider()
2020-05-18 19:05:54 +02:00
const refresh = useRefresh()
2020-06-05 01:05:41 +02:00
const notify = useNotify()
2020-11-26 23:00:53 +01:00
const version = useVersion()
2020-05-15 19:48:33 +02:00
2020-11-26 23:00:53 +01:00
const onAddToPlaylist = useCallback(
(pls) => {
if (pls.id === playlistId) {
2020-06-05 01:05:41 +02:00
refresh()
2020-11-26 23:00:53 +01:00
}
},
[playlistId, refresh]
)
2020-05-16 02:47:15 +02:00
2020-11-26 23:00:53 +01: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 19:05:54 +02:00
2020-11-26 23:00:53 +01:00
const handleDragEnd = useCallback(
(from, to) => {
const toId = ids[to]
const fromId = ids[from]
reorder(playlistId, fromId, toId)
},
[playlistId, reorder, ids]
)
2020-06-05 01:05:41 +02:00
2020-05-15 19:48:33 +02:00
return (
<>
<ListToolbar
2020-11-26 23:00:53 +01:00
classes={{ toolbar: classes.toolbar }}
2020-05-15 19:48:33 +02:00
filters={props.filters}
actions={props.actions}
2020-11-26 23:00:53 +01:00
{...props}
2020-05-15 19:48:33 +02:00
/>
<div className={classes.main}>
<Card
2020-11-28 00:27:32 +01:00
className={clsx(classes.content, {
2020-11-26 23:00:53 +01:00
[classes.bulkActionsDisplayed]: props.selectedIds.length > 0,
2020-05-15 19:48:33 +02:00
})}
key={version}
>
2020-11-26 23:00:53 +01:00
<BulkActionsToolbar {...props}>
<PlaylistSongBulkActions playlistId={playlistId} />
</BulkActionsToolbar>
<ReorderableList
readOnly={readOnly}
onDragEnd={handleDragEnd}
nodeSelector={'tr'}
>
<SongDatagrid
expand={!isXsmall && <SongDetails />}
rowClick={(id) => dispatch(playTracks(data, ids, id))}
{...props}
hasBulkActions={true}
contextAlwaysVisible={!isDesktop}
classes={{ row: classes.row }}
2020-06-05 16:22:31 +02:00
>
2020-11-26 23:00:53 +01:00
{isDesktop && <TextField source="id" label={'#'} />}
<SongTitleField source="title" showTrackNumbers={false} />
{isDesktop && <AlbumLinkField source="album" />}
{isDesktop && <TextField source="artist" />}
<DurationField source="duration" className={classes.draggable} />
<SongContextMenu
onAddToPlaylist={onAddToPlaylist}
showStar={false}
className={classes.contextMenu}
/>
</SongDatagrid>
</ReorderableList>
2020-05-15 19:48:33 +02:00
</Card>
</div>
2020-05-28 14:16:31 +02:00
<AddToPlaylistDialog />
2020-05-15 19:48:33 +02:00
</>
)
}
2020-11-26 23:00:53 +01:00
const SanitizedPlaylistSongs = (props) => {
const { loaded, loading, total, ...rest } = useListContext(props)
return (
<>
{loaded && (
<PlaylistSongs
{...rest}
playlistId={props.id}
actions={props.actions}
/>
)}
</>
)
}
export default SanitizedPlaylistSongs