quintodrome/ui/src/album/AlbumGridView.js

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

111 lines
3 KiB
JavaScript
Raw Normal View History

import React from 'react'
2020-04-02 15:20:39 -08:00
import { GridList, GridListTile, GridListTileBar } from '@material-ui/core'
import { makeStyles } from '@material-ui/core/styles'
import withWidth from '@material-ui/core/withWidth'
import { Link } from 'react-router-dom'
import { linkToRecord, Loading } from 'react-admin'
2020-04-30 17:26:30 -08:00
import { withContentRect } from 'react-measure'
import subsonic from '../subsonic'
import { ArtistLinkField } from './ArtistLinkField'
2020-04-29 17:25:43 -08:00
import AlbumContextMenu from './AlbumContextMenu.js'
const useStyles = makeStyles((theme) => ({
root: {
2020-04-02 14:18:52 -08:00
margin: '20px',
},
2020-04-07 17:25:06 -08:00
gridListTile: {
2020-04-09 05:53:53 -08:00
minHeight: '180px',
minWidth: '180px',
2020-04-07 17:25:06 -08:00
},
tileBar: {
2020-04-27 05:40:45 -08:00
textAlign: 'left',
background:
'linear-gradient(to top, rgba(0,0,0,1) 0%,rgba(0,0,0,0.4) 70%,rgba(0,0,0,0) 100%)',
},
albumArtistName: {
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
2020-04-27 05:40:45 -08:00
textAlign: 'left',
fontSize: '1em',
},
artistLink: {
color: theme.palette.primary.light,
},
}))
2020-04-30 17:26:30 -08:00
const useCoverStyles = makeStyles({
cover: {
display: 'inline-block',
width: '100%',
height: (props) => props.height,
},
})
const getColsForWidth = (width) => {
if (width === 'xs') return 2
2020-04-07 17:25:06 -08:00
if (width === 'sm') return 4
if (width === 'md') return 5
2020-04-30 17:26:30 -08:00
return 6
}
2020-04-30 17:26:30 -08:00
const Cover = withContentRect('bounds')(
({ album, measureRef, contentRect }) => {
// Force height to be the same as the width determined by the GridList
// noinspection JSSuspiciousNameCombination
const classes = useCoverStyles({ height: contentRect.bounds.width })
return (
<div ref={measureRef}>
<img
src={subsonic.url('getCoverArt', album.coverArtId || 'not_found', {
size: 300,
})}
alt={album.album}
className={classes.cover}
/>
</div>
)
}
)
const LoadedAlbumGrid = ({ ids, data, basePath, width }) => {
const classes = useStyles()
return (
<div className={classes.root}>
2020-04-07 17:25:06 -08:00
<GridList cellHeight={'auto'} cols={getColsForWidth(width)} spacing={20}>
{ids.map((id) => (
<GridListTile
2020-04-07 17:25:06 -08:00
className={classes.gridListTile}
component={Link}
key={id}
to={linkToRecord(basePath, data[id].id, 'show')}
>
2020-04-30 17:26:30 -08:00
<Cover album={data[id]} />
<GridListTileBar
className={classes.tileBar}
title={data[id].name}
subtitle={
2020-04-07 17:25:06 -08:00
<div className={classes.albumArtistName}>
<ArtistLinkField
record={data[id]}
className={classes.artistLink}
>
{data[id].albumArtist}
</ArtistLinkField>
2020-04-07 17:25:06 -08:00
</div>
}
2020-04-29 17:25:43 -08:00
actionIcon={<AlbumContextMenu id={id} />}
/>
</GridListTile>
))}
</GridList>
</div>
)
}
const AlbumGridView = ({ loading, ...props }) =>
loading ? <Loading /> : <LoadedAlbumGrid {...props} />
export default withWidth()(AlbumGridView)