keycloak-scim/src/components/realm-selector/recent-used.ts
Erik Jan de Wit eb9092116d
added recently used realms to the top (#502)
* added recently used realms to the top

fixing: #396

* fixed add realm button

* moved setting recent to setRealm
cleanup to where realm list is updated
2021-04-08 15:20:35 -04:00

32 lines
812 B
TypeScript

export class RecentUsed {
private readonly MAX_NUM = 3;
private readonly KEY = "recent-used-realms";
private recentUsedRealms: string[];
constructor() {
this.recentUsedRealms = JSON.parse(localStorage.getItem(this.KEY) || "[]");
}
private save() {
this.recentUsedRealms = this.recentUsedRealms.slice(0, this.MAX_NUM);
localStorage.setItem(this.KEY, JSON.stringify(this.recentUsedRealms));
}
clean(existingRealms: string[]) {
this.recentUsedRealms = this.recentUsedRealms.filter((realm) =>
existingRealms.includes(realm)
);
this.save();
}
get used(): string[] {
return this.recentUsedRealms;
}
setRecentUsed(realm: string) {
if (!this.recentUsedRealms.includes(realm)) {
this.recentUsedRealms.unshift(realm);
this.save();
}
}
}