Skip to content

Commit 1052255

Browse files
fix(threads): reserve hidden thread order keys
1 parent 1498abc commit 1052255

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎packages/client-runtime/src/state/threadSort.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,44 @@ describe("sortThreads", () => {
112112
});
113113
});
114114

115+
describe("planPinnedReorder with hidden rows", () => {
116+
it("keeps hidden slots available when inserting between visible neighbors", () => {
117+
const midpoint = pinOrderKeyBetween("f", "t")!;
118+
const keysById = new Map([
119+
["a", "f"],
120+
["b", "t"],
121+
["moved", "z"],
122+
["snoozed", midpoint],
123+
]);
124+
const assignments = planPinnedReorder({
125+
orderedIds: ["a", "moved", "b"],
126+
keysById,
127+
movedId: "moved",
128+
});
129+
expect(assignments).toHaveLength(1);
130+
const key = assignments[0]!.orderKey;
131+
expect(key > "f" && key < "t").toBe(true);
132+
expect(key).not.toBe(midpoint);
133+
expect(assignments[0]!.id).toBe("moved");
134+
});
135+
136+
it("materializes keyless rows without overwriting hidden slots", () => {
137+
const reserved = generateSpreadPinOrderKeys(6);
138+
const keysById = new Map<string, string | null>([
139+
["a", null],
140+
["b", null],
141+
["c", null],
142+
...reserved.map((key, i) => [`hidden-${i}`, key] as const),
143+
]);
144+
const assignments = planPinnedReorder({ orderedIds: ["c", "a", "b"], keysById, movedId: "c" });
145+
expect(assignments.map(({ id }) => id)).toEqual(["c", "a", "b"]);
146+
const keys = assignments.map(({ orderKey }) => orderKey);
147+
expect(keys).toEqual([...keys].sort());
148+
expect(new Set(keys).size).toBe(3);
149+
expect(keys.every((key) => !reserved.includes(key))).toBe(true);
150+
});
151+
});
152+
115153
describe("planPinnedMove", () => {
116154
it("moves a thread up with a single key write", () => {
117155
const assignments = planPinnedMove({

‎packages/client-runtime/src/state/threadSort.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,15 @@ export function generateSpreadPinOrderKeys(count: number): string[] {
241241
export function planPinnedReorder(input: {
242242
/** Thread ids in the desired visual order (after the move). */
243243
readonly orderedIds: readonly string[];
244+
/** Include retained keys from hidden rows; only orderedIds receive writes. */
244245
readonly keysById: ReadonlyMap<string, string | null | undefined>;
245246
readonly movedId: string;
246247
}): ReadonlyArray<{ readonly id: string; readonly orderKey: string }> {
247248
const { orderedIds, keysById, movedId } = input;
249+
const visibleIds = new Set(orderedIds);
250+
const reservedKeys = new Set(
251+
[...keysById].flatMap(([id, key]) => (!visibleIds.has(id) && key != null ? [key] : [])),
252+
);
248253
const movedIndex = orderedIds.indexOf(movedId);
249254
if (movedIndex === -1) return [];
250255
const beforeId = movedIndex > 0 ? orderedIds[movedIndex - 1] : null;
@@ -254,11 +259,14 @@ export function planPinnedReorder(input: {
254259
const beforeUsable = beforeId === null || beforeKey != null;
255260
const afterUsable = afterId === null || afterKey != null;
256261
if (beforeUsable && afterUsable) {
257-
const key = pinOrderKeyBetween(beforeKey, afterKey);
262+
let key = pinOrderKeyBetween(beforeKey, afterKey);
263+
while (key !== null && reservedKeys.has(key)) key = pinOrderKeyBetween(key, afterKey);
258264
if (key !== null) return [{ id: movedId, orderKey: key }];
259265
}
260266
// Keyless neighbor (or corrupt keys): rewrite the section in the new order.
261-
const keys = generateSpreadPinOrderKeys(orderedIds.length);
267+
const keys = generateSpreadPinOrderKeys(orderedIds.length + reservedKeys.size)
268+
.filter((key) => !reservedKeys.has(key))
269+
.slice(0, orderedIds.length);
262270
return orderedIds.flatMap((id, index) => {
263271
const key = keys[index]!;
264272
return keysById.get(id) === key ? [] : [{ id, orderKey: key }];

0 commit comments

Comments
 (0)