Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/client-core/src/sidebar/projectThreadGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,24 @@ function buildSortedItems(
compareThreads: ThreadComparator,
groupEnvironmentThreads: boolean,
draftThreadIds: ReadonlySet<string>,
respectSections = false,
): ProjectThreadItem[] {
if (groupEnvironmentThreads && respectSections) {
const nodesBySectionId = new Map<string | null, ProjectThreadNode[]>();
for (const node of nodes) {
const sectionId = node.thread.sectionId;
const bucket = nodesBySectionId.get(sectionId);
if (bucket) {
bucket.push(node);
} else {
nodesBySectionId.set(sectionId, [node]);
}
}
return [...nodesBySectionId.values()].flatMap((sectionNodes) =>
buildSortedItems(sectionNodes, compareThreads, true, draftThreadIds),
);
}

if (!groupEnvironmentThreads) {
nodes.sort((left, right) => compareThreads(left.thread, right.thread));
return nodes.map(buildThreadItem);
Expand Down Expand Up @@ -353,6 +370,7 @@ function buildThreadTreeItems(
compareThreads: ThreadComparator,
groupEnvironmentThreads: boolean,
draftThreadIds: ReadonlySet<string>,
respectSections = false,
): ProjectThreadItem[] {
const projectThreads = allThreads.filter(isSidebarProjectThread);
const projectThreadIds = new Set(projectThreads.map((thread) => thread.id));
Expand Down Expand Up @@ -413,6 +431,7 @@ function buildThreadTreeItems(
compareThreads,
groupEnvironmentThreads,
draftThreadIds,
respectSections,
);
}

Expand All @@ -438,11 +457,12 @@ export function buildSectionThreadList(
groupEnvironmentThreads = false,
): ProjectThreadItem[] {
return bucketIntoSections(
buildChronologicalThreadList(
buildThreadTreeItems(
allThreads,
compareThreads,
draftThreadIds,
groupEnvironmentThreads,
draftThreadIds,
true,
),
CHRONOLOGICAL_CONTAINER_ID,
compareThreads,
Expand Down
97 changes: 97 additions & 0 deletions packages/client-core/test/projectThreadGroups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,103 @@ describe("worktree grouping preference", () => {
]);
});

it.each([false, true])(
"respects sections with environment grouping %s",
(groupEnvironmentThreads) => {
const threads = [
...worktreeSiblings,
createThread({
id: "later",
environmentId: "env_wt",
environmentIsWorktree: true,
sectionId: "sec_later",
createdAt: 30,
}),
createThread({
id: "loose",
environmentId: "env_wt",
environmentIsWorktree: true,
createdAt: 40,
}),
];
const items = buildSectionThreadList(
threads,
compareStandardThreads,
[...sections, { id: "sec_later", name: "Later" }],
new Set(),
groupEnvironmentThreads,
);
expect(summarizeItems(items)).toEqual([
{
section: "chronological::sec_work",
name: "Work",
items: groupEnvironmentThreads
? [{ env: "env_wt", threads: ["wt-b", "wt-a"] }]
: ["wt-b", "wt-a"],
},
{
section: "chronological::sec_later",
name: "Later",
items: ["later"],
},
"loose",
]);
expect(summarizeItems(buildProjectThreadGroups(threads))).toEqual([
{ env: "env_wt", threads: ["loose", "later", "wt-b", "wt-a"] },
]);
},
);

it("keeps separate environment groups and their descendants in each section", () => {
const threads = [
...worktreeSiblings,
createThread({
id: "later-a",
environmentId: "env_wt",
environmentIsWorktree: true,
sectionId: "sec_later",
createdAt: 30,
}),
createThread({
id: "later-b",
environmentId: "env_wt",
environmentIsWorktree: true,
sectionId: "sec_later",
createdAt: 40,
}),
createThread({ id: "child", parentThreadId: "wt-a", createdAt: 50 }),
];
const items = buildSectionThreadList(
threads,
compareStandardThreads,
sections,
new Set(),
true,
);
expect(summarizeItems(items)).toEqual([
{
section: "chronological::sec_work",
name: "Work",
items: [
{
env: "env_wt",
threads: ["wt-b", { id: "wt-a", children: ["child"] }],
},
],
},
{
section: "chronological::sec_later",
name: "Section",
items: [{ env: "env_wt", threads: ["later-b", "later-a"] }],
},
]);
expect(
items.map((item) =>
item.kind === "section" ? item.group.threadCount : 0,
),
).toEqual([3, 2]);
});

it("keeps worktree siblings flat inside a section when disabled", () => {
const items = buildSectionThreadList(
worktreeSiblings,
Expand Down
Loading