Skip to content

Commit a124e82

Browse files
committed
feat(web): add manual thread ordering with drag-and-drop in sidebar v2
1 parent 6057b55 commit a124e82

6 files changed

Lines changed: 886 additions & 168 deletions

File tree

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,46 @@ export function orderItemsByPreferredIds<TItem, TId>(input: {
284284
return [...ordered, ...remaining];
285285
}
286286

287+
// Threads order the opposite way from projects: unknown items come *first*, not
288+
// last. A thread the user has never dragged is almost always a brand-new one,
289+
// and a new thread has to surface at the top of the active list where it is
290+
// looked for — burying it under every manually-placed row would make creating a
291+
// thread feel like it did nothing. Projects keep the unknown-last behavior of
292+
// `orderItemsByPreferredIds`, so the two helpers stay separate.
293+
export function orderThreadsByPreferredKeys<TItem>(input: {
294+
items: readonly TItem[];
295+
preferredKeys: readonly string[];
296+
getKey: (item: TItem) => string;
297+
}): TItem[] {
298+
const { getKey, items, preferredKeys } = input;
299+
if (preferredKeys.length === 0) {
300+
return [...items];
301+
}
302+
303+
const indexesByKey = new Map<string, number[]>();
304+
for (const [index, item] of items.entries()) {
305+
const key = getKey(item);
306+
const indexes = indexesByKey.get(key);
307+
if (indexes) {
308+
indexes.push(index);
309+
} else {
310+
indexesByKey.set(key, [index]);
311+
}
312+
}
313+
314+
const emittedIndexes = new Set<number>();
315+
const preferred = preferredKeys.flatMap((key) => {
316+
const index = indexesByKey.get(key)?.find((candidate) => !emittedIndexes.has(candidate));
317+
if (index === undefined) {
318+
return [];
319+
}
320+
emittedIndexes.add(index);
321+
return [items[index]!];
322+
});
323+
const unknown = items.filter((_, index) => !emittedIndexes.has(index));
324+
return [...unknown, ...preferred];
325+
}
326+
287327
export function getVisibleSidebarThreadIds<TThreadId>(
288328
renderedProjects: readonly {
289329
shouldShowThreadPanel?: boolean;

0 commit comments

Comments
 (0)