quintodrome/ui/src/album/AlbumList.jsx
Deluan Quintão d021289279
fix: enable multi-valued releasetype in smart playlists (#4621)
* fix: prevent infinite loop in Type filter autocomplete

Fixed an infinite loop issue in the album Type filter caused by an inline
arrow function in the optionText prop. The inline function created a new
reference on every render, causing React-Admin's AutocompleteInput to
continuously re-fetch data from the /api/tag endpoint.

The solution extracts the formatting function outside the component scope
as formatReleaseType, ensuring a stable function reference across renders.
This prevents unnecessary re-renders and API calls while maintaining the
humanized display format for release type values.

* fix: enable multi-valued releasetype in smart playlists

Smart playlists can now match all values in multi-valued releasetype tags.
Previously, the albumtype field was mapped to the single-valued mbz_album_type
database field, which only stored the first value from tags like album; soundtrack.
This prevented smart playlists from matching albums with secondary release types
like soundtrack, live, or compilation when tagged by MusicBrainz Picard.

The fix removes the direct database field mapping and allows both albumtype and
releasetype to use the multi-valued tag system. The albumtype field is now an
alias that points to the releasetype tag field, ensuring both query the same
JSON path in the tags column. This maintains backward compatibility with the
documented albumtype field while enabling proper multi-value tag matching.

Added tests to verify both releasetype and albumtype correctly generate
multi-valued tag queries.

Fixes #4616

* fix: resolve albumtype alias for all operators and sorting

Codex correctly identified that the initial fix only worked for Contains/StartsWith/EndsWith operators. The alias resolution was happening too late in the code path.

Fixed by resolving the alias in two places:
1. tagCond.ToSql() - now uses the actual field name (releasetype) in the JSON path
2. Criteria.OrderBy() - now uses the actual field name when building sort expressions

Added tests for Is/IsNot operators and sorting to ensure complete coverage.
2025-10-26 19:36:44 -04:00

253 lines
7.2 KiB
JavaScript

import { useSelector } from 'react-redux'
import { Redirect, useLocation } from 'react-router-dom'
import {
AutocompleteArrayInput,
AutocompleteInput,
Filter,
NullableBooleanInput,
NumberInput,
Pagination,
ReferenceArrayInput,
ReferenceInput,
SearchInput,
usePermissions,
useRefresh,
useTranslate,
useVersion,
} from 'react-admin'
import FavoriteIcon from '@material-ui/icons/Favorite'
import { withWidth } from '@material-ui/core'
import {
List,
QuickFilter,
Title,
useAlbumsPerPage,
useResourceRefresh,
useSetToggleableFields,
} from '../common'
import AlbumListActions from './AlbumListActions'
import AlbumTableView from './AlbumTableView'
import AlbumGridView from './AlbumGridView'
import albumLists, { defaultAlbumList } from './albumLists'
import config from '../config'
import AlbumInfo from './AlbumInfo'
import ExpandInfoDialog from '../dialogs/ExpandInfoDialog'
import { humanize } from 'inflection'
import { makeStyles } from '@material-ui/core/styles'
const useStyles = makeStyles({
chip: {
margin: 0,
height: '24px',
},
})
const formatReleaseType = (record) =>
record?.tagValue ? humanize(record?.tagValue) : '-- None --'
const AlbumFilter = (props) => {
const classes = useStyles()
const translate = useTranslate()
const { permissions } = usePermissions()
const isAdmin = permissions === 'admin'
return (
<Filter {...props} variant={'outlined'}>
<SearchInput id="search" source="name" alwaysOn />
<ReferenceInput
label={translate('resources.album.fields.artist')}
source="artist_id"
reference="artist"
sort={{ field: 'name', order: 'ASC' }}
filterToQuery={(searchText) => ({ name: [searchText] })}
>
<AutocompleteInput emptyText="-- None --" />
</ReferenceInput>
<ReferenceArrayInput
label={translate('resources.album.fields.genre')}
source="genre_id"
reference="genre"
perPage={0}
sort={{ field: 'name', order: 'ASC' }}
filterToQuery={(searchText) => ({ name: [searchText] })}
>
<AutocompleteArrayInput emptyText="-- None --" classes={classes} />
</ReferenceArrayInput>
<ReferenceInput
label={translate('resources.album.fields.recordLabel')}
source="recordlabel"
reference="tag"
perPage={0}
sort={{ field: 'tagValue', order: 'ASC' }}
filter={{ tag_name: 'recordlabel' }}
filterToQuery={(searchText) => ({
tag_value: [searchText],
})}
>
<AutocompleteInput emptyText="-- None --" optionText="tagValue" />
</ReferenceInput>
<ReferenceArrayInput
label={translate('resources.album.fields.grouping')}
source="grouping"
reference="tag"
perPage={0}
sort={{ field: 'tagValue', order: 'ASC' }}
filter={{ tag_name: 'grouping' }}
filterToQuery={(searchText) => ({
tag_value: [searchText],
})}
>
<AutocompleteArrayInput
emptyText="-- None --"
classes={classes}
optionText="tagValue"
/>
</ReferenceArrayInput>
<ReferenceArrayInput
label={translate('resources.album.fields.mood')}
source="mood"
reference="tag"
perPage={0}
sort={{ field: 'tagValue', order: 'ASC' }}
filter={{ tag_name: 'mood' }}
filterToQuery={(searchText) => ({
tag_value: [searchText],
})}
>
<AutocompleteArrayInput
emptyText="-- None --"
classes={classes}
optionText="tagValue"
/>
</ReferenceArrayInput>
<ReferenceInput
label={translate('resources.album.fields.media')}
source="media"
reference="tag"
perPage={0}
sort={{ field: 'tagValue', order: 'ASC' }}
filter={{ tag_name: 'media' }}
filterToQuery={(searchText) => ({
tag_value: [searchText],
})}
>
<AutocompleteInput emptyText="-- None --" optionText="tagValue" />
</ReferenceInput>
<ReferenceInput
label={translate('resources.album.fields.releaseType')}
source="releasetype"
reference="tag"
perPage={0}
sort={{ field: 'tagValue', order: 'ASC' }}
filter={{ tag_name: 'releasetype' }}
filterToQuery={(searchText) => ({
tag_value: [searchText],
})}
>
<AutocompleteInput
emptyText="-- None --"
optionText={formatReleaseType}
/>
</ReferenceInput>
<NullableBooleanInput source="compilation" />
<NumberInput source="year" />
{config.enableFavourites && (
<QuickFilter
source="starred"
label={<FavoriteIcon fontSize={'small'} />}
defaultValue={true}
/>
)}
{isAdmin && <NullableBooleanInput source="missing" />}
</Filter>
)
}
const AlbumListTitle = ({ albumListType }) => {
const translate = useTranslate()
let title = translate('resources.album.name', { smart_count: 2 })
if (albumListType) {
let listTitle = translate(`resources.album.lists.${albumListType}`, {
smart_count: 2,
})
title = `${title} - ${listTitle}`
}
return <Title subTitle={title} args={{ smart_count: 2 }} />
}
const randomStartingSeed = Math.random().toString()
const AlbumList = (props) => {
const { width } = props
const albumView = useSelector((state) => state.albumView)
const [perPage, perPageOptions] = useAlbumsPerPage(width)
const location = useLocation()
const version = useVersion()
const refresh = useRefresh()
useResourceRefresh('album')
const seed = `${randomStartingSeed}-${version}`
const albumListType = location.pathname
.replace(/^\/album/, '')
.replace(/^\//, '')
// Workaround to force album columns to appear the first time.
// See https://github.com/navidrome/navidrome/pull/923#issuecomment-833004842
// TODO: Find a better solution
useSetToggleableFields(
'album',
[
'artist',
'songCount',
'playCount',
'year',
'mood',
'duration',
'rating',
'size',
'createdAt',
],
['createdAt', 'size'],
)
// If it does not have filter/sort params (usually coming from Menu),
// reload with correct filter/sort params
if (!location.search) {
const type =
albumListType || localStorage.getItem('defaultView') || defaultAlbumList
const listParams = albumLists[type]
if (type === 'random') {
refresh()
}
if (listParams) {
return <Redirect to={`/album/${type}?${listParams.params}`} />
}
}
return (
<>
<List
{...props}
exporter={false}
bulkActionButtons={false}
filter={{ seed }}
actions={<AlbumListActions />}
filters={<AlbumFilter />}
perPage={perPage}
pagination={<Pagination rowsPerPageOptions={perPageOptions} />}
title={<AlbumListTitle albumListType={albumListType} />}
>
{albumView.grid ? (
<AlbumGridView albumListType={albumListType} {...props} />
) : (
<AlbumTableView {...props} />
)}
</List>
<ExpandInfoDialog content={<AlbumInfo />} />
</>
)
}
const AlbumListWithWidth = withWidth()(AlbumList)
export default AlbumListWithWidth