Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2import { useMemo } from "react";
3
4import { scopedProjectKey } from "../../lib/scopedEntities";
5import type { PendingNewTask } from "../../state/use-pending-new-tasks";
6import {
7 buildThreadListV2Items,
8 buildThreadListV2ListItems,
9 getThreadListV2OrderedSection,
10 threadListInboxReturns,
11} from "./threadListV2";
12import { computeThreadMoveAvailability } from "./threadOrder";
13
14/**
15 * Screens' select handlers read the content-only key set; a fresh one per list rebuild gave
16 * every row a new handler whenever any thread shell changed. Keyed by a string, the set keeps
17 * its identity until its keys change.
18 */
19function useKeySet(newlineSeparatedKeys: string): ReadonlySet<string> {
20 return useMemo(
21 () => new Set(newlineSeparatedKeys.length === 0 ? [] : newlineSeparatedKeys.split("\n")),
22 [newlineSeparatedKeys],
23 );
24}
25
26type LayoutInput = Parameters<typeof buildThreadListV2Items>[0];
27
28/**
29 * Home's and the thread sidebar's thread list. Its result is one value a screen only reads:
30 * built inline, the list's inputs stayed open to mutation across the screen's other hooks, so
31 * the React Compiler cached none of it and every render of the screen re-rendered every row.
32 */
33export function useThreadListV2(input: {
34 readonly threads: LayoutInput["threads"];
35 readonly pendingTasks: ReadonlyArray<PendingNewTask>;
36 readonly environmentId: EnvironmentId | null;
37 readonly projectRefs: LayoutInput["projectRefs"];
38 readonly searchQuery: string;
39 /** Threads that match the search by content, keyed like `threadSearchMatchKey`. */
40 readonly searchMatches: ReadonlyMap<string, unknown>;
41 readonly settlementEnvironmentIds: ReadonlySet<EnvironmentId>;
42 readonly snoozeEnvironmentIds: ReadonlySet<EnvironmentId>;
43 readonly pinReorderEnvironmentIds: ReadonlySet<EnvironmentId>;
44 readonly activeReorderEnvironmentIds: ReadonlySet<EnvironmentId>;
45 readonly queuedThreadKeys: ReadonlySet<string>;
46 readonly settledLimit: number;
47 readonly now: string;
48 readonly pendingOrder: LayoutInput["pendingOrder"];
49 readonly shelfPreferencesLoaded: boolean;
50 readonly workingShelfEnabled: boolean;
51 readonly workingShelfExpanded: boolean;
52 readonly snoozedShelfExpanded: boolean;
53 readonly settledShelfExpanded: boolean;
54 readonly selectedThreadKey: string | null;
55}) {
56 const { threads, workingShelfEnabled } = input;
57 // Observed once per change to the threads: that is when a thread can stop working.
58 const inboxReturnAt = useMemo(
59 () => threadListInboxReturns.observe(workingShelfEnabled ? threads : null),
60 [threads, workingShelfEnabled],
61 );
62 // Up/down menu availability for every card, computed once per section per
63 // rebuild (see computeThreadMoveAvailability): per-thread planner calls made
64 // list construction quadratic, and this list rebuilds on every minute tick.
65 const sectionAvailability = (section: "pinned" | "active") =>
66 computeThreadMoveAvailability({
67 allThreads: threads,
68 section,
69 pendingOrder: input.pendingOrder,
70 reorderableEnvironmentIds:
71 section === "pinned" ? input.pinReorderEnvironmentIds : input.activeReorderEnvironmentIds,
72 ordered: getThreadListV2OrderedSection({
73 threads,
74 section,
75 pendingOrder: input.pendingOrder,
76 now: input.now,
77 settlementEnvironmentIds: input.settlementEnvironmentIds,
78 snoozeEnvironmentIds: input.snoozeEnvironmentIds,
79 queuedThreadKeys: input.queuedThreadKeys,
80 }),
81 });
82 const moveAvailability = new Map([
83 ...sectionAvailability("pinned"),
84 ...(workingShelfEnabled ? [] : sectionAvailability("active")),
85 ]);
86 const layout = buildThreadListV2Items({
87 pendingOrder: input.pendingOrder,
88 threads: threads.filter((thread) => thread.archivedAt === null),
89 environmentId: input.environmentId,
90 projectRefs: input.projectRefs,
91 searchQuery: input.searchQuery,
92 matchedThreadKeys: new Set(input.searchMatches.keys()),
93 settlementEnvironmentIds: input.settlementEnvironmentIds,
94 snoozeEnvironmentIds: input.snoozeEnvironmentIds,
95 queuedThreadKeys: input.queuedThreadKeys,
96 settledLimit: input.settledLimit,
97 now: input.now,
98 workingShelfEnabled,
99 workingShelfExpanded: input.workingShelfExpanded,
100 inboxReturnAt,
101 snoozedShelfExpanded: input.snoozedShelfExpanded,
102 settledShelfExpanded: input.settledShelfExpanded,
103 selectedThreadKey: input.selectedThreadKey,
104 });
105 const contentOnlyThreadKeys = useKeySet([...layout.contentOnlyThreadKeys].sort().join("\n"));
106 const scopedProjectKeys =
107 input.projectRefs == null
108 ? null
109 : new Set(input.projectRefs.map((ref) => scopedProjectKey(ref.environmentId, ref.projectId)));
110 const searchQuery = input.searchQuery.trim().toLocaleLowerCase();
111 const pendingTasks = input.pendingTasks.filter(
112 (pendingTask) =>
113 (input.environmentId === null || pendingTask.environmentId === input.environmentId) &&
114 (scopedProjectKeys === null ||
115 scopedProjectKeys.has(
116 scopedProjectKey(pendingTask.environmentId, pendingTask.projectId),
117 )) &&
118 (searchQuery.length === 0 || pendingTask.title.toLocaleLowerCase().includes(searchQuery)),
119 );
120 const items = buildThreadListV2ListItems({
121 items: layout.items,
122 pendingTasks,
123 workingCount: layout.workingCount,
124 workingShelfExpanded: input.workingShelfExpanded,
125 workingShelfHeaderIndex: layout.workingShelfHeaderIndex,
126 snoozedCount: layout.snoozedCount,
127 snoozedShelfExpanded: input.snoozedShelfExpanded,
128 snoozedShelfHeaderIndex: layout.snoozedShelfHeaderIndex,
129 settledCount: layout.settledCount,
130 settledShelfExpanded: input.settledShelfExpanded,
131 settledShelfHeaderIndex: layout.settledShelfHeaderIndex,
132 snoozeLabelNow: `${input.now.slice(0, 16)}:00.000Z`,
133 snoozeEnvironmentIds: input.snoozeEnvironmentIds,
134 queuedThreadKeys: input.queuedThreadKeys,
135 moveAvailability,
136 shelfPreferencesLoading: !input.shelfPreferencesLoaded,
137 });
138 return { layout, items, contentOnlyThreadKeys };
139}