Skip to content

Commit dd59070

Browse files
shweta2101cyrossignol
authored andcommitted
fix: retain selected project group and workspace per tab on reload
- Init currentProjectGroup synchronously from sessionStorage (was ref(null), causing a race where the picker defaulted to page-1 first group before onMounted ran) - Store and restore the project group name alongside its ID so the picker input field shows the correct name for page-2+ groups (which aren't in the first API response) - Add nameModel (v-model:name) to ProjectGroupPicker and emit it on selection - Switch all dashboard storage from localStorage to sessionStorage so each browser tab maintains its own independent selection that survives reloads
1 parent 01a558a commit dd59070

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

components/ProjectGroupPicker.vue

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
<div class="pg-header">
2020
<span v-if="projectGroups.length > 0" class="pg-count">
2121
<template v-if="totalCount !== null">
22-
Showing {{ projectGroups.length }} of {{ totalCount }} project groups
22+
Showing first {{ projectGroups.length }} of {{ totalCount }} project groups
2323
<span v-if="hasMore && !loading" class="pg-scroll-hint">&#183; Scroll to continue loading</span>
2424
</template>
2525
<template v-else-if="!hasMore">Showing all {{ projectGroups.length }} project group{{ projectGroups.length !== 1 ? 's' : '' }}</template>
2626
<template v-else>
27-
Showing {{ projectGroups.length }} results
27+
Showing first {{ projectGroups.length }} results
2828
<span v-if="!loading" class="pg-scroll-hint">&#183; Scroll to continue loading</span>
2929
</template>
3030
</span>
@@ -69,6 +69,7 @@ const props = withDefaults(defineProps<{ disabled?: boolean }>(), {
6969
})
7070
7171
const model = defineModel({ required: true })
72+
const nameModel = defineModel('name', { default: '' })
7273
const searchText = ref('')
7374
const isOpen = ref(false)
7475
const projectGroups = ref<{ id: string; name: string }[]>([])
@@ -176,6 +177,7 @@ const selectGroup = (id: string) => {
176177
if (pg) {
177178
searchText.value = pg.name
178179
selectedGroupName.value = pg.name
180+
nameModel.value = pg.name
179181
}
180182
}
181183
@@ -269,10 +271,20 @@ onMounted(async () => {
269271
270272
if (projectGroups.value.length > 0) {
271273
const selected = projectGroups.value.find(pg => pg.id === model.value)
272-
const effective = selected ?? projectGroups.value[0]!
273-
model.value = effective.id
274-
searchText.value = effective.name
275-
selectedGroupName.value = effective.name
274+
if (selected) {
275+
searchText.value = selected.name
276+
selectedGroupName.value = selected.name
277+
} else if (model.value && nameModel.value) {
278+
// Group is beyond page 1 — use the cached name for display
279+
searchText.value = nameModel.value
280+
selectedGroupName.value = nameModel.value
281+
} else if (!model.value) {
282+
const first = projectGroups.value[0]!
283+
model.value = first.id
284+
searchText.value = first.name
285+
selectedGroupName.value = first.name
286+
nameModel.value = first.name
287+
}
276288
}
277289
})
278290

pages/dashboard.vue

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<h2 class="visually-hidden">My Workspaces</h2>
55

66
<label for="ws_project_group_picker">Project Group</label>
7-
<project-group-picker v-model="currentProjectGroup" id="ws_project_group_picker" />
7+
<project-group-picker v-model="currentProjectGroup" v-model:name="currentProjectGroupName" id="ws_project_group_picker" />
88

99
<nuxt-link class="btn btn-primary flex-shrink-0" to="/workspace/create">
1010
<app-icon variant="add" size="24" />
@@ -45,20 +45,27 @@
4545

4646
<script lang="ts">
4747
const STORAGE_KEY_PROJECT_GROUP = 'tdei-selected-project-group';
48+
const STORAGE_KEY_PROJECT_GROUP_NAME = 'tdei-selected-project-group-name';
4849
const STORAGE_KEY_WORKSPACE = 'tdei-selected-workspace';
4950
5051
function getLastProjectGroupId(): string | null {
51-
return localStorage.getItem(STORAGE_KEY_PROJECT_GROUP);
52+
return sessionStorage.getItem(STORAGE_KEY_PROJECT_GROUP);
5253
}
5354
function setLastProjectGroupId(id: string) {
54-
localStorage.setItem(STORAGE_KEY_PROJECT_GROUP, id);
55+
sessionStorage.setItem(STORAGE_KEY_PROJECT_GROUP, id);
56+
}
57+
function getLastProjectGroupName(): string | null {
58+
return sessionStorage.getItem(STORAGE_KEY_PROJECT_GROUP_NAME);
59+
}
60+
function setLastProjectGroupName(name: string) {
61+
sessionStorage.setItem(STORAGE_KEY_PROJECT_GROUP_NAME, name);
5562
}
5663
function getLastWorkspaceId(): number | null {
57-
const v = localStorage.getItem(STORAGE_KEY_WORKSPACE);
64+
const v = sessionStorage.getItem(STORAGE_KEY_WORKSPACE);
5865
return v ? Number(v) : null;
5966
}
6067
function setLastWorkspaceId(id: number) {
61-
localStorage.setItem(STORAGE_KEY_WORKSPACE, String(id));
68+
sessionStorage.setItem(STORAGE_KEY_WORKSPACE, String(id));
6269
}
6370
</script>
6471

@@ -71,7 +78,8 @@ const route = useRoute();
7178
const workspaces = (await workspacesClient.getMyWorkspaces()).sort(compareWorkspaceCreatedAtDesc);
7279
const workspacesByProjectGroup = Map.groupBy(workspaces, w => w.tdeiProjectGroupId);
7380
74-
const currentProjectGroup = ref(null);
81+
const currentProjectGroup = ref(getLastProjectGroupId());
82+
const currentProjectGroupName = ref(getLastProjectGroupName());
7583
const currentWorkspace = ref({});
7684
const currentWorkspaces = computed(() => workspacesByProjectGroup.get(currentProjectGroup.value));
7785
@@ -84,6 +92,7 @@ for (const w of workspaces) {
8492
onMounted(() => {
8593
watch(currentWorkspace, (val) => { if (val?.id) setLastWorkspaceId(val.id) });
8694
watch(currentProjectGroup, (val) => { if (val) setLastProjectGroupId(val) });
95+
watch(currentProjectGroupName, (val) => { if (val) setLastProjectGroupName(val) });
8796
watch(currentWorkspaces, onCurrentWorkspacesChange);
8897
8998
autoSelectPreferredView();
@@ -113,10 +122,7 @@ function autoSelectPreferredView() {
113122
}
114123
}
115124
116-
const lastProjectGroupId = getLastProjectGroupId();
117-
if (lastProjectGroupId) {
118-
currentProjectGroup.value = lastProjectGroupId;
119-
}
125+
// currentProjectGroup is already initialized from localStorage synchronously
120126
}
121127
122128
async function onCurrentWorkspacesChange(val) {

0 commit comments

Comments
 (0)