Skip to content

Commit

Permalink
chore: decouple shared code from main/renderer imports (#1587)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
eglitise authored Aug 7, 2024
1 parent 899d04c commit ab73873
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 48 deletions.
3 changes: 2 additions & 1 deletion app/common/renderer/actions/Inspector.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
9 changes: 3 additions & 6 deletions app/common/renderer/actions/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
12 changes: 9 additions & 3 deletions app/common/renderer/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 18 additions & 1 deletion app/common/renderer/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/common/renderer/reducers/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down
28 changes: 11 additions & 17 deletions app/common/shared/i18next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import settings from './settings';

export const languageList = [
{name: 'Arabic', code: 'ar', original: 'العربية'},
{name: 'Chinese Simplified', code: 'zh-CN', original: '中文简体'},
Expand All @@ -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),
};
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 8 additions & 2 deletions app/electron/main/i18next.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions app/electron/main/windows.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ab73873

Please sign in to comment.