(newSession: Session)
| 274 | |
| 275 | // Set up event listeners for session updates with targeted updates |
| 276 | const handleSessionCreated = (newSession: Session) => { |
| 277 | |
| 278 | if (!newSession.projectId) { |
| 279 | console.warn('[DraggableProjectTreeView] Session created without projectId, cannot add to tree'); |
| 280 | // Instead of reloading everything, just skip this session |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | // Check if this session belongs to a folder that might not exist yet |
| 285 | if (newSession.folderId) { |
| 286 | const project = projectsWithSessions.find(p => p.id === newSession.projectId); |
| 287 | const folderExists = project?.folders?.some(f => f.id === newSession.folderId); |
| 288 | |
| 289 | if (!folderExists) { |
| 290 | // If the folder doesn't exist in our local state, we need to refresh the data |
| 291 | // This can happen when multiple sessions are created quickly in a new folder |
| 292 | console.log('[DraggableProjectTreeView] Folder not found for new session, reloading project data'); |
| 293 | loadProjectsWithSessions(); |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Add the new session to the appropriate project without reloading everything |
| 299 | setProjectsWithSessions(prevProjects => { |
| 300 | const updatedProjects = prevProjects.map(project => { |
| 301 | if (project.id === newSession.projectId) { |
| 302 | // Add the new session to this project |
| 303 | const updatedProject = { |
| 304 | ...project, |
| 305 | sessions: [...project.sessions, newSession] |
| 306 | }; |
| 307 | return updatedProject; |
| 308 | } |
| 309 | return project; |
| 310 | }); |
| 311 | |
| 312 | // If no project was found, log a warning |
| 313 | if (!updatedProjects.some(p => p.id === newSession.projectId)) { |
| 314 | console.warn('[DraggableProjectTreeView] No matching project found for session projectId:', newSession.projectId); |
| 315 | } |
| 316 | |
| 317 | return updatedProjects; |
| 318 | }); |
| 319 | |
| 320 | // Auto-expand the project that contains the new session (immediate for all sessions) |
| 321 | if (newSession.projectId) { |
| 322 | console.log('[DraggableProjectTreeView] Immediately expanding project:', newSession.projectId); |
| 323 | setExpandedProjects(prev => { |
| 324 | const newSet = new Set([...prev, newSession.projectId!]); |
| 325 | console.log('[DraggableProjectTreeView] Expanded projects now:', Array.from(newSet)); |
| 326 | return newSet; |
| 327 | }); |
| 328 | } |
| 329 | |
| 330 | // If the session has a folderId, auto-expand that folder too (immediate for all sessions) |
| 331 | if (newSession.folderId) { |
| 332 | console.log('[DraggableProjectTreeView] Immediately expanding folder:', newSession.folderId); |
| 333 | setExpandedFolders(prev => { |
nothing calls this directly
no test coverage detected