Refactoring to make common components usage more uniform

This commit is contained in:
Deluan 2021-05-26 09:35:13 -04:00
parent 0941fbc0cd
commit 60d50de8c9
17 changed files with 79 additions and 54 deletions

View file

@ -16,7 +16,7 @@ import subsonic from '../subsonic'
import { formatBytes } from '../utils' import { formatBytes } from '../utils'
import { useMediaQuery, makeStyles } from '@material-ui/core' import { useMediaQuery, makeStyles } from '@material-ui/core'
import config from '../config' import config from '../config'
import ToggleFieldsMenu from '../common/ToggleFieldsMenu' import { ToggleFieldsMenu } from '../common'
const useStyles = makeStyles({ const useStyles = makeStyles({
toolbar: { display: 'flex', justifyContent: 'space-between', width: '100%' }, toolbar: { display: 'flex', justifyContent: 'space-between', width: '100%' },

View file

@ -1,5 +1,5 @@
import React, { useEffect } from 'react' import React from 'react'
import { useSelector, useDispatch } from 'react-redux' import { useSelector } from 'react-redux'
import { Redirect, useLocation } from 'react-router-dom' import { Redirect, useLocation } from 'react-router-dom'
import { import {
AutocompleteInput, AutocompleteInput,
@ -13,14 +13,19 @@ import {
} from 'react-admin' } from 'react-admin'
import FavoriteIcon from '@material-ui/icons/Favorite' import FavoriteIcon from '@material-ui/icons/Favorite'
import { withWidth } from '@material-ui/core' import { withWidth } from '@material-ui/core'
import { List, QuickFilter, Title, useAlbumsPerPage } from '../common' import {
List,
QuickFilter,
Title,
useAlbumsPerPage,
useSetToggleableFields,
} from '../common'
import AlbumListActions from './AlbumListActions' import AlbumListActions from './AlbumListActions'
import AlbumTableView from './AlbumTableView' import AlbumTableView from './AlbumTableView'
import AlbumGridView from './AlbumGridView' import AlbumGridView from './AlbumGridView'
import { AddToPlaylistDialog } from '../dialogs' import { AddToPlaylistDialog } from '../dialogs'
import albumLists, { defaultAlbumList } from './albumLists' import albumLists, { defaultAlbumList } from './albumLists'
import config from '../config' import config from '../config'
import { setToggleableFields, setOmittedFields } from '../actions'
const AlbumFilter = (props) => { const AlbumFilter = (props) => {
const translate = useTranslate() const translate = useTranslate()
@ -66,10 +71,6 @@ const AlbumList = (props) => {
const albumView = useSelector((state) => state.albumView) const albumView = useSelector((state) => state.albumView)
const [perPage, perPageOptions] = useAlbumsPerPage(width) const [perPage, perPageOptions] = useAlbumsPerPage(width)
const location = useLocation() const location = useLocation()
const toggleableAlbumFields = useSelector(
(state) => state.settings.toggleableFields
)?.album
const dispatch = useDispatch()
const albumListType = location.pathname const albumListType = location.pathname
.replace(/^\/album/, '') .replace(/^\/album/, '')
@ -78,24 +79,14 @@ const AlbumList = (props) => {
// Workaround to force album columns to appear the first time. // Workaround to force album columns to appear the first time.
// See https://github.com/navidrome/navidrome/pull/923#issuecomment-833004842 // See https://github.com/navidrome/navidrome/pull/923#issuecomment-833004842
// TODO: Find a better solution // TODO: Find a better solution
useSetToggleableFields('album', [
useEffect(() => { 'artist',
if (!toggleableAlbumFields) { 'songCount',
dispatch( 'playCount',
setToggleableFields({ 'year',
album: { 'duration',
artist: true, 'rating',
songCount: true, ])
playCount: true,
year: true,
duration: true,
rating: true,
},
})
)
dispatch(setOmittedFields({ album: [] }))
}
}, [dispatch, toggleableAlbumFields])
// If it does not have filter/sort params (usually coming from Menu), // If it does not have filter/sort params (usually coming from Menu),
// reload with correct filter/sort params // reload with correct filter/sort params

View file

@ -15,7 +15,7 @@ import ViewHeadlineIcon from '@material-ui/icons/ViewHeadline'
import ViewModuleIcon from '@material-ui/icons/ViewModule' import ViewModuleIcon from '@material-ui/icons/ViewModule'
import { useDispatch, useSelector } from 'react-redux' import { useDispatch, useSelector } from 'react-redux'
import { albumViewGrid, albumViewTable } from '../actions' import { albumViewGrid, albumViewTable } from '../actions'
import ToggleFieldsMenu from '../common/ToggleFieldsMenu' import { ToggleFieldsMenu } from '../common'
const useStyles = makeStyles({ const useStyles = makeStyles({
title: { margin: '1rem' }, title: { margin: '1rem' },

View file

@ -21,10 +21,10 @@ import {
SongDetails, SongDetails,
SongTitleField, SongTitleField,
RatingField, RatingField,
QualityInfo,
useSelectedFields,
} from '../common' } from '../common'
import { AddToPlaylistDialog } from '../dialogs' import { AddToPlaylistDialog } from '../dialogs'
import { QualityInfo } from '../common/QualityInfo'
import useSelectedFields from '../common/useSelectedFields'
import config from '../config' import config from '../config'
const useStyles = makeStyles( const useStyles = makeStyles(

View file

@ -26,9 +26,9 @@ import {
MultiLineTextField, MultiLineTextField,
AlbumContextMenu, AlbumContextMenu,
RatingField, RatingField,
useSelectedFields,
} from '../common' } from '../common'
import config from '../config' import config from '../config'
import useSelectedFields from '../common/useSelectedFields'
const useStyles = makeStyles({ const useStyles = makeStyles({
columnIcon: { columnIcon: {

View file

@ -10,6 +10,7 @@ import {
import { useMediaQuery, withWidth } from '@material-ui/core' import { useMediaQuery, withWidth } from '@material-ui/core'
import FavoriteIcon from '@material-ui/icons/Favorite' import FavoriteIcon from '@material-ui/icons/Favorite'
import FavoriteBorderIcon from '@material-ui/icons/FavoriteBorder' import FavoriteBorderIcon from '@material-ui/icons/FavoriteBorder'
import { makeStyles } from '@material-ui/core/styles'
import { AddToPlaylistDialog } from '../dialogs' import { AddToPlaylistDialog } from '../dialogs'
import { import {
ArtistContextMenu, ArtistContextMenu,
@ -18,11 +19,10 @@ import {
useGetHandleArtistClick, useGetHandleArtistClick,
ArtistSimpleList, ArtistSimpleList,
RatingField, RatingField,
useSelectedFields,
} from '../common' } from '../common'
import { makeStyles } from '@material-ui/core/styles'
import config from '../config' import config from '../config'
import ArtistListActions from './ArtistListActions' import ArtistListActions from './ArtistListActions'
import useSelectedFields from '../common/useSelectedFields'
const useStyles = makeStyles({ const useStyles = makeStyles({
contextHeader: { contextHeader: {

View file

@ -1,7 +1,7 @@
import React from 'react' import React from 'react'
import { sanitizeListRestProps, TopToolbar } from 'react-admin' import { sanitizeListRestProps, TopToolbar } from 'react-admin'
import { useMediaQuery } from '@material-ui/core' import { useMediaQuery } from '@material-ui/core'
import ToggleFieldsMenu from '../common/ToggleFieldsMenu' import { ToggleFieldsMenu } from '../common'
const ArtistListActions = ({ className, ...rest }) => { const ArtistListActions = ({ className, ...rest }) => {
const isNotSmall = useMediaQuery((theme) => theme.breakpoints.up('sm')) const isNotSmall = useMediaQuery((theme) => theme.breakpoints.up('sm'))

View file

@ -26,7 +26,7 @@ import PlayerToolbar from './PlayerToolbar'
import { sendNotification } from '../utils' import { sendNotification } from '../utils'
import { keyMap } from '../hotkeys' import { keyMap } from '../hotkeys'
import useCurrentTheme from '../themes/useCurrentTheme' import useCurrentTheme from '../themes/useCurrentTheme'
import { QualityInfo } from '../common/QualityInfo' import { QualityInfo } from '../common'
const useStyle = makeStyles( const useStyle = makeStyles(
(theme) => ({ (theme) => ({

View file

@ -27,7 +27,10 @@ const useStyles = makeStyles({
}, },
}) })
const ToggleFieldsMenu = ({ resource, topbarComponent: TopBarComponent }) => { export const ToggleFieldsMenu = ({
resource,
topbarComponent: TopBarComponent,
}) => {
const [anchorEl, setAnchorEl] = useState(null) const [anchorEl, setAnchorEl] = useState(null)
const dispatch = useDispatch() const dispatch = useDispatch()
const translate = useTranslate() const translate = useTranslate()
@ -101,8 +104,6 @@ const ToggleFieldsMenu = ({ resource, topbarComponent: TopBarComponent }) => {
) )
} }
export default ToggleFieldsMenu
ToggleFieldsMenu.propTypes = { ToggleFieldsMenu.propTypes = {
resource: PropTypes.string.isRequired, resource: PropTypes.string.isRequired,
topbarComponent: PropTypes.elementType, topbarComponent: PropTypes.elementType,

View file

@ -30,3 +30,6 @@ export * from './SongSimpleList'
export * from './ArtistSimpleList' export * from './ArtistSimpleList'
export * from './RatingField' export * from './RatingField'
export * from './useRating' export * from './useRating'
export * from './useSelectedFields'
export * from './ToggleFieldsMenu'
export * from './QualityInfo'

View file

@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
import { useDispatch, useSelector } from 'react-redux' import { useDispatch, useSelector } from 'react-redux'
import { setOmittedFields, setToggleableFields } from '../actions' import { setOmittedFields, setToggleableFields } from '../actions'
const useSelectedFields = ({ export const useSelectedFields = ({
resource, resource,
columns, columns,
omittedColumns = [], omittedColumns = [],
@ -69,11 +69,39 @@ const useSelectedFields = ({
return React.Children.toArray(filteredComponents) return React.Children.toArray(filteredComponents)
} }
export default useSelectedFields
useSelectedFields.propTypes = { useSelectedFields.propTypes = {
resource: PropTypes.string, resource: PropTypes.string,
columns: PropTypes.object, columns: PropTypes.object,
omittedColumns: PropTypes.arrayOf(PropTypes.string), omittedColumns: PropTypes.arrayOf(PropTypes.string),
defaultOff: PropTypes.arrayOf(PropTypes.string), defaultOff: PropTypes.arrayOf(PropTypes.string),
} }
export const useSetToggleableFields = (
resource,
toggleableColumns,
defaultOff = []
) => {
const current = useSelector((state) => state.settings.toggleableFields)?.album
const dispatch = useDispatch()
useEffect(() => {
if (!current) {
dispatch(
setToggleableFields({
[resource]: toggleableColumns.reduce((acc, cur) => {
return {
...acc,
...{ [cur]: true },
}
}, {}),
})
)
dispatch(setOmittedFields({ [resource]: defaultOff }))
}
}, [resource, toggleableColumns, dispatch, current, defaultOff])
}
useSetToggleableFields.propTypes = {
resource: PropTypes.string,
toggleableColumns: PropTypes.arrayOf(PropTypes.string),
defaultOff: PropTypes.arrayOf(PropTypes.string),
}

View file

@ -21,7 +21,7 @@ import PropTypes from 'prop-types'
import { formatBytes } from '../utils' import { formatBytes } from '../utils'
import { useMediaQuery, makeStyles } from '@material-ui/core' import { useMediaQuery, makeStyles } from '@material-ui/core'
import config from '../config' import config from '../config'
import ToggleFieldsMenu from '../common/ToggleFieldsMenu' import { ToggleFieldsMenu } from '../common'
const useStyles = makeStyles({ const useStyles = makeStyles({
toolbar: { display: 'flex', justifyContent: 'space-between', width: '100%' }, toolbar: { display: 'flex', justifyContent: 'space-between', width: '100%' },

View file

@ -12,8 +12,13 @@ import {
} from 'react-admin' } from 'react-admin'
import Switch from '@material-ui/core/Switch' import Switch from '@material-ui/core/Switch'
import { useMediaQuery } from '@material-ui/core' import { useMediaQuery } from '@material-ui/core'
import { DurationField, List, Writable, isWritable } from '../common' import {
import useSelectedFields from '../common/useSelectedFields' DurationField,
List,
Writable,
isWritable,
useSelectedFields,
} from '../common'
import PlaylistListActions from './PlaylistListActions' import PlaylistListActions from './PlaylistListActions'
const PlaylistFilter = (props) => ( const PlaylistFilter = (props) => (

View file

@ -6,7 +6,7 @@ import {
useTranslate, useTranslate,
} from 'react-admin' } from 'react-admin'
import { useMediaQuery } from '@material-ui/core' import { useMediaQuery } from '@material-ui/core'
import ToggleFieldsMenu from '../common/ToggleFieldsMenu' import { ToggleFieldsMenu } from '../common'
const PlaylistListActions = ({ className, ...rest }) => { const PlaylistListActions = ({ className, ...rest }) => {
const isNotSmall = useMediaQuery((theme) => theme.breakpoints.up('sm')) const isNotSmall = useMediaQuery((theme) => theme.breakpoints.up('sm'))

View file

@ -22,13 +22,13 @@ import {
SongContextMenu, SongContextMenu,
SongDatagrid, SongDatagrid,
SongTitleField, SongTitleField,
QualityInfo,
useSelectedFields,
} from '../common' } from '../common'
import { AddToPlaylistDialog } from '../dialogs' import { AddToPlaylistDialog } from '../dialogs'
import { AlbumLinkField } from '../song/AlbumLinkField' import { AlbumLinkField } from '../song/AlbumLinkField'
import { playTracks } from '../actions' import { playTracks } from '../actions'
import PlaylistSongBulkActions from './PlaylistSongBulkActions' import PlaylistSongBulkActions from './PlaylistSongBulkActions'
import { QualityInfo } from '../common/QualityInfo'
import useSelectedFields from '../common/useSelectedFields'
const useStyles = makeStyles( const useStyles = makeStyles(
(theme) => ({ (theme) => ({

View file

@ -20,16 +20,14 @@ import {
RatingField, RatingField,
} from '../common' } from '../common'
import { useDispatch } from 'react-redux' import { useDispatch } from 'react-redux'
import { makeStyles } from '@material-ui/core/styles'
import FavoriteBorderIcon from '@material-ui/icons/FavoriteBorder'
import { setTrack } from '../actions' import { setTrack } from '../actions'
import { SongBulkActions } from '../common'
import { SongListActions } from './SongListActions' import { SongListActions } from './SongListActions'
import { AlbumLinkField } from './AlbumLinkField' import { AlbumLinkField } from './AlbumLinkField'
import { AddToPlaylistDialog } from '../dialogs' import { AddToPlaylistDialog } from '../dialogs'
import { makeStyles } from '@material-ui/core/styles' import { SongBulkActions, QualityInfo, useSelectedFields } from '../common'
import FavoriteBorderIcon from '@material-ui/icons/FavoriteBorder'
import config from '../config' import config from '../config'
import useSelectedFields from '../common/useSelectedFields'
import { QualityInfo } from '../common/QualityInfo'
const useStyles = makeStyles({ const useStyles = makeStyles({
contextHeader: { contextHeader: {

View file

@ -1,8 +1,7 @@
import React, { cloneElement } from 'react' import React, { cloneElement } from 'react'
import { sanitizeListRestProps, TopToolbar } from 'react-admin' import { sanitizeListRestProps, TopToolbar } from 'react-admin'
import { useMediaQuery } from '@material-ui/core' import { useMediaQuery } from '@material-ui/core'
import { ShuffleAllButton } from '../common' import { ShuffleAllButton, ToggleFieldsMenu } from '../common'
import ToggleFieldsMenu from '../common/ToggleFieldsMenu'
export const SongListActions = ({ export const SongListActions = ({
currentSort, currentSort,