From 8d332d0ff89c2f38a919b0105132da73038039bf Mon Sep 17 00:00:00 2001 From: Jonas B <62108893+JonBunator@users.noreply.github.com> Date: Wed, 1 Nov 2023 13:13:18 +0100 Subject: [PATCH] Fix loading of settings from config (#15) --- .../components/Settings/SettingsPage.tsx | 67 +++++++++---------- .../components/Settings/SettingsProvider.tsx | 24 ++++--- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/src/renderer/components/Settings/SettingsPage.tsx b/src/src/renderer/components/Settings/SettingsPage.tsx index 12ba37a..b08e14e 100644 --- a/src/src/renderer/components/Settings/SettingsPage.tsx +++ b/src/src/renderer/components/Settings/SettingsPage.tsx @@ -22,38 +22,31 @@ export default function SettingsPage() { settings?.setSettingsProperty(id, value); }; - // header content - function Header() { - return ( - - - Settings - - - - - - ); - } - return ( -
+ + + Settings + + + + + General @@ -66,7 +59,7 @@ export default function SettingsPage() { displayValues={['Auto', 'Dark', 'Light']} defaultValue={ settings?.getSettingsProperty( - 'color-mode' + 'color-mode', ) as string } onValueChange={propertyChanged} @@ -81,7 +74,7 @@ export default function SettingsPage() { type="string" defaultValue={ settings?.getSettingsProperty( - 'default-file-name' + 'default-file-name', ) as string } onValueChange={propertyChanged} @@ -96,7 +89,7 @@ export default function SettingsPage() { maxValue={10} defaultValue={ settings?.getSettingsProperty( - 'top-margin' + 'top-margin', ) as string } onValueChange={propertyChanged} @@ -111,7 +104,7 @@ export default function SettingsPage() { maxValue={10} defaultValue={ settings?.getSettingsProperty( - 'bottom-margin' + 'bottom-margin', ) as string } onValueChange={propertyChanged} @@ -126,7 +119,7 @@ export default function SettingsPage() { maxValue={10} defaultValue={ settings?.getSettingsProperty( - 'left-margin' + 'left-margin', ) as string } onValueChange={propertyChanged} @@ -141,7 +134,7 @@ export default function SettingsPage() { maxValue={10} defaultValue={ settings?.getSettingsProperty( - 'right-margin' + 'right-margin', ) as string } onValueChange={propertyChanged} @@ -154,7 +147,7 @@ export default function SettingsPage() { type="string" defaultValue={ settings?.getSettingsProperty( - 'footer-template' + 'footer-template', ) as string } onValueChange={propertyChanged} @@ -168,7 +161,7 @@ export default function SettingsPage() { canBeEmpty defaultValue={ settings?.getSettingsProperty( - 'header-template' + 'header-template', ) as string } onValueChange={propertyChanged} diff --git a/src/src/renderer/components/Settings/SettingsProvider.tsx b/src/src/renderer/components/Settings/SettingsProvider.tsx index c246ef7..4961760 100644 --- a/src/src/renderer/components/Settings/SettingsProvider.tsx +++ b/src/src/renderer/components/Settings/SettingsProvider.tsx @@ -4,17 +4,18 @@ import { useContext, useState, useEffect, + useMemo, } from 'react'; import { settingsData } from './Settings'; -const SettingsContext = createContext(undefined); - interface SettingsProps { settings: Map; setSettingsProperty: (id: string, value: string) => void; getSettingsProperty: (id: string) => string; } +const SettingsContext = createContext(undefined); + export type Props = { children: ReactNode; }; @@ -49,25 +50,30 @@ export default function SettingsProvider(props: Props) { if (value === undefined || value === null) return; if (settings.get(id) !== value) { setSettings((prev) => new Map(prev.set(id, value))); + window.electronAPI.setSettingsProperty(id, value); } } // load settings from saved config useEffect(() => { settings.forEach(async (value, id) => { - const oldValue = + const loadedValue = (await window.electronAPI.getSettingsProperty(id)) ?? ''; - if (oldValue !== value) { - window.electronAPI.setSettingsProperty(id, value); + if (loadedValue !== '') { + setSettingsProperty(id, loadedValue); } }); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [settings]); + }, []); + + const memoSettings = useMemo( + () => ({ settings, setSettingsProperty, getSettingsProperty }), + // eslint-disable-next-line react-hooks/exhaustive-deps + [settings], + ); return ( - + {children} );