Skip to content

Commit 5566cdb

Browse files
authored
🤖 fix: use functional update for project expand/collapse state (#1017)
Fixes project expand/collapse bug where clicking one project could affect another project's state. The `toggleProject` function had a stale closure issue - it read from `expandedProjects` which was a new Set recreated on every render. When clicking rapidly, subsequent clicks could use stale state from an earlier render. Changed to use functional update pattern with `setExpandedProjectsArray` to always get the current state directly from React. --- _Generated with `mux`_
1 parent dc6e5b1 commit 5566cdb

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/browser/components/ProjectSidebar.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ const ProjectSidebarInner: React.FC<ProjectSidebarProps> = ({
231231
const expandedProjects = new Set(
232232
Array.isArray(expandedProjectsArray) ? expandedProjectsArray : []
233233
);
234-
const setExpandedProjects = (projects: Set<string>) => {
235-
setExpandedProjectsArray(Array.from(projects));
236-
};
237234

238235
// Track which projects have old workspaces expanded (per-project, per-tier)
239236
// Key format: `${projectPath}:${tierIndex}` where tierIndex is 0, 1, 2 for 1/7/30 days
@@ -263,15 +260,21 @@ const ProjectSidebarInner: React.FC<ProjectSidebarProps> = ({
263260
return PlatformPaths.getProjectName(path);
264261
};
265262

266-
const toggleProject = (projectPath: string) => {
267-
const newExpanded = new Set(expandedProjects);
268-
if (newExpanded.has(projectPath)) {
269-
newExpanded.delete(projectPath);
270-
} else {
271-
newExpanded.add(projectPath);
272-
}
273-
setExpandedProjects(newExpanded);
274-
};
263+
// Use functional update to avoid stale closure issues when clicking rapidly
264+
const toggleProject = useCallback(
265+
(projectPath: string) => {
266+
setExpandedProjectsArray((prev) => {
267+
const prevSet = new Set(Array.isArray(prev) ? prev : []);
268+
if (prevSet.has(projectPath)) {
269+
prevSet.delete(projectPath);
270+
} else {
271+
prevSet.add(projectPath);
272+
}
273+
return Array.from(prevSet);
274+
});
275+
},
276+
[setExpandedProjectsArray]
277+
);
275278

276279
const toggleOldWorkspaces = (projectPath: string, tierIndex: number) => {
277280
const key = `${projectPath}:${tierIndex}`;

0 commit comments

Comments
 (0)