Save theme settings to localStorage
This commit is contained in:
parent
09c1516a17
commit
347f3ce39e
|
|
@ -21,17 +21,6 @@ import {observer} from 'mobx-react';
|
||||||
import {observable} from 'mobx';
|
import {observable} from 'mobx';
|
||||||
import {inject, Stores} from '../inject';
|
import {inject, Stores} from '../inject';
|
||||||
|
|
||||||
const lightTheme = createMuiTheme({
|
|
||||||
palette: {
|
|
||||||
type: 'light',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const darkTheme = createMuiTheme({
|
|
||||||
palette: {
|
|
||||||
type: 'dark',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const styles = (theme: Theme) => ({
|
const styles = (theme: Theme) => ({
|
||||||
content: {
|
content: {
|
||||||
margin: '0 auto',
|
margin: '0 auto',
|
||||||
|
|
@ -41,12 +30,31 @@ const styles = (theme: Theme) => ({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const localStorageThemeKey = 'gotify-theme';
|
||||||
|
type ThemeKey = 'dark' | 'light';
|
||||||
|
const themeMap: Record<ThemeKey, Theme> = {
|
||||||
|
light: createMuiTheme({
|
||||||
|
palette: {
|
||||||
|
type: 'light',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
dark: createMuiTheme({
|
||||||
|
palette: {
|
||||||
|
type: 'dark',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const isThemeKey = (value: string | null): value is ThemeKey => {
|
||||||
|
return value === 'light' || value === 'dark';
|
||||||
|
};
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
class Layout extends React.Component<WithStyles<'content'> & Stores<'currentUser'>> {
|
class Layout extends React.Component<WithStyles<'content'> & Stores<'currentUser'>> {
|
||||||
private static defaultVersion = '0.0.0';
|
private static defaultVersion = '0.0.0';
|
||||||
|
|
||||||
@observable
|
@observable
|
||||||
private darkThemeVisible = true;
|
private currentTheme: ThemeKey = 'dark';
|
||||||
@observable
|
@observable
|
||||||
private showSettings = false;
|
private showSettings = false;
|
||||||
@observable
|
@observable
|
||||||
|
|
@ -58,10 +66,17 @@ class Layout extends React.Component<WithStyles<'content'> & Stores<'currentUser
|
||||||
this.version = resp.data.version;
|
this.version = resp.data.version;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const localStorageTheme = window.localStorage.getItem(localStorageThemeKey);
|
||||||
|
if (isThemeKey(localStorageTheme)) {
|
||||||
|
this.currentTheme = localStorageTheme;
|
||||||
|
} else {
|
||||||
|
window.localStorage.setItem(localStorageThemeKey, this.currentTheme);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public render() {
|
public render() {
|
||||||
const {version, showSettings, darkThemeVisible} = this;
|
const {version, showSettings, currentTheme} = this;
|
||||||
const {
|
const {
|
||||||
classes,
|
classes,
|
||||||
currentUser: {
|
currentUser: {
|
||||||
|
|
@ -71,7 +86,7 @@ class Layout extends React.Component<WithStyles<'content'> & Stores<'currentUser
|
||||||
logout,
|
logout,
|
||||||
},
|
},
|
||||||
} = this.props;
|
} = this.props;
|
||||||
const theme = darkThemeVisible ? darkTheme : lightTheme;
|
const theme = themeMap[currentTheme];
|
||||||
const loginRoute = () => (loggedIn ? <Redirect to="/" /> : <Login />);
|
const loginRoute = () => (loggedIn ? <Redirect to="/" /> : <Login />);
|
||||||
return (
|
return (
|
||||||
<MuiThemeProvider theme={theme}>
|
<MuiThemeProvider theme={theme}>
|
||||||
|
|
@ -83,7 +98,7 @@ class Layout extends React.Component<WithStyles<'content'> & Stores<'currentUser
|
||||||
name={name}
|
name={name}
|
||||||
version={version}
|
version={version}
|
||||||
loggedIn={loggedIn}
|
loggedIn={loggedIn}
|
||||||
toggleTheme={() => (this.darkThemeVisible = !this.darkThemeVisible)}
|
toggleTheme={this.toggleTheme.bind(this)}
|
||||||
showSettings={() => (this.showSettings = true)}
|
showSettings={() => (this.showSettings = true)}
|
||||||
logout={logout}
|
logout={logout}
|
||||||
/>
|
/>
|
||||||
|
|
@ -117,6 +132,11 @@ class Layout extends React.Component<WithStyles<'content'> & Stores<'currentUser
|
||||||
</MuiThemeProvider>
|
</MuiThemeProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private toggleTheme() {
|
||||||
|
this.currentTheme = this.currentTheme === 'dark' ? 'light' : 'dark';
|
||||||
|
localStorage.setItem(localStorageThemeKey, this.currentTheme);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withStyles(styles, {withTheme: true})<{}>(inject('currentUser')(Layout));
|
export default withStyles(styles, {withTheme: true})<{}>(inject('currentUser')(Layout));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue