feat(settings): enhance settings management with generic setter and display hel… (#6202)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
Ali Al Jufairi
2025-08-19 11:28:45 +09:00
committed by GitHub
parent 36ea986cfe
commit 92bb4624c4
5 changed files with 546 additions and 219 deletions

View File

@@ -392,7 +392,7 @@ describe('SettingsUtils', () => {
new Set(),
updatedPendingSettings,
);
expect(displayValue).toBe('true*'); // Should show true with * indicating change
expect(displayValue).toBe('true'); // Should show true (no * since value matches default)
// Test that modified settings also show the * indicator
const modifiedSettings = new Set([key]);
@@ -602,7 +602,7 @@ describe('SettingsUtils', () => {
mergedSettings,
modifiedSettings,
);
expect(result).toBe('false'); // matches default, no *
expect(result).toBe('false*');
});
it('should show default value when setting is not in scope', () => {

View File

@@ -91,7 +91,10 @@ export function getRestartRequiredSettings(): string[] {
/**
* Recursively gets a value from a nested object using a key path array.
*/
function getNestedValue(obj: Record<string, unknown>, path: string[]): unknown {
export function getNestedValue(
obj: Record<string, unknown>,
path: string[],
): unknown {
const [first, ...rest] = path;
if (!first || !(first in obj)) {
return undefined;
@@ -332,6 +335,20 @@ export function setPendingSettingValue(
return newSettings;
}
/**
* Generic setter: Set a setting value (boolean, number, string, etc.) in the pending settings
*/
export function setPendingSettingValueAny(
key: string,
value: unknown,
pendingSettings: Settings,
): Settings {
const path = key.split('.');
const newSettings = structuredClone(pendingSettings);
setNestedValue(newSettings, path, value);
return newSettings;
}
/**
* Check if any modified settings require a restart
*/
@@ -382,11 +399,9 @@ export function saveModifiedSettings(
// We need to set the whole parent object.
const [parentKey] = path;
if (parentKey) {
// Ensure value is a boolean for setPendingSettingValue
const booleanValue = typeof value === 'boolean' ? value : false;
const newParentValue = setPendingSettingValue(
const newParentValue = setPendingSettingValueAny(
settingKey,
booleanValue,
value,
loadedSettings.forScope(scope).settings,
)[parentKey as keyof Settings];
@@ -431,11 +446,12 @@ export function getDisplayValue(
const isChangedFromDefault =
typeof defaultValue === 'boolean' ? value !== defaultValue : value === true;
const isInModifiedSettings = modifiedSettings.has(key);
const hasPendingChanges =
pendingSettings && settingExistsInScope(key, pendingSettings);
// Add * indicator when value differs from default, is in modified settings, or has pending changes
if (isChangedFromDefault || isInModifiedSettings || hasPendingChanges) {
// Mark as modified if setting exists in current scope OR is in modified settings
if (settingExistsInScope(key, settings) || isInModifiedSettings) {
return `${valueString}*`; // * indicates setting is set in current scope
}
if (isChangedFromDefault || isInModifiedSettings) {
return `${valueString}*`; // * indicates changed from default value
}