From ab73873098770d5b0beaa04db6532e9343f89250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgars=20Egl=C4=ABtis?= <37242620+eglitise@users.noreply.github.com> Date: Wed, 7 Aug 2024 19:35:04 +0300 Subject: [PATCH] chore: decouple shared code from main/renderer imports (#1587) * chore: remove settings dependency in shared i18next config * chore: move settings out of shared folder * chore: separate setting defs from functions also include the visible providers const in setting defs * chore: move setting methods to polyfills * chore: add preferred language to setting defs * chore: set common i18next options first * chore: update comment --- app/common/renderer/actions/Inspector.js | 3 +- app/common/renderer/actions/Session.js | 9 ++---- app/common/renderer/i18next.js | 12 ++++++-- app/common/renderer/polyfills.js | 19 ++++++++++++- app/common/renderer/reducers/Session.js | 2 +- app/common/shared/i18next.config.js | 28 ++++++++----------- .../shared/{settings.js => setting-defs.js} | 23 ++++++--------- app/electron/main/i18next.js | 10 +++++-- app/electron/main/windows.js | 5 ++-- 9 files changed, 63 insertions(+), 48 deletions(-) rename app/common/shared/{settings.js => setting-defs.js} (57%) diff --git a/app/common/renderer/actions/Inspector.js b/app/common/renderer/actions/Inspector.js index ce2dcc9dd3..64cd6b6c46 100644 --- a/app/common/renderer/actions/Inspector.js +++ b/app/common/renderer/actions/Inspector.js @@ -1,11 +1,12 @@ import _ from 'lodash'; import {v4 as UUID} from 'uuid'; -import {SAVED_FRAMEWORK, SET_SAVED_GESTURES, getSetting, setSetting} from '../../shared/settings'; +import {SAVED_FRAMEWORK, SET_SAVED_GESTURES} from '../../shared/setting-defs'; import {APP_MODE, NATIVE_APP} from '../constants/session-inspector'; import i18n from '../i18next'; import AppiumClient from '../lib/appium-client'; import frameworks from '../lib/client-frameworks'; +import {getSetting, setSetting} from '../polyfills'; import {getOptimalXPath, getSuggestedLocators} from '../utils/locator-generation'; import { domParser, diff --git a/app/common/renderer/actions/Session.js b/app/common/renderer/actions/Session.js index 274e505305..dd4d980268 100644 --- a/app/common/renderer/actions/Session.js +++ b/app/common/renderer/actions/Session.js @@ -19,13 +19,12 @@ import { SERVER_ARGS, SESSION_SERVER_PARAMS, SESSION_SERVER_TYPE, - getSetting, - setSetting, -} from '../../shared/settings'; + VISIBLE_PROVIDERS, +} from '../../shared/setting-defs'; import {APP_MODE} from '../constants/session-inspector'; import CloudProviders from '../components/Session/CloudProviders.jsx'; import i18n from '../i18next'; -import {fs, ipcRenderer, util} from '../polyfills'; +import {fs, ipcRenderer, util, getSetting, setSetting} from '../polyfills'; import {log} from '../utils/logger'; import {addVendorPrefixes} from '../utils/other'; import {quitSession, setSessionDetails} from './Inspector'; @@ -52,8 +51,6 @@ export const CHANGE_SERVER_TYPE = 'CHANGE_SERVER_TYPE'; export const SET_SERVER_PARAM = 'SET_SERVER_PARAM'; export const SET_SERVER = 'SET_SERVER'; -export const VISIBLE_PROVIDERS = 'VISIBLE_PROVIDERS'; - export const SET_ATTACH_SESS_ID = 'SET_ATTACH_SESS_ID'; export const GET_SESSIONS_REQUESTED = 'GET_SESSIONS_REQUESTED'; diff --git a/app/common/renderer/i18next.js b/app/common/renderer/i18next.js index fb57f0aef8..ebfb354296 100644 --- a/app/common/renderer/i18next.js +++ b/app/common/renderer/i18next.js @@ -2,10 +2,16 @@ import i18n from 'i18next'; import _ from 'lodash'; import {initReactI18next, withTranslation as wt} from 'react-i18next'; -import {getI18NextOptions} from '../shared/i18next.config'; -import {i18NextBackend, i18NextBackendOptions} from './polyfills'; +import {commonI18NextOptions, fallbackLng} from '../shared/i18next.config'; +import {PREFERRED_LANGUAGE} from '../shared/setting-defs'; +import {i18NextBackend, i18NextBackendOptions, getSettingSync} from './polyfills'; + +const i18nextOptions = { + ...commonI18NextOptions, + backend: i18NextBackendOptions, + lng: getSettingSync(PREFERRED_LANGUAGE) || fallbackLng, +}; -const i18nextOptions = getI18NextOptions(i18NextBackendOptions); const namespace = 'translation'; if (!i18n.isInitialized) { diff --git a/app/common/renderer/polyfills.js b/app/common/renderer/polyfills.js index 323ae4e6ae..4eeeb6c891 100644 --- a/app/common/renderer/polyfills.js +++ b/app/common/renderer/polyfills.js @@ -4,8 +4,25 @@ * they cannot be added to tsconfig and eslint configurations */ +import {settings} from '#local-polyfills'; // eslint-disable-line import/no-unresolved +import {DEFAULT_SETTINGS} from '../shared/setting-defs'; + +export async function getSetting(setting) { + if (await settings.has(setting)) { + return await settings.get(setting); + } + return DEFAULT_SETTINGS[setting]; +} + +export async function setSetting(setting, value) { + await settings.set(setting, value); +} + +export function getSettingSync(setting) { + return settings.getSync(setting); +} + export { - settings, clipboard, shell, ipcRenderer, diff --git a/app/common/renderer/reducers/Session.js b/app/common/renderer/reducers/Session.js index f76815ebd5..135060eab4 100644 --- a/app/common/renderer/reducers/Session.js +++ b/app/common/renderer/reducers/Session.js @@ -43,7 +43,7 @@ import { ServerTypes, } from '../actions/Session'; -const visibleProviders = []; // Pull this from "electron-settings" +const visibleProviders = []; // Pull this from the VISIBLE_PROVIDERS setting const server = { local: {}, remote: {}, diff --git a/app/common/shared/i18next.config.js b/app/common/shared/i18next.config.js index d0787feceb..ffd48132f4 100644 --- a/app/common/shared/i18next.config.js +++ b/app/common/shared/i18next.config.js @@ -1,5 +1,3 @@ -import settings from './settings'; - export const languageList = [ {name: 'Arabic', code: 'ar', original: 'العربية'}, {name: 'Chinese Simplified', code: 'zh-CN', original: '中文简体'}, @@ -25,19 +23,15 @@ export const languageList = [ {name: 'Ukrainian', code: 'uk', original: 'Українська'}, ]; -const fallbackLng = 'en'; +export const fallbackLng = 'en'; -export function getI18NextOptions(backend) { - return { - backend, - // debug: true, - // saveMissing: true, - interpolation: { - escapeValue: false, - }, - lng: (settings && settings.getSync('PREFERRED_LANGUAGE')) || fallbackLng, - load: 'currentOnly', - fallbackLng, - supportedLngs: languageList.map((language) => language.code), - }; -} +export const commonI18NextOptions = { + // debug: true, + // saveMissing: true, + interpolation: { + escapeValue: false, + }, + load: 'currentOnly', + fallbackLng, + supportedLngs: languageList.map((language) => language.code), +}; diff --git a/app/common/shared/settings.js b/app/common/shared/setting-defs.js similarity index 57% rename from app/common/shared/settings.js rename to app/common/shared/setting-defs.js index 435b3514aa..e9ee56879b 100644 --- a/app/common/shared/settings.js +++ b/app/common/shared/setting-defs.js @@ -1,30 +1,23 @@ -import {settings} from '../renderer/polyfills'; +// Definitions for all the persistent settings used in the app +import {fallbackLng} from './i18next.config'; + +export const PREFERRED_LANGUAGE = 'PREFERRED_LANGUAGE'; export const SAVED_SESSIONS = 'SAVED_SESSIONS'; export const SET_SAVED_GESTURES = 'SET_SAVED_GESTURES'; export const SERVER_ARGS = 'SERVER_ARGS'; export const SESSION_SERVER_PARAMS = 'SESSION_SERVER_PARAMS'; export const SESSION_SERVER_TYPE = 'SESSION_SERVER_TYPE'; export const SAVED_FRAMEWORK = 'SAVED_FRAMEWORK'; +export const VISIBLE_PROVIDERS = 'VISIBLE_PROVIDERS'; -const DEFAULT_SETTINGS = { +export const DEFAULT_SETTINGS = { + [PREFERRED_LANGUAGE]: fallbackLng, [SAVED_SESSIONS]: [], [SET_SAVED_GESTURES]: [], [SERVER_ARGS]: null, [SESSION_SERVER_PARAMS]: null, [SESSION_SERVER_TYPE]: null, [SAVED_FRAMEWORK]: 'java', + [VISIBLE_PROVIDERS]: null, }; - -export async function getSetting(setting) { - if (await settings.has(setting)) { - return await settings.get(setting); - } - return DEFAULT_SETTINGS[setting]; -} - -export async function setSetting(setting, value) { - await settings.set(setting, value); -} - -export default settings; diff --git a/app/electron/main/i18next.js b/app/electron/main/i18next.js index cff6652f86..b8096a8afa 100644 --- a/app/electron/main/i18next.js +++ b/app/electron/main/i18next.js @@ -1,8 +1,10 @@ +import settings from 'electron-settings'; import i18n from 'i18next'; import i18NextBackend from 'i18next-fs-backend'; import {join} from 'path'; -import {getI18NextOptions} from '../../common/shared/i18next.config'; +import {commonI18NextOptions, fallbackLng} from '../../common/shared/i18next.config'; +import {PREFERRED_LANGUAGE} from '../../common/shared/setting-defs'; const localesPath = process.env.NODE_ENV === 'development' @@ -16,7 +18,11 @@ const i18NextBackendOptions = { jsonIndent: 2, }; -const i18nextOptions = getI18NextOptions(i18NextBackendOptions); +const i18nextOptions = { + ...commonI18NextOptions, + backend: i18NextBackendOptions, + lng: settings.getSync(PREFERRED_LANGUAGE) || fallbackLng, +}; if (!i18n.isInitialized) { i18n.use(i18NextBackend).init(i18nextOptions); diff --git a/app/electron/main/windows.js b/app/electron/main/windows.js index bd512180fe..5d48dcd5f2 100644 --- a/app/electron/main/windows.js +++ b/app/electron/main/windows.js @@ -1,7 +1,8 @@ import {BrowserWindow, Menu, dialog, ipcMain, webContents} from 'electron'; +import settings from 'electron-settings'; import {join} from 'path'; -import settings from '../../common/shared/settings'; +import {PREFERRED_LANGUAGE} from '../../common/shared/setting-defs'; import i18n from './i18next'; import {openFilePath} from './main'; import {APPIUM_SESSION_EXTENSION, isDev} from './helpers'; @@ -105,7 +106,7 @@ export function setupMainWindow() { return; } rebuildMenus(mainWindow); - await settings.set('PREFERRED_LANGUAGE', languageCode); + await settings.set(PREFERRED_LANGUAGE, languageCode); webContents.getAllWebContents().forEach((wc) => { wc.send('appium-language-changed', { language: languageCode,