Skip to content

Commit

Permalink
Remember the most recently selected namespace (kubeflow#6663)
Browse files Browse the repository at this point in the history
* Remember the most recently selected namespace
This way, Centraldashboard restores the namespace the user visited last

* Include user and namespace in localstorage's key
Remembering the most recently selected ns now works in multi-user envs

* Changed localstorage key to /centraldashboard/selectedNamespace/<user>
  • Loading branch information
TobiasGoerke authored and maroroman committed Feb 7, 2023
1 parent a4c2a8d commit ec3c616
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
9 changes: 9 additions & 0 deletions components/centraldashboard/public/components/main-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ export class MainPage extends utilitiesMixin(PolymerElement) {
_namespaceChanged(namespace) {
// update namespaced menu item when namespace is changed
// by namespace selector

if (namespace) {
// Save the user's choice so we are able to restore it,
// when re-loading the page without a queryParam
const localStorageKey = "/centraldashboard/selectedNamespace/" +
(this.user && "." + this.user || "");
localStorage.setItem(localStorageKey, namespace);
}

if (this.namespacedItemTemplete &&
this.namespacedItemTemplete.includes('{ns}')) {
this.set('subRouteData.path',
Expand Down
3 changes: 2 additions & 1 deletion components/centraldashboard/public/components/main-page.pug
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ app-drawer-layout.flex(narrow='{{narrowMode}}',
query-params='{{queryParams}}', route='{{route}}',
namespaces='[[namespaces]]', selected='{{namespace}}',
hides, hidden$='[[hideNamespaces]]'
all-namespaces='[[allNamespaces]]')
all-namespaces='[[allNamespaces]]',
user='[[user]]')
footer#User-Badge
a(target="_top", href="/logout")
iron-icon.icon(icon='kubeflow:logout' title="Logout")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class NamespaceSelector extends PolymerElement {
notify: true,
},
allNamespaces: {type: Boolean, value: false},
user: String,
selectedNamespaceIsOwned: {
type: Boolean,
readOnly: true,
Expand Down Expand Up @@ -196,6 +197,18 @@ export class NamespaceSelector extends PolymerElement {
* @return {string}
*/
getDefaultNamespace() {
// Restore the user's previous namespace choice
const localStorageKey = "/centraldashboard/selectedNamespace/" +
(this.user && "." + this.user || "");
const previousNamespaceName = localStorage.getItem(localStorageKey);
if (previousNamespaceName) {
const previousNamespace = this.namespaces.find(
(n) => n.namespace === previousNamespaceName);
if (previousNamespace) {
return previousNamespace;
}
}

return this.namespaces.find(
(n) => n.role == 'owner');
}
Expand Down

0 comments on commit ec3c616

Please sign in to comment.