From cf3893bf521a73425a9b577a2ddc9a6492900e88 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Thu, 7 Nov 2024 18:24:13 +0000 Subject: [PATCH] Tidy settings --- src/config/Config.ts | 15 +++++++++++---- src/settings/SettingsModal.tsx | 2 +- src/settings/settings.ts | 6 ++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/config/Config.ts b/src/config/Config.ts index b230734c..cbe68e55 100644 --- a/src/config/Config.ts +++ b/src/config/Config.ts @@ -70,10 +70,17 @@ export class Config { private applyConfigToSettings(): void { if (!this.config) return; - // only the value from config if it hasn't been overridden - if (showNonMemberTiles.value === undefined) { - showNonMemberTiles.setValue(this.config.show_non_member_tiles); - } + // use the value from config if it hasn't been overridden + const showNonMemberTilesSubscription = showNonMemberTiles.value.subscribe( + (val) => { + if (val === undefined && this.config) { + // we don't persist the value to local storage so that it is set from the config + // file on every startup + showNonMemberTiles.setValue(this.config.show_non_member_tiles, false); + showNonMemberTilesSubscription.unsubscribe(); + } + }, + ); } public config?: ResolvedConfigOptions; diff --git a/src/settings/SettingsModal.tsx b/src/settings/SettingsModal.tsx index eaf38be5..d36a05d2 100644 --- a/src/settings/SettingsModal.tsx +++ b/src/settings/SettingsModal.tsx @@ -246,7 +246,7 @@ export const SettingsModal: FC = ({ id="showNonMemberTiles" type="checkbox" label={t("settings.show_non_member_tiles")} - checked={showNonMemberTiles} + checked={!!showNonMemberTiles} onChange={useCallback( (event: ChangeEvent): void => { setShowNonMemberTiles(event.target.checked); diff --git a/src/settings/settings.ts b/src/settings/settings.ts index 47b80d9b..b614f5db 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -37,9 +37,11 @@ export class Setting { private readonly _value: BehaviorSubject; public readonly value: Observable; - public readonly setValue = (value: T): void => { + public readonly setValue = (value: T, persist = true): void => { this._value.next(value); - localStorage.setItem(this.key, JSON.stringify(value)); + if (persist) { + localStorage.setItem(this.key, JSON.stringify(value)); + } }; }