Skip to content

Commit a44fce4

Browse files
fix(sidebar): preserve child task sessions during root sync (anomalyco#946)
1 parent b60994a commit a44fce4

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

197 KB
Loading
197 KB
Loading

‎packages/app/src/app/app.tsx‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3091,17 +3091,53 @@ export default function App() {
30913091
? allSessions.filter((session) => normalizeDirectoryPath(session.directory) === activeWorkspaceRoot)
30923092
: allSessions;
30933093
const sorted = sortSessionsByActivity(scopedSessions);
3094-
setSidebarSessionsByWorkspaceId((prev) => ({
3095-
...prev,
3096-
[wsId]: sorted.map((s) => ({
3097-
id: s.id,
3098-
title: s.title,
3099-
slug: s.slug,
3100-
parentID: s.parentID,
3101-
time: s.time,
3102-
directory: s.directory,
3103-
})),
3094+
const rootItems: SidebarSessionItem[] = sorted.map((s) => ({
3095+
id: s.id,
3096+
title: s.title,
3097+
slug: s.slug,
3098+
parentID: s.parentID,
3099+
time: s.time,
3100+
directory: s.directory,
31043101
}));
3102+
setSidebarSessionsByWorkspaceId((prev) => {
3103+
const current = prev[wsId] ?? [];
3104+
const hasCurrentChildren = current.some((item) => Boolean(item.parentID?.trim()));
3105+
const incomingAreRootsOnly = rootItems.every((item) => !item.parentID?.trim());
3106+
if (!hasCurrentChildren || !incomingAreRootsOnly) {
3107+
return {
3108+
...prev,
3109+
[wsId]: rootItems,
3110+
};
3111+
}
3112+
3113+
const byId = new Map(current.map((item) => [item.id, item] as const));
3114+
for (const item of rootItems) {
3115+
byId.set(item.id, {
3116+
...(byId.get(item.id) ?? {}),
3117+
...item,
3118+
});
3119+
}
3120+
3121+
const rootIDs = new Set(rootItems.map((item) => item.id));
3122+
const keepChild = (item: SidebarSessionItem, seen = new Set<string>()) => {
3123+
const parentID = item.parentID?.trim() ?? "";
3124+
if (!parentID) return false;
3125+
if (rootIDs.has(parentID)) return true;
3126+
if (seen.has(parentID)) return false;
3127+
const parent = byId.get(parentID);
3128+
if (!parent) return false;
3129+
seen.add(parentID);
3130+
return keepChild(parent, seen);
3131+
};
3132+
3133+
return {
3134+
...prev,
3135+
[wsId]: [
3136+
...rootItems,
3137+
...current.filter((item) => !rootIDs.has(item.id) && keepChild(item)),
3138+
],
3139+
};
3140+
});
31053141
}
31063142
});
31073143

0 commit comments

Comments
 (0)