Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Allow user Theme Selection retain theme selection #7787

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ananzh
Copy link
Member

@ananzh ananzh commented Aug 21, 2024

Description

Add browserStoredSettings and getWithBrowserSettings method to allow the retrieval and resolution of values from the cache and browser settings separately. When user control is enabled, we update the browserStoredSettings without interrupting the cache (advance settings user values).

Issues Resolved

#7689

Screenshot

2024-08-21_10-09-24.mp4

Changelog

  • fix: [BUG] Allow user Theme Selection retain theme selection

Check List

  • All tests pass
    • yarn test:jest
    • yarn test:jest_integration
  • New functionality includes testing.
  • New functionality has been documented.
  • Update CHANGELOG.md
  • Commits are signed per the DCO using --signoff

Copy link
Contributor

❌ Empty Changelog Section

The Changelog section in your PR description is empty. Please add a valid changelog entry or entries. If you did add a changelog entry, check to make sure that it was not accidentally included inside the comment block in the Changelog section.

@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the order of precedence left to right, so this doesn't really change any behavior, right?

it feels like we can just remove this if we're not utilizing it because there shouldn't be a case where there's a browser setting but not an advanced setting, right? Or if an admin doesn't explicitly save something, is there no userValue from advanced settings?

}

const type = this.cache[key].type;
const userValue = this.cache[key].userValue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wouldn't name variable userValue as its really the value from advanced settings, not related to the user

const defaultValue = defaultOverride !== undefined ? defaultOverride : this.cache[key].value;

let value;
if (this.cache['theme:enableUserControl']?.userValue ?? this.cache['theme:enableUserControl']?.value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

know this is a wip, but seems like this should be consumer logic

Comment on lines 139 to 141
value = browserValue ?? userValue ?? defaultValue;
} else {
value = userValue ?? defaultValue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can null be a valid value? if so, don't think we should use ??

Copy link

codecov bot commented Aug 21, 2024

Codecov Report

Attention: Patch coverage is 30.43478% with 32 lines in your changes missing coverage. Please review.

Project coverage is 63.83%. Comparing base (aed03fa) to head (4bf12eb).
Report is 336 commits behind head on main.

Files with missing lines Patch % Lines
...dvanced_settings/public/header_user_theme_menu.tsx 0.00% 20 Missing ⚠️
src/core/public/ui_settings/ui_settings_client.ts 53.84% 7 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7787      +/-   ##
==========================================
+ Coverage   63.80%   63.83%   +0.03%     
==========================================
  Files        3656     3659       +3     
  Lines       81205    81326     +121     
  Branches    12950    12987      +37     
==========================================
+ Hits        51809    51915     +106     
- Misses      26215    26225      +10     
- Partials     3181     3186       +5     
Flag Coverage Δ
Linux_1 ?
Linux_2 55.89% <53.84%> (+0.02%) ⬆️
Linux_3 40.37% <0.00%> (-0.05%) ⬇️
Linux_4 31.29% <0.00%> (+<0.01%) ⬆️
Windows_1 30.17% <0.00%> (+0.07%) ⬆️
Windows_2 55.85% <53.84%> (+0.02%) ⬆️
Windows_3 40.38% <0.00%> (-0.04%) ⬇️
Windows_4 31.29% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ananzh ananzh force-pushed the fix-browser-theme branch from 102d371 to 67cadf3 Compare August 23, 2024 04:58
@ananzh ananzh changed the title Fix Browser theme [BUG] Allow user Theme Selection retain theme selection Aug 23, 2024
Copy link
Contributor

❌ Empty Changelog Section

The Changelog section in your PR description is empty. Please add a valid changelog entry or entries. If you did add a changelog entry, check to make sure that it was not accidentally included inside the comment block in the Changelog section.

@ananzh ananzh added the bug Something isn't working label Aug 23, 2024
@ananzh
Copy link
Member Author

ananzh commented Aug 23, 2024

This can only be backported to 2.x if #5652, which is reverted in #6978, and #7776 are backported.

@ananzh ananzh force-pushed the fix-browser-theme branch from 59183f0 to d2a5fc8 Compare August 23, 2024 17:26
Comment on lines +57 to +74
/**
* Gets the value for a specific uiSetting, considering browser-stored settings and advanced settings.
* This method returns an object containing the resolved value and individual setting values.
*
* @param key - The key of the uiSetting to retrieve
* @param defaultOverride - An optional default value to use if the setting is not declared
* @returns An object containing the resolved value and additional setting information
* @throws Error if the setting is not declared and no defaultOverride is provided
*/
getWithBrowserSettings<T = any>(
key: string,
defaultOverride?: T
): {
advancedSettingValue: T | undefined;
browserValue: T | undefined;
defaultValue: T;
};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callout: I think this is fine for now to avoid increasing scope - i think in the future, we should consider making get() aware of all contexts, and having experiences like advanced settings explicitly call another method or a variant to exclude browser settings in resolution

two suggestions:

  • should we call this getValues() or something more generic where this can provide all levels of values for this setting (not just browser as it also returns default value today)?
  • should this include a resolvedValue or a value that handles what the current active value is so consumers don't have to resolve this themselves?

const resolveValue = (val: any): T => this.resolveValue(val, type);

const resolvedAdvancedValue =
advancedSettingValue !== undefined ? resolveValue(advancedSettingValue) : undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could create a helper function to handle this undefined case and inline these with return


const oldVal = declared ? this.cache[key].userValue : undefined;
const userControl =
this.cache['theme:enableUserControl']?.userValue ??
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ui_settings_client shouldn't be aware of a theme specific setting right?

also, could other settings be null? if so, this probably should be checking that these aren't defined.

Comment on lines +97 to +99
const currentDarkMode = enableUserControl
? result.browserValue ?? result.advancedSettingValue ?? result.defaultValue
: result.advancedSettingValue ?? result.defaultValue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think consumers should be resolving values - think this should come from uisettingsclient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants