Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
Signed-off-by: Anan Zhuang <ananzh@amazon.com>
  • Loading branch information
ananzh committed Aug 21, 2024
1 parent aed03fa commit 102d371
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/core/public/ui_settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface IUiSettingsClient {
* by any plugin then an error is thrown, otherwise reads the default value defined by a plugin.
*/
get: <T = any>(key: string, defaultOverride?: T) => T;
getWithBrowserSettings: <T = any>(key: string, defaultOverride?: T) => T;

/**
* Gets an observable of the current value for a config key, and all updates to that config
Expand Down
35 changes: 33 additions & 2 deletions src/core/public/ui_settings/ui_settings_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* under the License.
*/

import { cloneDeep, defaultsDeep } from 'lodash';
import { cloneDeep, defaultsDeep, merge } from 'lodash';
import { Observable, Subject, concat, defer, of } from 'rxjs';
import { filter, map } from 'rxjs/operators';

Expand All @@ -52,6 +52,7 @@ export class UiSettingsClient implements IUiSettingsClient {
private readonly api: UiSettingsApi;
private readonly defaults: Record<string, PublicUiSettingsParams>;
private cache: Record<string, PublicUiSettingsParams & UserProvidedValues>;
private readonly browserStoredSettings: Record<string, any> = {};

constructor(params: UiSettingsClientParams) {
this.api = params.api;
Expand All @@ -62,7 +63,8 @@ export class UiSettingsClient implements IUiSettingsClient {
this.cache['theme:enableUserControl']?.userValue ??
this.cache['theme:enableUserControl']?.value
) {
this.cache = defaultsDeep(this.cache, this.getBrowserStoredSettings());
this.browserStoredSettings = this.getBrowserStoredSettings();
this.cache = defaultsDeep({}, this.defaults, this.cache, this.browserStoredSettings);

Check warning on line 67 in src/core/public/ui_settings/ui_settings_client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/public/ui_settings/ui_settings_client.ts#L66-L67

Added lines #L66 - L67 were not covered by tests
}

params.done$.subscribe({
Expand Down Expand Up @@ -114,6 +116,34 @@ You can use \`IUiSettingsClient.get("${key}", defaultValue)\`, which will just r
return this.resolveValue(value, type);
}

getWithBrowserSettings<T = any>(key: string, defaultOverride?: T) {
const declared = this.isDeclared(key);

Check warning on line 120 in src/core/public/ui_settings/ui_settings_client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/public/ui_settings/ui_settings_client.ts#L120

Added line #L120 was not covered by tests

if (!declared && defaultOverride !== undefined) {
return defaultOverride;

Check warning on line 123 in src/core/public/ui_settings/ui_settings_client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/public/ui_settings/ui_settings_client.ts#L123

Added line #L123 was not covered by tests
}

if (!declared) {
throw new Error(

Check warning on line 127 in src/core/public/ui_settings/ui_settings_client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/public/ui_settings/ui_settings_client.ts#L127

Added line #L127 was not covered by tests
`Unexpected \`IUiSettingsClient.get("${key}")\` call on unrecognized configuration setting "${key}".`
);
}

const type = this.cache[key].type;
const userValue = this.cache[key].userValue;
const browserValue = this.browserStoredSettings[key]?.userValue;

Check warning on line 134 in src/core/public/ui_settings/ui_settings_client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/public/ui_settings/ui_settings_client.ts#L132-L134

Added lines #L132 - L134 were not covered by tests
const defaultValue = defaultOverride !== undefined ? defaultOverride : this.cache[key].value;

let value;
if (this.cache['theme:enableUserControl']?.userValue ?? this.cache['theme:enableUserControl']?.value) {
value = browserValue ?? userValue ?? defaultValue;
} else {
value = userValue ?? defaultValue;
}

return this.resolveValue(value, type);

Check warning on line 144 in src/core/public/ui_settings/ui_settings_client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/public/ui_settings/ui_settings_client.ts#L144

Added line #L144 was not covered by tests
}

get$<T = any>(key: string, defaultOverride?: T) {
return concat(
defer(() => of(this.get(key, defaultOverride))),
Expand Down Expand Up @@ -250,6 +280,7 @@ You can use \`IUiSettingsClient.get("${key}", defaultValue)\`, which will just r
? this.setBrowserStoredSettings(key, newVal)
: (await this.api.batchSet(key, newVal)) || {};
this.cache = defaultsDeep({}, defaults, this.getBrowserStoredSettings(), settings);
console.log("this.cache");

Check warning on line 283 in src/core/public/ui_settings/ui_settings_client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/public/ui_settings/ui_settings_client.ts#L283

Added line #L283 was not covered by tests
} else {
const { settings } = (await this.api.batchSet(key, newVal)) || {};
this.cache = defaultsDeep({}, defaults, settings);
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/ui_settings/settings/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const getThemeSettings = (): Record<string, UiSettingsParams> => {
defaultMessage: `<p>Switch between the themes used for the current and next versions of OpenSearch Dashboards. A page refresh is required for the setting to be applied.</p><p><a href="{href}">{linkText}</a></p>`,
values: {
href: 'https://forum.opensearch.org/t/feedback-on-dark-mode-experience/15725',
linkText: 'Theme feedback',
linkText: 'Theme feedback1',
},
}),
requiresPageReload: true,
Expand Down
29 changes: 18 additions & 11 deletions src/plugins/advanced_settings/public/header_user_theme_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,28 @@ export const HeaderUserThemeMenu = () => {
const defaultScreenMode = uiSettings.getDefault('theme:darkMode');
const prefersAutomatic =
(window.localStorage.getItem('useBrowserColorScheme') && window.matchMedia) || false;

const [enableUserControl] = useUiSetting$<boolean>('theme:enableUserControl');

Check warning on line 54 in src/plugins/advanced_settings/public/header_user_theme_menu.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/advanced_settings/public/header_user_theme_menu.tsx#L54

Added line #L54 was not covered by tests
const [darkMode, setDarkMode] = useUiSetting$<boolean>('theme:darkMode');
const [themeVersion, setThemeVersion] = useUiSetting$<string>('theme:version');
const [isPopoverOpen, setPopover] = useState(false);
// TODO: improve naming?
const [theme, setTheme] = useState(
themeOptions.find((t) => t.value === themeVersionValueMap[themeVersion])?.value ||
themeVersionValueMap[defaultTheme]
);
const [screenMode, setScreenMode] = useState(
prefersAutomatic
? screenModeOptions[2].value
: darkMode
? screenModeOptions[1].value
: screenModeOptions[0].value
);


const [theme, setTheme] = useState(() => {

Check warning on line 61 in src/plugins/advanced_settings/public/header_user_theme_menu.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/advanced_settings/public/header_user_theme_menu.tsx#L61

Added line #L61 was not covered by tests
const currentTheme = enableUserControl
? uiSettings.getWithBrowserSettings('theme:version')
: uiSettings.get('theme:version');
return themeOptions.find((t) => t.value === themeVersionValueMap[currentTheme])?.value ||
themeVersionValueMap[defaultTheme];
});
const [screenMode, setScreenMode] = useState(() => {

Check warning on line 68 in src/plugins/advanced_settings/public/header_user_theme_menu.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/advanced_settings/public/header_user_theme_menu.tsx#L68

Added line #L68 was not covered by tests
if (prefersAutomatic) return 'automatic';
const currentDarkMode = enableUserControl
? uiSettings.getWithBrowserSettings('theme:darkMode')
: uiSettings.get('theme:darkMode');
return currentDarkMode ? 'dark' : 'light';
});

const legacyAppearance = !uiSettings.get('home:useNewHomePage');

Expand Down

0 comments on commit 102d371

Please sign in to comment.