-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from alexmt/337-remember-filtering
Issue #337 - remember my resource filtering preferences
- Loading branch information
Showing
4 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
|
||
export interface ViewPreferences { | ||
appDetails: { defaultKindFilter: string[] }; | ||
} | ||
|
||
const VIEW_PREFERENCES_KEY = 'view_preferences'; | ||
|
||
const DEFAULT_PREFERENCES = { | ||
appDetails: { defaultKindFilter: ['Deployment', 'Service', 'Pod', 'StatefulSet', 'Ingress', 'ConfigMap'] }, | ||
}; | ||
|
||
export class ViewPreferencesService { | ||
private preferencesSubj: BehaviorSubject<ViewPreferences>; | ||
|
||
public init() { | ||
if (!this.preferencesSubj) { | ||
this.preferencesSubj = new BehaviorSubject(this.loadPreferences()); | ||
window.addEventListener('storage', () => { | ||
this.preferencesSubj.next(this.loadPreferences()); | ||
}); | ||
} | ||
} | ||
|
||
public getPreferences(): Observable<ViewPreferences> { | ||
return this.preferencesSubj; | ||
} | ||
|
||
public updatePreferences(change: Partial<ViewPreferences>) { | ||
const nextPref = Object.assign({}, this.preferencesSubj.getValue(), change); | ||
window.localStorage.setItem(VIEW_PREFERENCES_KEY, JSON.stringify(nextPref)); | ||
this.preferencesSubj.next(nextPref); | ||
} | ||
|
||
private loadPreferences(): ViewPreferences { | ||
let preferences: ViewPreferences; | ||
const preferencesStr = window.localStorage.getItem(VIEW_PREFERENCES_KEY); | ||
if (preferencesStr) { | ||
try { | ||
preferences = JSON.parse(preferencesStr); | ||
} catch (e) { | ||
preferences = DEFAULT_PREFERENCES; | ||
} | ||
} else { | ||
preferences = DEFAULT_PREFERENCES; | ||
} | ||
return preferences; | ||
} | ||
} |