navidrome/ui/src/common/ToggleFieldsMenu.jsx
Deluan Quintão fcdd30ba8f
build(ui): migrate from CRA/Jest to Vite/Vitest (#3311)
* feat: create vite project

* feat: it's alive!

* feat: `make dev` working!

* feat: replace custom serviceWorker with vite plugin

* test: replace Jest with Vitest

* fix: run prettier

* fix: skip eslint for now.

* chore: remove ui.old folder

* refactor: replace lodash.pick with simple destructuring

* fix: eslint errors (wip)

* fix: eslint errors (wip)

* fix: display-name eslint errors (wip)

* fix: no-console eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: build

* fix: pwa manifest

* refactor: pwa manifest

* refactor: simplify PORT configuration

* refactor: rename simple JS files

* test: cover playlistUtils

* fix: react-image-lightbox

* feat(ui): add sourcemaps to help debug issues
2024-09-28 11:54:36 -04:00

112 lines
2.9 KiB
JavaScript

import React, { useState } from 'react'
import PropTypes from 'prop-types'
import IconButton from '@material-ui/core/IconButton'
import Menu from '@material-ui/core/Menu'
import MenuItem from '@material-ui/core/MenuItem'
import { makeStyles, Typography } from '@material-ui/core'
import MoreVertIcon from '@material-ui/icons/MoreVert'
import Checkbox from '@material-ui/core/Checkbox'
import { useDispatch, useSelector } from 'react-redux'
import { useTranslate } from 'react-admin'
import { setToggleableFields } from '../actions'
const useStyles = makeStyles({
menuIcon: {
position: 'relative',
top: '-0.5em',
},
menu: {
width: '24ch',
},
columns: {
maxHeight: '21rem',
overflow: 'auto',
},
title: {
margin: '1rem',
},
})
export const ToggleFieldsMenu = ({
resource,
topbarComponent: TopBarComponent,
hideColumns,
}) => {
const [anchorEl, setAnchorEl] = useState(null)
const dispatch = useDispatch()
const translate = useTranslate()
const toggleableColumns = useSelector(
(state) => state.settings.toggleableFields[resource],
)
const omittedColumns =
useSelector((state) => state.settings.omittedFields[resource]) || []
const classes = useStyles()
const open = Boolean(anchorEl)
const handleOpen = (event) => {
setAnchorEl(event.currentTarget)
}
const handleClose = () => {
setAnchorEl(null)
}
const handleClick = (selectedColumn) => {
dispatch(
setToggleableFields({
[resource]: {
...toggleableColumns,
[selectedColumn]: !toggleableColumns[selectedColumn],
},
}),
)
}
return (
<div className={classes.menuIcon}>
<IconButton
aria-label="more"
aria-controls="long-menu"
aria-haspopup="true"
onClick={handleOpen}
>
<MoreVertIcon />
</IconButton>
<Menu
id="long-menu"
anchorEl={anchorEl}
keepMounted
open={open}
onClose={handleClose}
classes={{
paper: classes.menu,
}}
>
{TopBarComponent && <TopBarComponent />}
{!hideColumns && toggleableColumns ? (
<div>
<Typography className={classes.title}>
{translate('ra.toggleFieldsMenu.columnsToDisplay')}
</Typography>
<div className={classes.columns}>
{Object.entries(toggleableColumns).map(([key, val]) =>
!omittedColumns.includes(key) ? (
<MenuItem key={key} onClick={() => handleClick(key)}>
<Checkbox checked={val} />
{translate(`resources.${resource}.fields.${key}`)}
</MenuItem>
) : null,
)}
</div>
</div>
) : null}
</Menu>
</div>
)
}
ToggleFieldsMenu.propTypes = {
resource: PropTypes.string.isRequired,
topbarComponent: PropTypes.elementType,
hideColumns: PropTypes.bool,
}