Refactor Settings component

This commit is contained in:
Thomas Nordquist
2019-04-10 19:47:20 +02:00
parent 14f0f32560
commit a2fae27919
2 changed files with 70 additions and 52 deletions

View File

@@ -0,0 +1,49 @@
import * as React from 'react'
import { InputLabel, Switch, Tooltip, Theme } from '@material-ui/core'
import { withStyles } from '@material-ui/styles'
const sha1 = require('sha1')
function BooleanSwitch(props: {title: string, value: boolean, tooltip: string, action: () => void, classes: any}) {
const { tooltip, value, action, title, classes } = props
const clickHandler = (e: React.MouseEvent) => {
e.stopPropagation()
e.preventDefault()
action()
}
return (
<div style={{ padding: '8px', display: 'flex' }}>
<Tooltip title={tooltip}>
<InputLabel
htmlFor={`toggle-${sha1(title)}`}
onClick={clickHandler}
className={classes.label}
>
{title}
</InputLabel>
</Tooltip>
<Tooltip title={tooltip}>
<Switch
name={`toggle-${sha1(title)}`}
checked={value}
onChange={action}
color="primary"
classes={{ switchBase: classes.switchBase }}
/>
</Tooltip>
</div>
)
}
const styles = (theme: Theme) => ({
switchBase: {
height: theme.spacing(4),
},
label: {
flex: '1',
paddingTop: '8px',
},
})
export default withStyles(styles)(BooleanSwitch)

View File

@@ -1,10 +1,11 @@
import * as React from 'react' import * as React from 'react'
import BooleanSwitch from './BooleanSwitch'
import BrokerStatistics from './BrokerStatistics' import BrokerStatistics from './BrokerStatistics'
import ChevronRight from '@material-ui/icons/ChevronRight' import ChevronRight from '@material-ui/icons/ChevronRight'
import { AppState } from '../../reducers' import { AppState } from '../../reducers'
import { bindActionCreators } from 'redux' import { bindActionCreators } from 'redux'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { settingsActions } from '../../actions' import { globalActions, settingsActions } from '../../actions'
import { shell } from 'electron' import { shell } from 'electron'
import { StyleRulesCallback, withStyles } from '@material-ui/core/styles' import { StyleRulesCallback, withStyles } from '@material-ui/core/styles'
import { TopicOrder } from '../../reducers/Settings' import { TopicOrder } from '../../reducers/Settings'
@@ -18,10 +19,8 @@ import {
MenuItem, MenuItem,
Select, Select,
Typography, Typography,
Switch,
Tooltip, Tooltip,
} from '@material-ui/core' } from '@material-ui/core'
const sha1 = require('sha1')
export const autoExpandLimitSet = [{ export const autoExpandLimitSet = [{
limit: 0, limit: 0,
@@ -33,7 +32,7 @@ export const autoExpandLimitSet = [{
limit: 3, limit: 3,
name: 'Some', name: 'Some',
}, { }, {
limit: 10, limit: 15,
name: 'Most', name: 'Most',
}, { }, {
limit: 1E6, limit: 1E6,
@@ -63,13 +62,13 @@ const styles: StyleRulesCallback = theme => ({
color: theme.palette.text.hint, color: theme.palette.text.hint,
cursor: 'pointer' as 'pointer', cursor: 'pointer' as 'pointer',
}, },
switchBase: {
height: theme.spacing(4),
},
}) })
interface Props { interface Props {
actions: typeof settingsActions actions: {
settings: typeof settingsActions,
global: typeof globalActions,
}
autoExpandLimit: number autoExpandLimit: number
classes: any classes: any
highlightTopicUpdates: boolean highlightTopicUpdates: boolean
@@ -93,53 +92,20 @@ class Settings extends React.Component<Props, {}> {
private renderHighlightTopicUpdates() { private renderHighlightTopicUpdates() {
const { highlightTopicUpdates, actions } = this.props const { highlightTopicUpdates, actions } = this.props
return this.renderSwitch('Show Activity', highlightTopicUpdates, actions.toggleHighlightTopicUpdates, 'Topics blink when a new message arrives') return <BooleanSwitch title="Show Activity" tooltip="Topics blink when a new message arrives" value={highlightTopicUpdates} action={actions.settings.toggleHighlightTopicUpdates}/>
}
private renderSwitch(title: string, checked: boolean, action: any, tooltip: string) {
const { classes } = this.props
const clickHandler = (e: React.MouseEvent) => {
e.stopPropagation()
e.preventDefault()
action()
}
return (
<div style={{ padding: '8px', display: 'flex' }}>
<Tooltip title={tooltip}>
<InputLabel
htmlFor={`toggle-${sha1(title)}`}
onClick={clickHandler}
style={{ flex: '1', paddingTop: '8px' }}
>
{title}
</InputLabel>
</Tooltip>
<Tooltip title={tooltip}>
<Switch
name={`toggle-${sha1(title)}`}
checked={checked}
onChange={action}
color="primary"
classes={{ switchBase: classes.switchBase }}
/>
</Tooltip>
</div>
)
} }
private selectTopicsOnMouseOver() { private selectTopicsOnMouseOver() {
const { actions, selectTopicWithMouseOver } = this.props const { actions, selectTopicWithMouseOver } = this.props
const toggle = () => actions.selectTopicWithMouseOver(!selectTopicWithMouseOver) const toggle = () => actions.settings.selectTopicWithMouseOver(!selectTopicWithMouseOver)
return this.renderSwitch('Quick Preview', selectTopicWithMouseOver, toggle, 'Select topics on mouse over') return <BooleanSwitch title="Quick Preview" tooltip="Select topics on mouse over" value={selectTopicWithMouseOver} action={toggle} />
} }
private toggleTheme() { private toggleTheme() {
const { actions, theme } = this.props const { actions, theme } = this.props
return this.renderSwitch('Theme', theme === 'light', actions.toggleTheme, 'Select a theme') return <BooleanSwitch title="Dark theme" tooltip="Enable dark theme" value={theme === 'light'} action={actions.settings.toggleTheme} />
} }
private renderAutoExpand() { private renderAutoExpand() {
@@ -164,7 +130,7 @@ class Settings extends React.Component<Props, {}> {
} }
private onChangeAutoExpand = (e: React.ChangeEvent<HTMLSelectElement>) => { private onChangeAutoExpand = (e: React.ChangeEvent<HTMLSelectElement>) => {
this.props.actions.setAutoExpandLimit(parseInt(e.target.value, 10)) this.props.actions.settings.setAutoExpandLimit(parseInt(e.target.value, 10))
} }
private renderNodeOrder() { private renderNodeOrder() {
@@ -191,7 +157,7 @@ class Settings extends React.Component<Props, {}> {
} }
private onChangeSorting = (e: React.ChangeEvent<HTMLSelectElement>) => { private onChangeSorting = (e: React.ChangeEvent<HTMLSelectElement>) => {
this.props.actions.setTopicOrder(e.target.value as TopicOrder) this.props.actions.settings.setTopicOrder(e.target.value as TopicOrder)
} }
public render() { public render() {
@@ -208,15 +174,15 @@ class Settings extends React.Component<Props, {}> {
tabIndex={0} tabIndex={0}
role="button" role="button"
> >
<Typography className={classes.title} variant="h6" color="inherit"> <Typography className={classes.title} variant="h6" color="inherit">
<IconButton onClick={actions.toggleSettingsVisibility}> <IconButton onClick={actions.global.toggleSettingsVisibility}>
<ChevronRight /> <ChevronRight />
</IconButton> </IconButton>
Settings Settings
</Typography> </Typography>
<Divider /> <Divider style={{ userSelect: 'none' }} />
</div>
<div>
{this.renderAutoExpand()} {this.renderAutoExpand()}
{this.renderNodeOrder()} {this.renderNodeOrder()}
{this.renderHighlightTopicUpdates()} {this.renderHighlightTopicUpdates()}
@@ -247,7 +213,10 @@ const mapStateToProps = (state: AppState) => {
const mapDispatchToProps = (dispatch: any) => { const mapDispatchToProps = (dispatch: any) => {
return { return {
actions: bindActionCreators(settingsActions, dispatch), actions: {
settings: bindActionCreators(settingsActions, dispatch),
global: bindActionCreators(globalActions, dispatch),
}
} }
} }