Fix session persistence after page refresh

Root cause: loadManuallyCreatedProjects() was restoring projects with
stale session arrays from localStorage. When loadProjects() tried to
merge with fresh API data, the stale sessions would override.

Fix 1: In loadManuallyCreatedProjects(), reset the sessions array to
empty for each loaded project. This ensures sessions always come from
the API (authoritative source) rather than localStorage.

Fix 2: In createSessionInFolder(), remove the redundant initialize()
call after loadProjects(). initialize() would reload stale localStorage
data, undoing the fresh data fetched by loadProjects().

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
uroma
2026-01-22 14:49:36 +00:00
Unverified
parent 9107b3db85
commit c5dbb6c244
5 changed files with 105 additions and 61 deletions

View File

@@ -51,6 +51,8 @@ class ProjectManager {
/**
* Load manually created projects from localStorage
* CRITICAL FIX: Reset sessions array to avoid stale data from localStorage
* Sessions will be populated by loadProjects() from the API
*/
loadManuallyCreatedProjects() {
try {
@@ -66,8 +68,15 @@ class ProjectManager {
projectsData.forEach(projectData => {
const projectKey = projectData.id.replace('project-', '');
this.projects.set(projectKey, projectData);
console.log('[ProjectManager] Loaded project:', projectData.name, 'with', projectData.sessions.length, 'sessions');
// CRITICAL FIX: Reset sessions to empty array to avoid stale localStorage data
// Sessions will be populated from API by loadProjects()
const sanitizedProject = {
...projectData,
sessions: [], // Reset sessions - will be loaded from API
activeSessionId: null // Reset active session
};
this.projects.set(projectKey, sanitizedProject);
console.log('[ProjectManager] Loaded project:', projectData.name, 'sessions reset to empty (will load from API)');
});
} else {
console.log('[ProjectManager] No manually created projects found in storage');
@@ -533,16 +542,17 @@ class ProjectManager {
const data = await res.json();
if (data.success || data.id) {
// Reload projects and switch to new session
// CRITICAL FIX: Only call loadProjects() - do NOT call initialize()
// initialize() would reload stale data from localStorage
await this.loadProjects();
await this.initialize();
this.renderProjectTabs();
// Find the new session and switch to it
const session = data.session || data;
for (const project of this.projects.values()) {
const foundSession = project.sessions.find(s => s.id === session.id);
if (foundSession) {
this.switchProject(project.id);
await this.switchProject(project.id);
break;
}
}