-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e085841
commit 26d955f
Showing
3 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
packages/compass-preferences-model/src/compass-web-preferences-access.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; | ||
import { Preferences, type PreferencesAccess } from './preferences'; | ||
import type { UserPreferences } from './preferences-schema'; | ||
import { type AllPreferences } from './preferences-schema'; | ||
import { InMemoryStorage } from './preferences-in-memory-storage'; | ||
import { getActiveUser } from './utils'; | ||
|
||
export class CompassWebPreferencesAccess implements PreferencesAccess { | ||
private _preferences: Preferences; | ||
constructor(preferencesOverrides?: Partial<AllPreferences>) { | ||
this._preferences = new Preferences({ | ||
logger: createNoopLogger(), | ||
preferencesStorage: new InMemoryStorage(preferencesOverrides), | ||
}); | ||
} | ||
|
||
savePreferences(_attributes: Partial<UserPreferences>) { | ||
if ( | ||
Object.keys(_attributes).length === 1 && | ||
'optInDataExplorerGenAIFeatures' in _attributes | ||
) { | ||
return Promise.resolve(this._preferences.savePreferences(_attributes)); | ||
} | ||
// do not save any attributes other than the optInDataExplorerGenAIFeatures setting | ||
return Promise.resolve(this._preferences.getPreferences()); | ||
} | ||
|
||
refreshPreferences() { | ||
return Promise.resolve(this._preferences.getPreferences()); | ||
} | ||
|
||
getPreferences() { | ||
return this._preferences.getPreferences(); | ||
} | ||
|
||
ensureDefaultConfigurableUserPreferences() { | ||
return this._preferences.ensureDefaultConfigurableUserPreferences(); | ||
} | ||
|
||
getConfigurableUserPreferences() { | ||
return Promise.resolve(this._preferences.getConfigurableUserPreferences()); | ||
} | ||
|
||
getPreferenceStates() { | ||
return Promise.resolve(this._preferences.getPreferenceStates()); | ||
} | ||
|
||
onPreferenceValueChanged<K extends keyof AllPreferences>( | ||
preferenceName: K, | ||
callback: (value: AllPreferences[K]) => void | ||
) { | ||
return ( | ||
this._preferences?.onPreferencesChanged?.( | ||
(preferences: Partial<AllPreferences>) => { | ||
if (Object.keys(preferences).includes(preferenceName)) { | ||
return callback((preferences as AllPreferences)[preferenceName]); | ||
} | ||
} | ||
) ?? | ||
(() => { | ||
/* no fallback */ | ||
}) | ||
); | ||
} | ||
|
||
createSandbox() { | ||
return Promise.resolve( | ||
new CompassWebPreferencesAccess(this.getPreferences()) | ||
); | ||
} | ||
|
||
getPreferencesUser() { | ||
return getActiveUser(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters