From 0e115e06b1cd36d5dffef7dc0544363f4186d34b Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Wed, 3 Apr 2024 07:55:42 +0200 Subject: [PATCH] Refactor Webpacks (#1212) refactor webpacks --- frontend/src/contexts/AppContext.tsx | 20 ++++- .../Settings.default.basic.selfhosted.ts | 4 +- .../models/Settings.default.pro.selfhosted.ts | 4 +- frontend/src/models/Settings.default.pro.ts | 4 +- frontend/src/services/Native/index.d.ts | 1 + frontend/webpack.config.ts | 89 +------------------ nodeapp/basic.html | 5 +- nodeapp/pro.html | 5 +- web/basic.html | 3 + web/pro.html | 5 +- 10 files changed, 41 insertions(+), 99 deletions(-) diff --git a/frontend/src/contexts/AppContext.tsx b/frontend/src/contexts/AppContext.tsx index 99f91995..b2a79260 100644 --- a/frontend/src/contexts/AppContext.tsx +++ b/frontend/src/contexts/AppContext.tsx @@ -20,6 +20,9 @@ import { createTheme, type Theme } from '@mui/material/styles'; import i18n from '../i18n/Web'; import getWorldmapGeojson from '../geo/Web'; import { apiClient } from '../services/api'; +import SettingsSelfhosted from '../models/Settings.default.basic.selfhosted'; +import SettingsSelfhostedPro from '../models/Settings.default.pro.selfhosted'; +import SettingsPro from '../models/Settings.default.pro'; const getWindowSize = function (fontSize: number): { width: number; height: number } { // returns window size in EM units @@ -101,6 +104,19 @@ const getOrigin = (network = 'mainnet'): Origin => { return origin; }; +const getSettings = (): Settings => { + let settings = new Settings(); + if (window.RobosatsSettings === 'selfhosted-basic') { + settings = new SettingsSelfhosted(); + } else if (window.RobosatsSettings === 'selfhosted-pro') { + settings = new SettingsSelfhostedPro(); + } else if (window.RobosatsSettings === 'web-pro') { + settings = new SettingsPro(); + } + + return settings; +}; + export interface WindowSize { width: number; height: number; @@ -140,7 +156,7 @@ export interface UseAppStoreType { export const initialAppContext: UseAppStoreType = { theme: undefined, torStatus: 'NOTINIT', - settings: new Settings(), + settings: getSettings(), setSettings: () => {}, page: entryPage, setPage: () => {}, @@ -175,7 +191,7 @@ export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.E const hostUrl = initialAppContext.hostUrl; const origin = initialAppContext.origin; - const [settings, setSettings] = useState(initialAppContext.settings); + const [settings, setSettings] = useState(getSettings()); const [theme, setTheme] = useState(() => { return makeTheme(settings); }); diff --git a/frontend/src/models/Settings.default.basic.selfhosted.ts b/frontend/src/models/Settings.default.basic.selfhosted.ts index e8493e1e..9db4c295 100644 --- a/frontend/src/models/Settings.default.basic.selfhosted.ts +++ b/frontend/src/models/Settings.default.basic.selfhosted.ts @@ -1,7 +1,7 @@ import { systemClient } from '../services/System'; import BaseSettings from './Settings.model'; -class Settings extends BaseSettings { +class SettingsSelfhosted extends BaseSettings { constructor() { super(); const fontSizeCookie = systemClient.getItem('settings_fontsize_basic'); @@ -12,4 +12,4 @@ class Settings extends BaseSettings { public selfhostedClient: boolean = true; } -export default Settings; +export default SettingsSelfhosted; diff --git a/frontend/src/models/Settings.default.pro.selfhosted.ts b/frontend/src/models/Settings.default.pro.selfhosted.ts index f0af2805..aca5d300 100644 --- a/frontend/src/models/Settings.default.pro.selfhosted.ts +++ b/frontend/src/models/Settings.default.pro.selfhosted.ts @@ -1,7 +1,7 @@ import { systemClient } from '../services/System'; import BaseSettings from './Settings.model'; -class Settings extends BaseSettings { +class SettingsSelfhostedPro extends BaseSettings { constructor() { super(); const fontSizeCookie = systemClient.getItem('settings_fontsize_pro'); @@ -12,4 +12,4 @@ class Settings extends BaseSettings { public selfhostedClient: boolean = true; } -export default Settings; +export default SettingsSelfhostedPro; diff --git a/frontend/src/models/Settings.default.pro.ts b/frontend/src/models/Settings.default.pro.ts index 4b715313..2175406e 100644 --- a/frontend/src/models/Settings.default.pro.ts +++ b/frontend/src/models/Settings.default.pro.ts @@ -1,7 +1,7 @@ import { systemClient } from '../services/System'; import BaseSettings from './Settings.model'; -class Settings extends BaseSettings { +class SettingsPro extends BaseSettings { constructor() { super(); const fontSizeCookie = systemClient.getItem('settings_fontsize_pro'); @@ -11,4 +11,4 @@ class Settings extends BaseSettings { public frontend: 'basic' | 'pro' = 'pro'; } -export default Settings; +export default SettingsPro; diff --git a/frontend/src/services/Native/index.d.ts b/frontend/src/services/Native/index.d.ts index 3ef9c335..e93edd55 100644 --- a/frontend/src/services/Native/index.d.ts +++ b/frontend/src/services/Native/index.d.ts @@ -4,6 +4,7 @@ declare global { interface Window { ReactNativeWebView?: ReactNativeWebView; NativeRobosats?: NativeRobosats; + RobosatsSettings: 'web-basic' | 'web-pro' | 'selfhosted-basic' | 'selfhosted-pro'; } } diff --git a/frontend/webpack.config.ts b/frontend/webpack.config.ts index 51594f86..d3625542 100644 --- a/frontend/webpack.config.ts +++ b/frontend/webpack.config.ts @@ -32,93 +32,6 @@ const configWeb: Configuration = { }, }; -const configWebSelfhosted: Configuration = { - ...config, - module: { - ...config.module, - rules: [ - ...(config?.module?.rules || []), - { - test: path.resolve(__dirname, 'src/models/Settings.default.basic.ts'), - loader: 'file-replace-loader', - options: { - condition: 'if-replacement-exists', - replacement: path.resolve(__dirname, 'src/models/Settings.default.basic.selfhosted.ts'), - async: true, - }, - }, - ], - }, - output: { - path: path.resolve(__dirname, 'static/frontend'), - filename: 'basic.selfhosted.js', - }, -}; - -const configWebPro: Configuration = { - ...config, - module: { - ...config.module, - rules: [ - ...(config?.module?.rules || []), - { - test: path.resolve(__dirname, 'src/basic/Main.tsx'), - loader: 'file-replace-loader', - options: { - condition: 'if-replacement-exists', - replacement: path.resolve(__dirname, 'src/pro/Main.tsx'), - async: true, - }, - }, - { - test: path.resolve(__dirname, 'src/models/Settings.default.basic.ts'), - loader: 'file-replace-loader', - options: { - condition: 'if-replacement-exists', - replacement: path.resolve(__dirname, 'src/models/Settings.default.pro.ts'), - async: true, - }, - }, - ], - }, - output: { - path: path.resolve(__dirname, 'static/frontend'), - filename: 'pro.js', - }, -}; - -const configWebProSelfhosted: Configuration = { - ...config, - module: { - ...config.module, - rules: [ - ...(config?.module?.rules || []), - { - test: path.resolve(__dirname, 'src/basic/Main.tsx'), - loader: 'file-replace-loader', - options: { - condition: 'if-replacement-exists', - replacement: path.resolve(__dirname, 'src/pro/Main.tsx'), - async: true, - }, - }, - { - test: path.resolve(__dirname, 'src/models/Settings.default.basic.ts'), - loader: 'file-replace-loader', - options: { - condition: 'if-replacement-exists', - replacement: path.resolve(__dirname, 'src/models/Settings.default.pro.selfhosted.ts'), - async: true, - }, - }, - ], - }, - output: { - path: path.resolve(__dirname, 'static/frontend'), - filename: 'pro.selfhosted.js', - }, -}; - const configMobile: Configuration = { ...config, module: { @@ -177,4 +90,4 @@ const configMobile: Configuration = { }, }; -export default [configWeb, configWebPro, configWebSelfhosted, configWebProSelfhosted, configMobile]; +export default [configWeb, configMobile]; diff --git a/nodeapp/basic.html b/nodeapp/basic.html index 32af09a8..7034bd9b 100644 --- a/nodeapp/basic.html +++ b/nodeapp/basic.html @@ -55,6 +55,9 @@ - + + \ No newline at end of file diff --git a/nodeapp/pro.html b/nodeapp/pro.html index a582ac21..6e48a07a 100644 --- a/nodeapp/pro.html +++ b/nodeapp/pro.html @@ -57,6 +57,9 @@ - + + \ No newline at end of file diff --git a/web/basic.html b/web/basic.html index 99baef71..ffe6e35b 100644 --- a/web/basic.html +++ b/web/basic.html @@ -55,6 +55,9 @@ + \ No newline at end of file diff --git a/web/pro.html b/web/pro.html index 24292289..c75a0500 100644 --- a/web/pro.html +++ b/web/pro.html @@ -57,6 +57,9 @@ - + + \ No newline at end of file