From 44fc24951fd5a5328c9f50e464cfa99cf84e871e Mon Sep 17 00:00:00 2001 From: David Ralph Date: Sun, 4 Jul 2021 12:28:28 +0100 Subject: [PATCH] fix: import settings on welcome, progress bar, custom js etc --- src/components/modals/welcome/Welcome.jsx | 2 +- .../modals/welcome/WelcomeSections.jsx | 57 ++++++++++++++++--- src/modules/default_settings.json | 4 -- src/modules/helpers/settings/index.js | 8 ++- 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/components/modals/welcome/Welcome.jsx b/src/components/modals/welcome/Welcome.jsx index fb017201..d3e60c14 100644 --- a/src/components/modals/welcome/Welcome.jsx +++ b/src/components/modals/welcome/Welcome.jsx @@ -43,7 +43,7 @@ export default class WelcomeModal extends React.PureComponent { this.setState({ currentTab: tab, image: this.images[tab], - buttonText: this.language.buttons.next + buttonText: (tab !== this.state.finalTab + 1) ? this.language.buttons.next : this.language.buttons.close }); } diff --git a/src/components/modals/welcome/WelcomeSections.jsx b/src/components/modals/welcome/WelcomeSections.jsx index 8ab9748a..67c5d8b2 100644 --- a/src/components/modals/welcome/WelcomeSections.jsx +++ b/src/components/modals/welcome/WelcomeSections.jsx @@ -9,17 +9,23 @@ import LightModeIcon from '@material-ui/icons/LightMode'; import DarkModeIcon from '@material-ui/icons/DarkMode'; import SettingsFunctions from '../../../modules/helpers/settings'; +import SettingsFunctionsModal from '../../../modules/helpers/settings/modals'; const languages = require('../../../modules/languages.json'); +const default_settings = require('../../../modules/default_settings.json'); export default class WelcomeSections extends React.Component { constructor() { super(); this.state = { + // themes autoClass: 'toggle auto active', lightClass: 'toggle lightTheme', darkClass: 'toggle darkTheme', - welcomeImage: 0 + // welcome + welcomeImage: 0, + // final + importedSettings: [] }; this.changeWelcomeImg = this.changeWelcomeImg.bind(this); this.welcomeImages = ['./welcome-images/example1.webp', './welcome-images/example2.webp', './welcome-images/example3.webp', './welcome-images/example4.webp']; @@ -42,8 +48,33 @@ export default class WelcomeSections extends React.Component { } importSettings(e) { - SettingsFunctions.importSettings(e); - this.props.switchTab(4); + SettingsFunctionsModal.importSettings(e); + + let settings = []; + const data = JSON.parse(e.target.result); + Object.keys(data).forEach((setting) => { + if (setting === 'language' || setting === 'theme'|| setting === 'firstRun' || setting === 'showWelcome' || setting === 'showReminder') { + return; + } + + const defaultSetting = default_settings.find((i) => i.name === setting); + if (defaultSetting !== undefined) { + if (data[setting] === String(defaultSetting.value)) { + return; + } + } + + settings.push({ + name: setting, + value: data[setting] + }); + }); + + this.setState({ + importedSettings: settings + }); + + this.props.switchTab(5); } changeWelcomeImg() { @@ -58,9 +89,16 @@ export default class WelcomeSections extends React.Component { this.timeout = setTimeout(this.changeWelcomeImg, 3 * 1000); } - componentWillUnmount() { - if (this.timeout) { - clearTimeout(this.timeout); + // cancel welcome image timer if not on welcome tab + componentDidUpdate() { + if (this.props.currentTab !== 0) { + if (this.timeout) { + clearTimeout(this.timeout); + } + } else { + if (!this.timeout) { + this.timeout = setTimeout(this.changeWelcomeImg, 3 * 1000); + } } } @@ -88,6 +126,8 @@ export default class WelcomeSections extends React.Component { ); const { appearance } = window.language.modals.main.settings.sections; + const languageSettings = window.language.modals.main.settings.sections.language; + const theme = ( <>

{language.sections.theme.title}

@@ -142,8 +182,9 @@ export default class WelcomeSections extends React.Component {

{language.sections.final.changes}

{language.sections.final.changes_description}

-
this.props.switchTab(1)}>Language: {languages.find((i) => i.value === localStorage.getItem('language')).name}
-
this.props.switchTab(3)}>Theme: {this.getSetting('theme')}
+
this.props.switchTab(1)}>{languageSettings.title}: {languages.find((i) => i.value === localStorage.getItem('language')).name}
+
this.props.switchTab(3)}>{appearance.theme.title}: {this.getSetting('theme')}
+ {(this.state.importedSettings.length !== 0) ?
this.props.switchTab(2)}>Imported {this.state.importedSettings.length} settings
: null}
); diff --git a/src/modules/default_settings.json b/src/modules/default_settings.json index f751f1f6..f8e85d3e 100644 --- a/src/modules/default_settings.json +++ b/src/modules/default_settings.json @@ -215,10 +215,6 @@ "name": "backgroundFilter", "value": "grayscale" }, - { - "name": "navbarHover", - "value": false - }, { "name": "apiQuality", "value": "high" diff --git a/src/modules/helpers/settings/index.js b/src/modules/helpers/settings/index.js index feaf90f8..eb19b5dd 100644 --- a/src/modules/helpers/settings/index.js +++ b/src/modules/helpers/settings/index.js @@ -92,8 +92,12 @@ export default class SettingsFunctions { const js = localStorage.getItem('customjs'); if (js) { - // eslint-disable-next-line no-eval - eval(js); + try { + // eslint-disable-next-line no-eval + eval(js); + } catch (e) { + console.error('Failed to run custom JS: ', e); + } } if (localStorage.getItem('experimental') === 'true') {