Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadOrder.ts

120 lines4.5 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import { planPinnedMove } from "@t3tools/client-runtime/state/thread-sort";
3import type { EnvironmentId } from "@t3tools/contracts";
4​
5type OrderRow = Pick<
6 EnvironmentThreadShell,
7 | "id"
8 | "environmentId"
9 | "pinOrderKey"
10 | "activeOrderKey"
11 | "createdAt"
12 | "unsettledAt"
13 | "pinnedAt"
14>;
15​
16export interface PendingThreadOrder {
17 readonly section: "pinned" | "active";
18 readonly orderedIds: readonly string[];
19 readonly before: ReadonlyMap<string, { readonly key: string | null; readonly anchor: string }>;
20 readonly assignments: ReadonlyMap<string, string>;
21 readonly confirmed: ReadonlySet<string>;
22 readonly commandsComplete: boolean;
24​
25function rowId(row: OrderRow): string {
26 return `${row.environmentId}:${row.id}`;
28​
29function rowOrder(row: OrderRow, section: PendingThreadOrder["section"]) {
30 return {
31 key: (section === "pinned" ? row.pinOrderKey : row.activeOrderKey) ?? null,
32 anchor: section === "pinned" ? (row.pinnedAt ?? "") : (row.unsettledAt ?? row.createdAt),
33 };
35​
36/** Keep every visible row as an anchor, but only offer plans whose key writes
37 * are supported. Menu availability and execution use this same planner. */
38export function createThreadMovePlanner(input: {
39 readonly ordered: readonly OrderRow[];
40 readonly allThreads?: readonly OrderRow[];
41 readonly section: PendingThreadOrder["section"];
42 readonly reorderableEnvironmentIds: ReadonlySet<EnvironmentId>;
43}) {
44 const orderedIds = input.ordered.map(rowId);
45 const keysById = new Map(
46 (input.allThreads ?? input.ordered).map((row) => [
47 rowId(row),
48 rowOrder(row, input.section).key,
49 ]),
50 );
51 const writableIds = new Set(
52 input.ordered
53 .filter((row) => input.reorderableEnvironmentIds.has(row.environmentId))
54 .map(rowId),
55 );
56 return (movedId: string, direction: "up" | "down") => {
57 if (!writableIds.has(movedId)) return null;
58 const assignments = planPinnedMove({ orderedIds, keysById, movedId, direction });
59 return assignments === null ||
60 assignments.length === 0 ||
61 assignments.some((assignment) => !writableIds.has(assignment.id))
62 ? null
63 : assignments;
64 };
66​
67export function createPendingThreadOrder(input: {
68 readonly section: PendingThreadOrder["section"];
69 readonly ordered: readonly OrderRow[];
70 readonly movedId: string;
71 readonly direction: "up" | "down";
72 readonly assignments: readonly { readonly id: string; readonly orderKey: string }[];
73}): PendingThreadOrder {
74 const orderedIds = input.ordered.map(rowId);
75 const from = orderedIds.indexOf(input.movedId);
76 orderedIds.splice(from, 1);
77 orderedIds.splice(from + (input.direction === "up" ? -1 : 1), 0, input.movedId);
78 return {
79 section: input.section,
80 orderedIds,
81 before: new Map(input.ordered.map((row) => [rowId(row), rowOrder(row, input.section)])),
82 assignments: new Map(input.assignments.map(({ id, orderKey }) => [id, orderKey])),
83 confirmed: new Set(),
84 commandsComplete: false,
85 };
87​
88/** Receipts and shell updates arrive independently. Only our own key writes
89 * may pass through the hold; membership and other arrangement changes win. */
90export function reconcilePendingThreadOrder(
91 pending: PendingThreadOrder,
92 ordered: readonly OrderRow[],
93): PendingThreadOrder | null {
94 if (ordered.length !== pending.before.size) return null;
95 const confirmed = new Set(pending.confirmed);
96 for (const row of ordered) {
97 const id = rowId(row);
98 const before = pending.before.get(id);
99 const current = rowOrder(row, pending.section);
100 if (before === undefined || current.anchor !== before.anchor) return null;
101 const assigned = pending.assignments.get(id);
102 if (assigned !== undefined && current.key === assigned) confirmed.add(id);
103 else if (current.key !== before.key || confirmed.has(id)) return null;
104 }
105 if (pending.commandsComplete && confirmed.size === pending.assignments.size) return null;
106 return confirmed.size === pending.confirmed.size ? pending : { ...pending, confirmed };
108​
109/** Apply the full section's pending order after search/environment filtering. */
110export function applyPendingThreadOrder<T extends OrderRow>(
111 rows: readonly T[],
112 section: PendingThreadOrder["section"],
113 pending: PendingThreadOrder | null | undefined,
114): T[] {
115 if (pending == null || pending.section !== section) return [...rows];
116 const rank = new Map(pending.orderedIds.map((id, index) => [id, index]));
117 return [...rows].sort(
118 (left, right) => (rank.get(rowId(left)) ?? Infinity) - (rank.get(rowId(right)) ?? Infinity),
119 );