-
Notifications
You must be signed in to change notification settings - Fork 918
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,17 +52,18 @@ | |
private readonly api: UiSettingsApi; | ||
private readonly defaults: Record<string, PublicUiSettingsParams>; | ||
private cache: Record<string, PublicUiSettingsParams & UserProvidedValues>; | ||
private browserStoredSettings: Record<string, any> = {}; | ||
|
||
constructor(params: UiSettingsClientParams) { | ||
this.api = params.api; | ||
this.defaults = cloneDeep(params.defaults); | ||
this.cache = defaultsDeep({}, this.defaults, cloneDeep(params.initialSettings)); | ||
|
||
if ( | ||
const userControl = | ||
this.cache['theme:enableUserControl']?.userValue ?? | ||
this.cache['theme:enableUserControl']?.value | ||
) { | ||
this.cache = defaultsDeep(this.cache, this.getBrowserStoredSettings()); | ||
this.cache['theme:enableUserControl']?.value; | ||
if (userControl) { | ||
this.browserStoredSettings = this.getBrowserStoredSettings(); | ||
} | ||
|
||
params.done$.subscribe({ | ||
|
@@ -114,6 +115,43 @@ | |
return this.resolveValue(value, type); | ||
} | ||
|
||
getWithBrowserSettings<T = any>( | ||
key: string, | ||
defaultOverride?: T | ||
): { | ||
advancedSettingValue: T | undefined; | ||
browserValue: T | undefined; | ||
defaultValue: T; | ||
} { | ||
const declared = this.isDeclared(key); | ||
|
||
if (!declared && defaultOverride === undefined) { | ||
throw new Error( | ||
`Unexpected \`IUiSettingsClient.getWithBrowserSettings("${key}")\` call on unrecognized configuration setting "${key}".` | ||
); | ||
} | ||
|
||
const type = this.cache[key]?.type; | ||
const advancedSettingValue = this.cache[key]?.userValue; | ||
const browserValue = this.browserStoredSettings[key]?.userValue; | ||
const defaultValue = defaultOverride !== undefined ? defaultOverride : this.cache[key]?.value; | ||
|
||
// Resolve the value based on the type | ||
const resolveValue = (val: any): T => this.resolveValue(val, type); | ||
|
||
const resolvedAdvancedValue = | ||
advancedSettingValue !== undefined ? resolveValue(advancedSettingValue) : undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 resolvedBrowserValue = | ||
browserValue !== undefined ? resolveValue(browserValue) : undefined; | ||
const resolvedDefaultValue = resolveValue(defaultValue); | ||
|
||
return { | ||
advancedSettingValue: resolvedAdvancedValue, | ||
browserValue: resolvedBrowserValue, | ||
defaultValue: resolvedDefaultValue, | ||
}; | ||
} | ||
|
||
get$<T = any>(key: string, defaultOverride?: T) { | ||
return concat( | ||
defer(() => of(this.get(key, defaultOverride))), | ||
|
@@ -230,8 +268,15 @@ | |
|
||
const declared = this.isDeclared(key); | ||
const defaults = this.defaults; | ||
|
||
const oldVal = declared ? this.cache[key].userValue : undefined; | ||
const userControl = | ||
this.cache['theme:enableUserControl']?.userValue ?? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
this.cache['theme:enableUserControl']?.value; | ||
const oldVal = userControl | ||
? this.getBrowserStoredSettings()[key]?.userValue ?? | ||
(declared ? this.cache[key].userValue : undefined) | ||
: declared | ||
? this.cache[key].userValue | ||
: undefined; | ||
|
||
const unchanged = oldVal === newVal; | ||
if (unchanged) { | ||
|
@@ -242,16 +287,15 @@ | |
this.setLocally(key, newVal); | ||
|
||
try { | ||
if ( | ||
this.cache['theme:enableUserControl']?.userValue ?? | ||
this.cache['theme:enableUserControl']?.value | ||
) { | ||
const { settings } = this.cache[key]?.preferBrowserSetting | ||
? this.setBrowserStoredSettings(key, newVal) | ||
: (await this.api.batchSet(key, newVal)) || {}; | ||
this.cache = defaultsDeep({}, defaults, this.getBrowserStoredSettings(), settings); | ||
if (userControl) { | ||
if (this.cache[key]?.preferBrowserSetting) { | ||
this.setBrowserStoredSettings(key, newVal); | ||
} else { | ||
const { settings = {} } = (await this.api.batchSet(key, newVal)) || {}; | ||
this.cache = defaultsDeep({}, this.cache, settings); | ||
} | ||
} else { | ||
const { settings } = (await this.api.batchSet(key, newVal)) || {}; | ||
const { settings = {} } = (await this.api.batchSet(key, newVal)) || {}; | ||
this.cache = defaultsDeep({}, defaults, settings); | ||
} | ||
this.saved$.next({ key, newValue: newVal, oldValue: initialVal }); | ||
|
There was a problem hiding this comment.
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: