Skip to content

Commit

Permalink
chore: move electron-settings to IPC (#1655)
Browse files Browse the repository at this point in the history
  • Loading branch information
eglitise authored Sep 5, 2024
1 parent 4da061a commit 2c84978
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
6 changes: 3 additions & 3 deletions app/common/renderer/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import i18n from 'i18next';
import _ from 'lodash';
import {initReactI18next, withTranslation as wt} from 'react-i18next';

import {commonI18NextOptions, fallbackLng} from '../shared/i18next.config';
import {commonI18NextOptions} from '../shared/i18next.config';
import {PREFERRED_LANGUAGE} from '../shared/setting-defs';
import {i18NextBackend, i18NextBackendOptions, getSettingSync} from './polyfills';
import {i18NextBackend, i18NextBackendOptions, getSetting} from './polyfills';

const i18nextOptions = {
...commonI18NextOptions,
backend: i18NextBackendOptions,
lng: getSettingSync(PREFERRED_LANGUAGE) || fallbackLng,
lng: await getSetting(PREFERRED_LANGUAGE),
};

const namespace = 'translation';
Expand Down
4 changes: 0 additions & 4 deletions app/common/renderer/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export async function setSetting(setting, value) {
await settings.set(setting, value);
}

export function getSettingSync(setting) {
return settings.getSync(setting);
}

export {
copyToClipboard,
openLink,
Expand Down
4 changes: 4 additions & 0 deletions app/electron/main/helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {clipboard, ipcMain, shell} from 'electron';
import settings from 'electron-settings';
import fs from 'fs';

import i18n from './i18next';

export const isDev = process.env.NODE_ENV === 'development';

export function setupIPCListeners() {
ipcMain.handle('settings:has', async (_evt, key) => await settings.has(key));
ipcMain.handle('settings:set', async (_evt, key, value) => await settings.set(key, value));
ipcMain.handle('settings:get', async (_evt, key) => await settings.get(key));
ipcMain.on('electron:openLink', (_evt, link) => shell.openExternal(link));
ipcMain.on('electron:copyToClipboard', (_evt, text) => clipboard.writeText(text));
ipcMain.handle('sessionfile:open', async (_evt, filePath) => openSessionFile(filePath));
Expand Down
16 changes: 15 additions & 1 deletion app/electron/renderer/polyfills.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ipcRenderer} from 'electron';
import settings from 'electron-settings';
import i18NextBackend from 'i18next-fs-backend';
import {join} from 'path';

Expand All @@ -20,6 +19,21 @@ const electronUtils = {
openLink: (link) => ipcRenderer.send('electron:openLink', link),
};

class ElectronSettings {
async has(key) {
return await ipcRenderer.invoke('settings:has', key);
}

async set(key, val) {
return await ipcRenderer.invoke('settings:set', key, val);
}

async get(key) {
return await ipcRenderer.invoke('settings:get', key);
}
}

const settings = new ElectronSettings();
const {copyToClipboard, openLink} = electronUtils;

export {settings, copyToClipboard, openLink, ipcRenderer, i18NextBackend, i18NextBackendOptions};
23 changes: 10 additions & 13 deletions app/web/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ const localesPath =
? '/locales' // 'public' folder contents are served at '/'
: '../locales'; // from 'dist-browser/assets/'

const i18NextBackendOptions = {
backends: [LocalStorageBackend, HttpApi],
backendOptions: [
{},
{
loadPath: `${localesPath}/{{lng}}/{{ns}}.json`,
},
],
};

const browserUtils = {
copyToClipboard: (text) => navigator.clipboard.writeText(text),
openLink: (url) => window.open(url, ''),
Expand All @@ -29,22 +39,9 @@ class BrowserSettings {
get(key) {
return JSON.parse(localStorage.getItem(key));
}

getSync(key) {
return this.get(key);
}
}

const settings = new BrowserSettings();
const {copyToClipboard, openLink, ipcRenderer} = browserUtils;
const i18NextBackendOptions = {
backends: [LocalStorageBackend, HttpApi],
backendOptions: [
{},
{
loadPath: `${localesPath}/{{lng}}/{{ns}}.json`,
},
],
};

export {settings, copyToClipboard, openLink, ipcRenderer, i18NextBackend, i18NextBackendOptions};
1 change: 1 addition & 0 deletions vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
emptyOutDir: true,
minify: false, // needed to avoid issues with web2driver
reportCompressedSize: false,
target: 'es2022',
},
define: {
'process.env': process.env,
Expand Down

0 comments on commit 2c84978

Please sign in to comment.