Skip to content
Merged
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
14 changes: 9 additions & 5 deletions apps/web/src/components/ChatView.move-thread.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,10 @@ describe("ChatView (full app): moving a thread to another computer", () => {
title: titles.get(thread.id)!,
parentThreadId: THREAD_ID,
attachedToParent: true,
// Review was started first, though this app hears of it second.
...(thread.id === reviewId
? { createdAt: new Date(Date.parse(thread.createdAt) - 60_000).toISOString() }
: {}),
}
: thread,
),
Expand Down Expand Up @@ -2149,12 +2153,12 @@ describe("ChatView (full app): moving a thread to another computer", () => {
const rows = Array.from(
document.querySelectorAll<HTMLElement>('[data-testid^="move-threads-row-"]'),
).map((row) => row.dataset.testid);
// The two children are listed in the order this app received them,
// which two streams race to set, so only their place under it is fixed.
expect(rows[0]).toBe(`move-threads-row-${THREAD_ID}`);
expect(rows.slice(1).toSorted()).toEqual([
`move-threads-row-${fixTestsId}`,
// Its children oldest first, as the sidebar lists them, whatever order
// this app received them in.
expect(rows).toEqual([
`move-threads-row-${THREAD_ID}`,
`move-threads-row-${reviewId}`,
`move-threads-row-${fixTestsId}`,
]);
for (const id of [THREAD_ID, fixTestsId, reviewId]) {
await expect.element(dialog.getByTestId(`move-threads-tick-${id}`)).toBeChecked();
Expand Down
8 changes: 2 additions & 6 deletions apps/web/src/components/Sidebar.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getThreadInFlightStatus,
getThreadSortTimestamp,
sortThreads,
compareFamilyChildren,
toSortableTimestamp,
type ThreadSortInput,
} from "../lib/threadSort";
Expand Down Expand Up @@ -1832,12 +1833,7 @@ export function countThreadAttention(
export function sortFamilyChildren<
E extends { readonly thread: { readonly createdAt: string; readonly id: string } },
>(children: readonly E[]): E[] {
return children.toSorted(
(left, right) =>
(toSortableTimestamp(left.thread.createdAt) ?? 0) -
(toSortableTimestamp(right.thread.createdAt) ?? 0) ||
left.thread.id.localeCompare(right.thread.id),
);
return children.toSorted((left, right) => compareFamilyChildren(left.thread, right.thread));
}

/**
Expand Down
15 changes: 15 additions & 0 deletions apps/web/src/lib/threadSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ export function toSortableTimestamp(iso: string | undefined): number | null {
return Number.isFinite(ms) ? ms : null;
}

/**
* A parent's children in family order: oldest first, then by id, never
* re-sorted. The sidebar's families and the Move threads dialog both list
* them this way.
*/
export function compareFamilyChildren(
left: { readonly createdAt?: string | undefined; readonly id: string },
right: { readonly createdAt?: string | undefined; readonly id: string },
): number {
return (
(toSortableTimestamp(left.createdAt) ?? 0) - (toSortableTimestamp(right.createdAt) ?? 0) ||
left.id.localeCompare(right.id)
);
}

function getLatestUserMessageTimestamp(thread: ThreadSortInput): number {
if (thread.latestUserMessageAt) {
return toSortableTimestamp(thread.latestUserMessageAt) ?? Number.NEGATIVE_INFINITY;
Expand Down
15 changes: 15 additions & 0 deletions apps/web/src/threadMove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,21 @@ describe("a family in the Move threads and quit flows (Part 3f, (g))", () => {
]);
});

it("lists a family's children oldest first, whatever order they came in", () => {
const older = { ...other, createdAt: "2026-11-17T09:00:00.000Z" };
const newer = { ...child, createdAt: "2026-11-17T10:00:00.000Z" };
for (const listed of [
[parent, newer, older],
[parent, older, newer],
]) {
expect(groupMoveFamilies(listed).map((row) => row.candidate.threadId)).toEqual([
"p",
"c2",
"c1",
]);
}
});

it("ticks a whole family from any member, and unticks one child or the whole family", () => {
const candidates = [parent, child, other, alone];
const canGo = (id: string) => id !== "c2";
Expand Down
35 changes: 25 additions & 10 deletions apps/web/src/threadMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from "@threadlines/contracts";

import { formatDownloadProgress } from "./lib/formatBytes";
import { compareFamilyChildren } from "./lib/threadSort";
import type { ThreadMoveStatus } from "./types";

/**
Expand Down Expand Up @@ -712,6 +713,8 @@ export interface MoveCandidate<TThreadId extends string = string> {
readonly working: boolean;
/** The thread that started it, while it is in that thread's family. */
readonly parentThreadId?: TThreadId;
/** When it was made: a family's children are listed oldest first. */
readonly createdAt?: string;
}

/**
Expand All @@ -731,6 +734,7 @@ export function listMoveCandidates<
readonly moveStatus?: ThreadMoveStatus | undefined;
readonly parentThreadId?: string | null | undefined;
readonly attachedToParent?: boolean | undefined;
readonly createdAt?: string | undefined;
},
>(
entries: ReadonlyArray<{ readonly thread: TThread; readonly isDone: boolean }>,
Expand All @@ -749,6 +753,7 @@ export function listMoveCandidates<
threadId: thread.id,
title: thread.title,
working: isWorking(thread),
...(thread.createdAt !== undefined ? { createdAt: thread.createdAt } : {}),
...(thread.attachedToParent === true && typeof thread.parentThreadId === "string"
? { parentThreadId: thread.parentThreadId as TThread["id"] }
: {}),
Expand Down Expand Up @@ -902,24 +907,34 @@ function familyRootOf<T extends { readonly threadId: string; readonly parentThre

/**
* The Move threads dialog's rows with each family together: a parent, then
* the children listed with it, in the order given otherwise. A family moves
* its children listed with it, oldest first as the sidebar lists them
* (`compareFamilyChildren`), and the rest in the order given. A family moves
* as one group (`depth` 1 marks a child under its parent).
*/
export function groupMoveFamilies<
T extends { readonly threadId: string; readonly parentThreadId?: string },
T extends {
readonly threadId: string;
readonly parentThreadId?: string;
readonly createdAt?: string;
},
>(candidates: ReadonlyArray<T>): Array<{ readonly candidate: T; readonly depth: 0 | 1 }> {
const grouped: Array<{ readonly candidate: T; readonly depth: 0 | 1 }> = [];
for (const candidate of candidates) {
if (familyRootOf(candidates, candidate) !== candidate.threadId) continue;
grouped.push({ candidate, depth: 0 });
for (const child of candidates) {
if (
child.threadId !== candidate.threadId &&
familyRootOf(candidates, child) === candidate.threadId
) {
grouped.push({ candidate: child, depth: 1 });
}
}
const children = candidates
.filter(
(child) =>
child.threadId !== candidate.threadId &&
familyRootOf(candidates, child) === candidate.threadId,
)
.toSorted((left, right) =>
compareFamilyChildren(
{ createdAt: left.createdAt, id: left.threadId },
{ createdAt: right.createdAt, id: right.threadId },
),
);
grouped.push(...children.map((child) => ({ candidate: child, depth: 1 as const })));
}
return grouped;
}
Expand Down
Loading