navidrome/ui/src/common/ContextMenus.jsx
Deluan Quintão 752b38609c
feat(artist): add Share and Download actions to the Artist detail page (#5944)
* feat(ui): add Share button to artist detail page

* feat(ui): add Download button to artist detail page

* fix(ui): scope artist share/download to album-artist content

Gate the artist Share/Download actions on album-artist stats and show the
album-artist size, since ZipArtist and the share query only cover
album_artist_id songs. Previously the total (role-inclusive) size was shown
and guest-only artists could produce an empty archive. Applies to the artist
toolbar, the shared context menu, and the download dialog title.

* fix: match artist download/share to album-artist participation

ZipArtist and the artist share query filtered the deprecated album_artist_id
column, which only stores the first album artist of a track. Secondary
album-artists (co-credited but not first) got an empty download/share even
though the UI offered it. Filter by the album-artist role participation
instead, matching the artist's album-artist stats used to gate the actions.
Also cover the artist-specific size branch of the download dialog.

* fix(share): scope artist shares to the owner's libraries

The artist share query broadened to album-artist participation, which could
pull a secondary album artist's tracks from libraries the (non-admin) share
owner cannot access into the public share. Load the artist share as the owner
so their library access is applied, mirroring how playlist shares already work.
Adds a repository test covering co-album-artist inclusion and library scoping.

* test(share): assert album participation branch of artist shares

Link the co-album-artist fixtures to albums and assert share.Albums (used by
Subsonic getShares) includes the accessible album and excludes the one in a
library the owner cannot access, so the album participation + scoping branch
is covered too.

* fix: exclude missing files from artist download/share actions

An artist's stats still count files that went missing, so the toolbar/context
menu could offer Download/Share for an artist whose files are all gone, while
the share query (missing=false) returns nothing and downloads open dead paths.
Hide the actions when the artist is missing and exclude missing files from
ZipArtist, matching the share semantics.

* refactor: dedupe artist download-size and share-owner lookups

Extract the 'album-artist download size (or none when missing)' rule into a
single artistDownloadSize() helper shared by the toolbar, context menu, and
download dialog, and factor the duplicated share-owner context lookup into a
shareRepository.ownerContext() method used by both the artist and playlist
share cases.

* refactor(ui): move artistDownloadSize helper to common

utils is for domain-agnostic, potentially portable code; this helper is
Navidrome-specific (artist stats shape), so it belongs in common. Consumers
import it directly from common/artist to avoid pulling in the common barrel.
2026-08-12 11:59:56 -04:00

283 lines
6.9 KiB
JavaScript

import React, { useState } from 'react'
import PropTypes from 'prop-types'
import { useDispatch } from 'react-redux'
import IconButton from '@material-ui/core/IconButton'
import Menu from '@material-ui/core/Menu'
import MenuItem from '@material-ui/core/MenuItem'
import MoreVertIcon from '@material-ui/icons/MoreVert'
import { MdQuestionMark } from 'react-icons/md'
import { makeStyles } from '@material-ui/core/styles'
import { useDataProvider, useNotify, useTranslate } from 'react-admin'
import clsx from 'clsx'
import {
playNext,
addTracks,
playTracks,
shuffleTracks,
openAddToPlaylist,
openDownloadMenu,
openExtendedInfoDialog,
DOWNLOAD_MENU_ALBUM,
DOWNLOAD_MENU_ARTIST,
openShareMenu,
} from '../actions'
import { LoveButton } from './LoveButton'
import config from '../config'
import { formatBytes } from '../utils'
import { artistDownloadSize } from './artist'
const useStyles = makeStyles({
noWrap: {
whiteSpace: 'nowrap',
},
menu: {
color: (props) => props.color,
},
})
const MoreButton = ({ record, onClick, info, ...rest }) => {
const handleClick = record.missing
? (e) => {
e.preventDefault()
info.action(record)
e.stopPropagation()
}
: onClick
return (
<IconButton onClick={handleClick} size={'small'} {...rest}>
{record?.missing ? (
<MdQuestionMark fontSize={'large'} />
) : (
<MoreVertIcon fontSize={'small'} />
)}
</IconButton>
)
}
const ContextMenu = ({
resource,
showLove,
record,
color,
className,
songQueryParams,
hideShare,
hideInfo,
}) => {
const classes = useStyles({ color })
const dataProvider = useDataProvider()
const dispatch = useDispatch()
const translate = useTranslate()
const notify = useNotify()
const [anchorEl, setAnchorEl] = useState(null)
const isArtist = resource === 'artist'
const downloadSize = isArtist ? artistDownloadSize(record) : record?.size
const options = {
play: {
enabled: true,
needData: true,
label: translate('resources.album.actions.playAll'),
action: (data, ids) => dispatch(playTracks(data, ids)),
},
playNext: {
enabled: true,
needData: true,
label: translate('resources.album.actions.playNext'),
action: (data, ids) => dispatch(playNext(data, ids)),
},
addToQueue: {
enabled: true,
needData: true,
label: translate('resources.album.actions.addToQueue'),
action: (data, ids) => dispatch(addTracks(data, ids)),
},
shuffle: {
enabled: true,
needData: true,
label: translate('resources.album.actions.shuffle'),
action: (data, ids) => dispatch(shuffleTracks(data, ids)),
},
addToPlaylist: {
enabled: true,
needData: true,
label: translate('resources.album.actions.addToPlaylist'),
action: (data, ids) => dispatch(openAddToPlaylist({ selectedIds: ids })),
},
...(!hideShare && {
share: {
enabled: config.enableSharing && (!isArtist || downloadSize),
needData: false,
label: translate('ra.action.share'),
action: (record) =>
dispatch(openShareMenu([record.id], resource, record.name)),
},
}),
download: {
enabled: config.enableDownloads && downloadSize,
needData: false,
label: `${translate('ra.action.download')} (${formatBytes(downloadSize)})`,
action: () => {
dispatch(
openDownloadMenu(
record,
record.duration !== undefined
? DOWNLOAD_MENU_ALBUM
: DOWNLOAD_MENU_ARTIST,
),
)
},
},
...(!hideInfo && {
info: {
enabled: true,
needData: true,
label: translate('resources.album.actions.info'),
action: () => dispatch(openExtendedInfoDialog(record)),
},
}),
}
const handleClick = (e) => {
e.preventDefault()
setAnchorEl(e.currentTarget)
e.stopPropagation()
}
const handleOnClose = (e) => {
e.preventDefault()
setAnchorEl(null)
e.stopPropagation()
}
let extractSongsData = function (response) {
const data = response.data.reduce(
(acc, cur) => ({ ...acc, [cur.id]: cur }),
{},
)
const ids = response.data.map((r) => r.id)
return { data, ids }
}
const handleItemClick = (e) => {
setAnchorEl(null)
const key = e.target.getAttribute('value')
if (options[key].needData) {
dataProvider
.getList('song', songQueryParams)
.then((response) => {
let { data, ids } = extractSongsData(response)
options[key].action(data, ids)
})
.catch(() => {
notify('ra.page.error', 'warning')
})
} else {
options[key].action(record)
}
e.stopPropagation()
}
const open = Boolean(anchorEl)
if (!record) {
return null
}
const present = !record.missing
return (
<span className={clsx(classes.noWrap, className)}>
<LoveButton
record={record}
resource={resource}
visible={config.enableFavourites && showLove && present}
color={color}
/>
<MoreButton
record={record}
onClick={handleClick}
info={options.info}
aria-label="more"
aria-controls="context-menu"
aria-haspopup="true"
className={classes.menu}
/>
<Menu
id="context-menu"
anchorEl={anchorEl}
keepMounted
open={open}
onClose={handleOnClose}
>
{Object.keys(options).map(
(key) =>
options[key].enabled && (
<MenuItem value={key} key={key} onClick={handleItemClick}>
{options[key].label}
</MenuItem>
),
)}
</Menu>
</span>
)
}
export const AlbumContextMenu = (props) =>
props.record ? (
<ContextMenu
{...props}
resource={'album'}
songQueryParams={{
pagination: { page: 1, perPage: -1 },
sort: { field: 'album', order: 'ASC' },
filter: {
album_id: props.record.id,
disc_number: props.discNumber,
missing: false,
},
}}
/>
) : null
AlbumContextMenu.propTypes = {
record: PropTypes.object,
discNumber: PropTypes.number,
color: PropTypes.string,
showLove: PropTypes.bool,
}
AlbumContextMenu.defaultProps = {
showLove: true,
addLabel: true,
}
export const ArtistContextMenu = (props) =>
props.record ? (
<ContextMenu
{...props}
hideInfo={true}
resource={'artist'}
songQueryParams={{
pagination: { page: 1, perPage: 200 },
sort: {
field: 'album',
order: 'ASC',
},
filter: { album_artist_id: props.record.id, missing: false },
}}
/>
) : null
ArtistContextMenu.propTypes = {
record: PropTypes.object,
color: PropTypes.string,
showLove: PropTypes.bool,
}
ArtistContextMenu.defaultProps = {
showLove: true,
addLabel: true,
}