From fb99c3491ceb1824f91a30db031bf9392fe2cd80 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Wed, 24 Apr 2024 15:55:32 +1000 Subject: [PATCH] chore: remove calls to Storage from settings.tsx --- ts/components/SessionInboxView.tsx | 24 ++++++++++++-- ts/state/ducks/settings.tsx | 53 ++++++++++++++---------------- ts/state/reducer.ts | 13 ++++---- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/ts/components/SessionInboxView.tsx b/ts/components/SessionInboxView.tsx index f2d8c7616..798ac095f 100644 --- a/ts/components/SessionInboxView.tsx +++ b/ts/components/SessionInboxView.tsx @@ -1,4 +1,4 @@ -import { fromPairs, map } from 'lodash'; +import { fromPairs, isBoolean, map } from 'lodash'; import moment from 'moment'; import React from 'react'; import { Provider } from 'react-redux'; @@ -87,11 +87,31 @@ function createSessionInboxStore() { return createStore(initialState); } +function getBoolFromStorageOrFalse(settingsKey: string): boolean { + const got = Storage.get(settingsKey, false); + if (isBoolean(got)) { + return got; + } + return false; +} function setupLeftPane(forceUpdateInboxComponent: () => void) { window.openConversationWithMessages = openConversationWithMessages; window.inboxStore = createSessionInboxStore(); - window.inboxStore.dispatch(updateAllOnStorageReady()); + + window.inboxStore.dispatch( + updateAllOnStorageReady({ + hasBlindedMsgRequestsEnabled: getBoolFromStorageOrFalse( + SettingsKey.hasBlindedMsgRequestsEnabled + ), + someDeviceOutdatedSyncing: getBoolFromStorageOrFalse(SettingsKey.someDeviceOutdatedSyncing), + settingsLinkPreview: getBoolFromStorageOrFalse(SettingsKey.settingsLinkPreview), + hasFollowSystemThemeEnabled: getBoolFromStorageOrFalse( + SettingsKey.hasFollowSystemThemeEnabled + ), + hasShiftSendEnabled: getBoolFromStorageOrFalse(SettingsKey.hasShiftSendEnabled), + }) + ); forceUpdateInboxComponent(); } diff --git a/ts/state/ducks/settings.tsx b/ts/state/ducks/settings.tsx index 318e7fc23..dd89ac1b2 100644 --- a/ts/state/ducks/settings.tsx +++ b/ts/state/ducks/settings.tsx @@ -1,8 +1,7 @@ import { isBoolean } from 'lodash'; import { PayloadAction, createSlice } from '@reduxjs/toolkit'; -import { SettingsKey } from '../../data/settings-key'; -import { Storage } from '../../util/storage'; +import { SettingsKey } from '../../data/settings-key'; // ok: does not import anything else const SettingsBoolsKeyTrackedInRedux = [ SettingsKey.someDeviceOutdatedSyncing, @@ -44,33 +43,31 @@ const settingsSlice = createSlice({ // Once the storage is ready, initialState: getSettingsInitialState(), reducers: { - updateAllOnStorageReady(state) { - const linkPreview = Storage.get(SettingsKey.settingsLinkPreview, false); - const outdatedSync = Storage.get(SettingsKey.someDeviceOutdatedSyncing, false); - const hasBlindedMsgRequestsEnabled = Storage.get( - SettingsKey.hasBlindedMsgRequestsEnabled, - false - ); - const hasFollowSystemThemeEnabled = Storage.get( - SettingsKey.hasFollowSystemThemeEnabled, - false - ); - const hasShiftSendEnabled = Storage.get(SettingsKey.hasShiftSendEnabled, false); - state.settingsBools.someDeviceOutdatedSyncing = isBoolean(outdatedSync) - ? outdatedSync - : false; - state.settingsBools['link-preview-setting'] = isBoolean(linkPreview) ? linkPreview : false; // this is the value of SettingsKey.settingsLinkPreview - state.settingsBools.hasBlindedMsgRequestsEnabled = isBoolean(hasBlindedMsgRequestsEnabled) - ? hasBlindedMsgRequestsEnabled - : false; + updateAllOnStorageReady( + state, + { + payload, + }: PayloadAction<{ + settingsLinkPreview: boolean; + someDeviceOutdatedSyncing: boolean; + hasBlindedMsgRequestsEnabled: boolean; + hasFollowSystemThemeEnabled: boolean; + hasShiftSendEnabled: boolean; + }> + ) { + const { + hasBlindedMsgRequestsEnabled, + hasFollowSystemThemeEnabled, + settingsLinkPreview, + someDeviceOutdatedSyncing, + hasShiftSendEnabled, + } = payload; - state.settingsBools.hasFollowSystemThemeEnabled = isBoolean(hasFollowSystemThemeEnabled) - ? hasFollowSystemThemeEnabled - : false; - - state.settingsBools.hasShiftSendEnabled = isBoolean(hasShiftSendEnabled) - ? hasShiftSendEnabled - : false; + state.settingsBools.someDeviceOutdatedSyncing = someDeviceOutdatedSyncing; + state.settingsBools['link-preview-setting'] = settingsLinkPreview; + state.settingsBools.hasBlindedMsgRequestsEnabled = hasBlindedMsgRequestsEnabled; + state.settingsBools.hasFollowSystemThemeEnabled = hasFollowSystemThemeEnabled; + state.settingsBools.hasShiftSendEnabled = hasShiftSendEnabled; return state; }, diff --git a/ts/state/reducer.ts b/ts/state/reducer.ts index 0715c8a75..da8148b46 100644 --- a/ts/state/reducer.ts +++ b/ts/state/reducer.ts @@ -1,17 +1,16 @@ import { combineReducers } from '@reduxjs/toolkit'; - import { callReducer as call, CallStateType } from './ducks/call'; // ok: importing only RingingManager.ts which is not importing anything else import { reducer as conversations, ConversationsStateType } from './ducks/conversations'; // todo import { defaultRoomReducer as defaultRooms, DefaultRoomsState } from './ducks/defaultRooms'; // todo import { reducer as primaryColor } from './ducks/primaryColor'; // ok: importing only Constants.tsx which is not importing anything else -import { reducer as search, SearchStateType } from './ducks/search'; -import { reducer as section, SectionStateType } from './ducks/section'; -import { ReduxSogsRoomInfos, SogsRoomInfoState } from './ducks/sogsRoomInfo'; -import { reducer as theme } from './ducks/theme'; // ok: importing only Constants.tsx which is not importing anything else -import { reducer as user, UserStateType } from './ducks/user'; // ok: not importing anything else +import { reducer as search, SearchStateType } from './ducks/search'; // todo +import { reducer as section, SectionStateType } from './ducks/section'; // ok: importing only SessionSettingsCategory which is not importing anything else +import { ReduxSogsRoomInfos, SogsRoomInfoState } from './ducks/sogsRoomInfo'; // todo +import { reducer as theme } from './ducks/theme'; // ok: importing only Constants.tsx which is not importing anything else +import { reducer as user, UserStateType } from './ducks/user'; // ok: not importing anything else -import { PrimaryColorStateType, ThemeStateType } from '../themes/constants/colors'; // ok: not importing anything else +import { PrimaryColorStateType, ThemeStateType } from '../themes/constants/colors'; // ok: not importing anything else import { modalReducer as modals, ModalState } from './ducks/modalDialog'; import { defaultOnionReducer as onionPaths, OnionState } from './ducks/onion'; // ok: not importing anything else import { settingsReducer, SettingsState } from './ducks/settings';