Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/App/Settings/Core/EmbeddedCoreSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Button from 'components/Button';
import Switch from 'components/Switch';
import { TextField } from 'components/TextField';
import { useFieldValue } from 'lib/form';
import nexusEnv from 'lib/nexusEnv';
import { updateSettings, settingAtoms } from 'lib/settings';
import { confirm, openErrorDialog } from 'lib/dialog';
import { restartCore, stopCore, startCore } from 'lib/core';
Expand Down Expand Up @@ -102,7 +103,7 @@ function CoreBinaryPathField() {
title: __('Select Nexus Core binary'),
properties: ['openFile'],
filters:
process.platform === 'win32'
nexusEnv.platform === 'win32'
? [{ name: 'Windows executable', extensions: ['exe'] }]
: undefined,
});
Expand Down
3 changes: 2 additions & 1 deletion src/App/Settings/Style/BackgroundPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
updateTheme,
themeAtom,
} from 'lib/theme';
import nexusEnv from 'lib/nexusEnv';

__ = __context('Settings.Style');

Expand All @@ -27,7 +28,7 @@ async function handleFilePick() {
});
let path = files?.[0];
if (path) {
if (process.platform === 'win32') {
if (nexusEnv.platform === 'win32') {
path = path.replace(/\\/g, '/');
}
updateTheme({ wallpaper: path });
Expand Down
7 changes: 7 additions & 0 deletions src/main/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ function exposeInMainWorld(name, api) {
exposeInMainWorld('nexusEnv', {
NODE_ENV: process.env.NODE_ENV || 'production',
PORT: process.env.PORT || '',
// Exposed so renderer code can branch on OS without needing direct access
// to the `process` global, which is unavailable when contextIsolation is
// enabled and nodeIntegration is disabled.
platform: process.platform,
arch: process.arch,
HOME: process.env.HOME || '',
USERPROFILE: process.env.USERPROFILE || '',
});

exposeInMainWorld('nexusElectron', {
Expand Down
11 changes: 6 additions & 5 deletions src/shared/lib/appMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from 'lib/coreInfo';
// import { confirm } from 'lib/dialog';
import { walletDataDir } from 'consts/paths';
import nexusEnv, { isDevelopment } from 'lib/nexusEnv';
import { checkForUpdates, quitAndInstall, updaterStateAtom } from 'lib/updater';
import AboutModal from 'components/AboutModal';

Expand Down Expand Up @@ -340,9 +341,9 @@ function buildDarwinTemplate() {
label: __('View'),
submenu: [menuItems.reloadUI, menuItems.toggleFullScreen],
};
if (process.env.NODE_ENV === 'development' || devMode) {
if (isDevelopment || devMode) {
subMenuWindow.submenu.push(menuItems.toggleDevTools);
if (process.env.NODE_ENV === 'development') {
if (isDevelopment) {
subMenuWindow.submenu.push(menuItems.toggleJotaiDevTools);
subMenuWindow.submenu.push(menuItems.toggleReactQueryDevTools);
}
Expand Down Expand Up @@ -417,9 +418,9 @@ function buildDefaultTemplate() {
label: __('View'),
submenu: [menuItems.reloadUI, menuItems.toggleFullScreen],
};
if (process.env.NODE_ENV === 'development' || devMode) {
if (isDevelopment || devMode) {
subMenuView.submenu.push(menuItems.separator, menuItems.toggleDevTools);
if (process.env.NODE_ENV === 'development') {
if (isDevelopment) {
subMenuView.submenu.push(menuItems.toggleJotaiDevTools);
subMenuView.submenu.push(menuItems.toggleReactQueryDevTools);
}
Expand Down Expand Up @@ -452,7 +453,7 @@ function buildDefaultTemplate() {
*/
function buildMenu() {
const template =
process.platform === 'darwin'
nexusEnv.platform === 'darwin'
? buildDarwinTemplate()
: buildDefaultTemplate();
ipcRenderer.invoke('set-app-menu', template);
Expand Down
48 changes: 48 additions & 0 deletions src/shared/lib/nexusEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Safe accessor for `window.nexusEnv`, which is exposed by the preload
* script (see src/main/preload.js). Renderer code should read platform/env
* information through this module instead of the `process` global, since
* `process` is unavailable in the renderer's main world when
* contextIsolation is enabled and nodeIntegration is disabled.
*/
export interface NexusEnv {
/** Mirrors `process.env.NODE_ENV` from the main process. */
NODE_ENV: string;
/** Dev server port, only meaningful when `NODE_ENV === 'development'`. */
PORT: string;
/** Mirrors `process.platform` from the main process. */
platform: NodeJS.Platform;
/** Mirrors `process.arch` from the main process. */
arch: string;
/** Mirrors `process.env.HOME`; populated on macOS/Linux. */
HOME: string;
/** Mirrors `process.env.USERPROFILE`; populated on Windows. */
USERPROFILE: string;
}

const fallbackNexusEnv: NexusEnv = {
NODE_ENV: 'production',
PORT: '',
platform: 'linux',
arch: '',
HOME: '',
USERPROFILE: '',
};
Comment on lines +13 to +30

const injectedNexusEnv: NexusEnv | undefined = (window as any).nexusEnv;
if (!injectedNexusEnv) {
// window.nexusEnv should always be set by the preload script
// (src/main/preload.js). If it's missing, fall back to safe defaults but
// log loudly so the misconfiguration doesn't silently masquerade as Linux.
console.error(
'window.nexusEnv is not available; the preload bridge may not have run. ' +
'Falling back to default platform/env values, which may be incorrect.'
);
}

const nexusEnv: NexusEnv = injectedNexusEnv || fallbackNexusEnv;

export default nexusEnv;

export const isDevelopment = nexusEnv.NODE_ENV === 'development';
export const platform = nexusEnv.platform;
3 changes: 2 additions & 1 deletion src/shared/lib/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { queryClientAtom } from 'jotai-tanstack-query';
import { rqDevToolsOpenAtom, jotaiDevToolsOpenAtom } from 'lib/ui';
import jotaiDevToolsStyles from 'jotai-devtools/styles.css';
import { isDevelopment } from 'lib/nexusEnv';

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

export const store = createStore();

Expand Down
3 changes: 2 additions & 1 deletion src/shared/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { stopCore } from 'lib/core';
import { coreConnectedAtom } from 'lib/coreInfo';
import { logOut, loggedInAtom } from 'lib/session';
import { settingsAtom } from 'lib/settings';
import nexusEnv from 'lib/nexusEnv';

let _navigate: NavigateFunction | null = null;
export function navigate(to: To, options?: NavigateOptions) {
Expand Down Expand Up @@ -55,7 +56,7 @@ export function prepareWallet() {
const forceQuit = await ipcRenderer.invoke('is-force-quit');
if (!forceQuit) {
ipcRenderer.invoke('hide-window');
if (process.platform === 'darwin') {
if (nexusEnv.platform === 'darwin') {
ipcRenderer.invoke('hide-dock');
}
return;
Expand Down