Skip to content

Commit e83f81c

Browse files
authored
[NU-2151] provide handling of initial user flags (#8001)
* NU-2151 provide handling of initial user flags * NU-2151 fixes * NU-2151 persist only user settings changed by the user * NU-2151 change default
1 parent 3dfcae4 commit e83f81c

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

designer/client/src/components/graph/node-modal/editors/expression/CustomCompleterAceEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { cx } from "@emotion/css";
22
import { Box, Fade, IconButton, LinearProgress, styled } from "@mui/material";
33
import { isEmpty } from "lodash";
4-
import type { ReactNode} from "react";
4+
import type { ReactNode } from "react";
55
import React, { useCallback, useState } from "react";
66
import type ReactAce from "react-ace/lib/ace";
77

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { persistReducer } from "redux-persist";
1+
import { persistReducer, createTransform } from "redux-persist";
22
import storage from "redux-persist/lib/storage";
33

44
import type { Reducer } from "../actions/reduxTypes";
@@ -20,31 +20,60 @@ type SettingsNames =
2020

2121
export type UserSettings = Partial<Record<SettingsNames, boolean>>;
2222

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;
3562
},
36-
action,
37-
) => {
63+
(outboundState) => outboundState,
64+
);
65+
66+
const reducer: Reducer<UserSettings> = (state = getDefaultUserSettings(), action) => {
3867
switch (action.type) {
3968
case "SET_SETTINGS":
4069
return action.settings;
4170
case "TOGGLE_SETTINGS":
4271
return action.settings.reduce((value, key) => ({ ...value, [key]: !state[key] }), state);
4372
case "RESET_TOOLBARS":
44-
return { ...state, "debug.userSettingsVisible": isDev };
73+
return { ...state, ...getDefaultUserSettings() };
4574
default:
4675
return state;
4776
}
4877
};
4978

50-
export const userSettings = persistReducer({ key: `settings`, storage }, reducer);
79+
export const userSettings = persistReducer({ key: "settings", storage, transforms: [filterInitialValuesTransform] }, reducer);

0 commit comments

Comments
 (0)