quintodrome/ui/src/album/AlbumDetails.js

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

204 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-11-27 12:02:02 -09:00
import React, { useMemo, useCallback } from 'react'
2020-11-13 17:11:24 -09:00
import {
Card,
CardContent,
CardMedia,
Collapse,
makeStyles,
2020-11-27 12:02:02 -09:00
Typography,
2020-11-13 17:11:24 -09:00
useMediaQuery,
} from '@material-ui/core'
import { useTranslate } from 'react-admin'
import clsx from 'clsx'
2020-08-21 08:41:23 -08:00
import Lightbox from 'react-image-lightbox'
import 'react-image-lightbox/style.css'
import subsonic from '../subsonic'
2020-11-13 17:11:24 -09:00
import {
ArtistLinkField,
2020-11-27 12:02:02 -09:00
DurationField,
2020-11-13 17:11:24 -09:00
formatRange,
2020-11-27 12:02:02 -09:00
SizeField,
StarButton,
2020-11-13 17:11:24 -09:00
} from '../common'
2020-11-13 17:11:24 -09:00
const useStyles = makeStyles((theme) => ({
root: {
2020-11-13 17:11:24 -09:00
[theme.breakpoints.down('xs')]: {
padding: '0.7em',
2021-03-15 22:21:11 -08:00
minWidth: '20em',
2020-11-13 17:11:24 -09:00
},
[theme.breakpoints.up('sm')]: {
padding: '1em',
minWidth: '32em',
},
},
cardContents: {
display: 'flex',
2020-11-13 17:11:24 -09:00
},
details: {
display: 'flex',
flexDirection: 'column',
},
content: {
flex: '2 0 auto',
},
coverParent: {
2020-11-13 17:11:24 -09:00
[theme.breakpoints.down('xs')]: {
height: '8em',
width: '8em',
minWidth: '8em',
2020-11-13 17:11:24 -09:00
},
[theme.breakpoints.up('sm')]: {
height: '10em',
width: '10em',
minWidth: '10em',
2020-11-13 17:11:24 -09:00
},
[theme.breakpoints.up('lg')]: {
height: '15em',
width: '15em',
minWidth: '15em',
2020-11-13 17:11:24 -09:00
},
},
cover: {
2020-11-22 11:01:08 -09:00
objectFit: 'contain',
cursor: 'pointer',
display: 'block',
width: '100%',
2020-11-22 11:01:08 -09:00
height: '100%',
2020-11-13 17:11:24 -09:00
},
starButton: {
top: theme.spacing(-0.2),
left: theme.spacing(0.5),
2020-11-13 17:11:24 -09:00
},
2020-11-27 12:02:02 -09:00
commentBlock: {
2020-11-13 17:11:24 -09:00
display: 'inline-block',
2020-11-27 12:02:02 -09:00
marginTop: '1em',
2020-11-13 17:11:24 -09:00
float: 'left',
wordBreak: 'break-all',
2020-11-13 17:11:24 -09:00
},
pointerCursor: {
cursor: 'pointer',
},
2020-11-13 17:11:24 -09:00
}))
2020-11-27 12:02:02 -09:00
const AlbumComment = ({ record }) => {
const classes = useStyles()
2020-11-13 17:11:24 -09:00
const [expanded, setExpanded] = React.useState(false)
2021-02-15 10:18:57 -09:00
const lines = record.comment.split('\n')
2020-11-27 12:02:02 -09:00
const formatted = useMemo(() => {
2021-02-15 10:18:57 -09:00
return lines.map((line, idx) => (
2021-02-02 14:00:06 -09:00
<span key={record.id + '-comment-' + idx}>
<span dangerouslySetInnerHTML={{ __html: line }} />
2020-11-27 12:02:02 -09:00
<br />
2021-02-02 14:00:06 -09:00
</span>
2020-11-27 12:02:02 -09:00
))
2021-02-15 10:18:57 -09:00
}, [lines, record.id])
2020-11-27 12:02:02 -09:00
const handleExpandClick = useCallback(() => {
setExpanded(!expanded)
}, [expanded, setExpanded])
2020-11-13 17:11:24 -09:00
return (
2020-11-27 12:02:02 -09:00
<Collapse
collapsedHeight={'1.5em'}
in={expanded}
timeout={'auto'}
className={clsx(
classes.commentBlock,
2021-02-15 10:18:57 -09:00
lines.length > 1 && classes.pointerCursor
)}
2020-11-27 12:02:02 -09:00
>
<Typography variant={'body1'} onClick={handleExpandClick}>
2020-11-27 12:02:02 -09:00
{formatted}
</Typography>
</Collapse>
2020-11-13 17:11:24 -09:00
)
}
const AlbumDetails = ({ record }) => {
const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('lg'))
const classes = useStyles()
2020-08-21 08:41:23 -08:00
const [isLightboxOpen, setLightboxOpen] = React.useState(false)
const translate = useTranslate()
2020-11-13 17:11:24 -09:00
const genreYear = (record) => {
let genreDateLine = []
if (record.genre) {
genreDateLine.push(record.genre)
}
const year = formatRange(record, 'year')
if (year) {
genreDateLine.push(year)
}
return genreDateLine.join(' · ')
}
const imageUrl = subsonic.getCoverArtUrl(record, 300)
const fullImageUrl = subsonic.getCoverArtUrl(record)
2020-08-21 08:41:23 -08:00
const handleOpenLightbox = React.useCallback(() => setLightboxOpen(true), [])
const handleCloseLightbox = React.useCallback(
() => setLightboxOpen(false),
[]
)
return (
<Card className={classes.root}>
<div className={classes.cardContents}>
<div className={classes.coverParent}>
<CardMedia
component={'img'}
src={imageUrl}
width="400"
height="400"
className={classes.cover}
onClick={handleOpenLightbox}
title={record.name}
2020-11-13 17:11:24 -09:00
/>
</div>
<div className={classes.details}>
<CardContent className={classes.content}>
<Typography variant="h5">
{record.name}
<StarButton
className={classes.starButton}
record={record}
resource={'album'}
size={isDesktop ? 'default' : 'small'}
aria-label="star"
color="primary"
/>
</Typography>
<Typography component="h6">
<ArtistLinkField record={record} />
</Typography>
<Typography component="p">{genreYear(record)}</Typography>
<Typography component="p">
{record.songCount}{' '}
{translate('resources.song.name', {
smart_count: record.songCount,
})}
{' · '} <DurationField record={record} source={'duration'} />{' '}
{' · '}
<SizeField record={record} source="size" />
</Typography>
2020-11-27 12:02:02 -09:00
{isDesktop && record['comment'] && <AlbumComment record={record} />}
</CardContent>
</div>
</div>
2020-11-27 12:02:02 -09:00
{!isDesktop && record['comment'] && <AlbumComment record={record} />}
2020-08-21 08:41:23 -08:00
{isLightboxOpen && (
<Lightbox
imagePadding={50}
animationDuration={200}
imageTitle={record.name}
mainSrc={fullImageUrl}
2020-08-21 08:41:23 -08:00
onCloseRequest={handleCloseLightbox}
/>
)}
</Card>
)
}
2020-02-07 11:04:45 -09:00
export default AlbumDetails