1
- import { persistReducer } from "redux-persist" ;
1
+ import { persistReducer , createTransform } from "redux-persist" ;
2
2
import storage from "redux-persist/lib/storage" ;
3
3
4
4
import type { Reducer } from "../actions/reduxTypes" ;
@@ -20,31 +20,60 @@ type SettingsNames =
20
20
21
21
export type UserSettings = Partial < Record < SettingsNames , boolean > > ;
22
22
23
- const reducer : Reducer < UserSettings > = (
24
- state = {
25
- "node.showAggregateSwitcher" : false ,
26
- "node.shortCounts" : false ,
27
- "node.showInputsAndOutputs" : false ,
28
- "node.showFragmentCreator" : false ,
29
- "node.autoApply" : false ,
30
- "cloud.showIntegrationsCreators" : false ,
31
- "debug.nodesAsJson" : false ,
32
- "debug.forceDisableModals" : false ,
33
- "debug.userSettingsVisible" : isDev ,
34
- "editor.jsonTemplate.showLines" : true ,
23
+ const getInitialUserFlag = ( flagName : SettingsNames , defaultValue = false ) : boolean => {
24
+ return window ?. [ "$initialUserFlags" ] ?. [ flagName ] ?? defaultValue ;
25
+ } ;
26
+
27
+ const getDefaultUserSettings = ( ) : UserSettings => ( {
28
+ "node.showAggregateSwitcher" : getInitialUserFlag ( "node.showAggregateSwitcher" ) ,
29
+ "node.shortCounts" : getInitialUserFlag ( "node.shortCounts" ) ,
30
+ "node.showInputsAndOutputs" : getInitialUserFlag ( "node.showInputsAndOutputs" ) ,
31
+ "node.showFragmentCreator" : getInitialUserFlag ( "node.showFragmentCreator" ) ,
32
+ "node.autoApply" : getInitialUserFlag ( "node.autoApply" ) ,
33
+ "cloud.showIntegrationsCreators" : getInitialUserFlag ( "cloud.showIntegrationsCreators" ) ,
34
+ "debug.nodesAsJson" : getInitialUserFlag ( "debug.nodesAsJson" ) ,
35
+ "debug.forceDisableModals" : getInitialUserFlag ( "debug.forceDisableModals" ) ,
36
+ "debug.userSettingsVisible" : getInitialUserFlag ( "debug.userSettingsVisible" , isDev ) ,
37
+ "editor.jsonTemplate.showLines" : getInitialUserFlag ( "editor.jsonTemplate.showLines" , true ) ,
38
+ } ) ;
39
+
40
+ /**
41
+ * @desc The idea is to get default values from the global config and then use them in the reducer unless the user changed the setting manually; in this case, we take a value from local storage.
42
+ * 1. We want to persist in a user setting state in localstorage only if the user has changed it.
43
+ * 2. We don't want to persist the default values.
44
+ * 3. When the value set by the user is the same as the default value, we don't want to persist it.
45
+ */
46
+ const filterInitialValuesTransform = createTransform (
47
+ ( inboundState , key ) => {
48
+ const persistedState = localStorage . getItem ( "persist:settings" ) ;
49
+ const defaults = getDefaultUserSettings ( ) ;
50
+
51
+ const valueFromLocalStorage = persistedState ?. [ key ] ;
52
+ if ( valueFromLocalStorage ) {
53
+ return valueFromLocalStorage ;
54
+ }
55
+
56
+ const valueSetByTheUserManually = defaults [ key ] !== inboundState ;
57
+
58
+ if ( valueSetByTheUserManually ) {
59
+ return inboundState ;
60
+ }
61
+ return undefined ;
35
62
} ,
36
- action ,
37
- ) => {
63
+ ( outboundState ) => outboundState ,
64
+ ) ;
65
+
66
+ const reducer : Reducer < UserSettings > = ( state = getDefaultUserSettings ( ) , action ) => {
38
67
switch ( action . type ) {
39
68
case "SET_SETTINGS" :
40
69
return action . settings ;
41
70
case "TOGGLE_SETTINGS" :
42
71
return action . settings . reduce ( ( value , key ) => ( { ...value , [ key ] : ! state [ key ] } ) , state ) ;
43
72
case "RESET_TOOLBARS" :
44
- return { ...state , "debug.userSettingsVisible" : isDev } ;
73
+ return { ...state , ... getDefaultUserSettings ( ) } ;
45
74
default :
46
75
return state ;
47
76
}
48
77
} ;
49
78
50
- export const userSettings = persistReducer ( { key : ` settings` , storage } , reducer ) ;
79
+ export const userSettings = persistReducer ( { key : " settings" , storage, transforms : [ filterInitialValuesTransform ] } , reducer ) ;
0 commit comments