quintodrome/ui/src/personal/Personal.js

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

202 lines
5 KiB
JavaScript
Raw Normal View History

import React from 'react'
2020-03-31 13:02:22 -08:00
import { useDispatch, useSelector } from 'react-redux'
import {
Card,
FormControl,
FormHelperText,
FormControlLabel,
Switch,
} from '@material-ui/core'
import {
SelectInput,
2020-05-01 14:29:50 -08:00
SimpleForm,
Title,
useLocale,
useNotify,
2020-05-01 14:29:50 -08:00
useSetLocale,
useTranslate,
} from 'react-admin'
import { makeStyles } from '@material-ui/core/styles'
import HelpOutlineIcon from '@material-ui/icons/HelpOutline'
2020-11-19 19:22:18 -09:00
import { changeTheme, setNotificationsState } from '../actions'
2020-03-31 13:02:22 -08:00
import themes from '../themes'
import { docsUrl } from '../utils'
2020-05-02 13:44:24 -08:00
import { useGetLanguageChoices } from '../i18n'
import albumLists, { defaultAlbumList } from '../album/albumLists'
const useStyles = makeStyles({
root: { marginTop: '1em' },
})
const helpKey = '_help'
function openInNewTab(url) {
const win = window.open(url, '_blank')
win.focus()
}
const HelpMsg = ({ caption }) => (
<>
<HelpOutlineIcon />
&nbsp;&nbsp; {caption}
</>
)
const SelectLanguage = (props) => {
const translate = useTranslate()
const setLocale = useSetLocale()
2020-05-01 14:29:50 -08:00
const locale = useLocale()
2020-05-02 07:21:08 -08:00
const { choices } = useGetLanguageChoices()
2020-05-01 14:29:50 -08:00
2020-05-02 07:21:08 -08:00
choices.push({
id: helpKey,
name: <HelpMsg caption={'Help to translate'} />,
})
2020-05-01 14:29:50 -08:00
return (
<SelectInput
{...props}
2020-05-01 14:29:50 -08:00
source="language"
label={translate('menu.personal.options.language')}
defaultValue={locale}
2020-05-02 07:21:08 -08:00
choices={choices}
2020-05-01 14:29:50 -08:00
translateChoice={false}
onChange={(event) => {
if (event.target.value === helpKey) {
openInNewTab(docsUrl('/docs/developers/translations/'))
return
}
setLocale(event.target.value)
localStorage.setItem('locale', event.target.value)
}}
/>
)
}
const SelectTheme = (props) => {
const translate = useTranslate()
const dispatch = useDispatch()
const currentTheme = useSelector((state) => state.theme)
const themeChoices = Object.keys(themes).map((key) => {
return { id: key, name: themes[key].themeName }
})
themeChoices.push({
id: helpKey,
name: <HelpMsg caption={'Create your own'} />,
})
return (
<SelectInput
{...props}
source="theme"
label={translate('menu.personal.options.theme')}
defaultValue={currentTheme}
2020-05-01 14:29:50 -08:00
translateChoice={false}
choices={themeChoices}
onChange={(event) => {
if (event.target.value === helpKey) {
openInNewTab(docsUrl('/docs/developers/creating-themes/'))
return
}
dispatch(changeTheme(event.target.value))
}}
/>
)
}
const SelectDefaultView = (props) => {
const translate = useTranslate()
const current = localStorage.getItem('defaultView') || defaultAlbumList
const choices = Object.keys(albumLists).map((type) => ({
id: type,
name: translate(`resources.album.lists.${type}`),
}))
return (
<SelectInput
{...props}
source="defaultView"
label={translate('menu.personal.options.defaultView')}
defaultValue={current}
choices={choices}
translateChoice={false}
onChange={(event) => {
localStorage.setItem('defaultView', event.target.value)
}}
/>
)
}
const NotificationsToggle = () => {
2020-11-19 19:06:09 -09:00
const translate = useTranslate()
const dispatch = useDispatch()
const notify = useNotify()
const currentSetting = useSelector((state) => state.settings.notifications)
const notAvailable = !('Notification' in window) || !window.isSecureContext
if (
(currentSetting && Notification.permission !== 'granted') ||
notAvailable
) {
dispatch(setNotificationsState(false))
}
const toggleNotifications = (event) => {
if (currentSetting && !event.target.checked) {
dispatch(setNotificationsState(false))
} else {
if (Notification.permission === 'denied') {
notify(translate('message.notifications_blocked'), 'warning')
} else {
Notification.requestPermission().then((permission) => {
dispatch(setNotificationsState(permission === 'granted'))
})
}
2020-11-19 19:06:09 -09:00
}
}
2020-11-19 19:06:09 -09:00
return (
<FormControl>
<FormControlLabel
control={
<Switch
id={'notifications'}
color="primary"
checked={currentSetting}
disabled={notAvailable}
onChange={toggleNotifications}
/>
2020-11-19 19:06:09 -09:00
}
label={
<span>
{translate('menu.personal.options.desktop_notifications')}
</span>
}
/>
{notAvailable && (
<FormHelperText id="notifications-disabled-helper-text">
{translate('message.notifications_not_available')}
</FormHelperText>
)}
</FormControl>
2020-11-19 19:06:09 -09:00
)
}
const Personal = () => {
const translate = useTranslate()
const classes = useStyles()
2020-03-31 13:02:22 -08:00
return (
<Card className={classes.root}>
<Title title={'Navidrome - ' + translate('menu.personal.name')} />
<SimpleForm toolbar={null} variant={'outlined'}>
<SelectTheme />
<SelectLanguage />
<SelectDefaultView />
2020-11-19 19:06:09 -09:00
<NotificationsToggle />
</SimpleForm>
</Card>
)
}
export default Personal