@@ -28,9 +28,8 @@ function useKeySet(newlineSeparatedKeys: string): ReadonlySet<string> {
2828type LayoutInput = Parameters < typeof buildThreadListV2Items > [ 0 ] ;
2929
3030/**
31- * Home's and the thread sidebar's thread list. Its result is one value a screen only reads:
32- * built inline, the list's inputs stayed open to mutation across the screen's other hooks, so
33- * the React Compiler cached none of it and every render of the screen re-rendered every row.
31+ * Shelf callbacks prevent compiler caching across the whole derivation, so each stage keeps
32+ * its own memo boundary to preserve the list's identity during unrelated navigation renders.
3433 */
3534export function useThreadListV2 ( input : {
3635 readonly threads : LayoutInput [ "threads" ] ;
@@ -69,101 +68,167 @@ export function useThreadListV2(input: {
6968 // Up/down menu availability for every card, computed once per section per
7069 // rebuild (see computeThreadMoveAvailability): per-thread planner calls made
7170 // list construction quadratic, and this list rebuilds on every minute tick.
72- const sectionAvailability = ( section : "pinned" | "active" ) =>
73- computeThreadMoveAvailability ( {
74- allThreads : threads ,
75- section,
76- pendingOrder : input . pendingOrder ,
77- reorderableEnvironmentIds :
78- section === "pinned" ? input . pinReorderEnvironmentIds : input . activeReorderEnvironmentIds ,
79- ordered : getThreadListV2OrderedSection ( {
80- threads,
71+ const moveAvailability = useMemo ( ( ) => {
72+ const sectionAvailability = ( section : "pinned" | "active" ) =>
73+ computeThreadMoveAvailability ( {
74+ allThreads : threads ,
8175 section,
8276 pendingOrder : input . pendingOrder ,
83- now : input . now ,
84- settlementEnvironmentIds : input . settlementEnvironmentIds ,
85- snoozeEnvironmentIds : input . snoozeEnvironmentIds ,
86- queuedThreadKeys : input . queuedThreadKeys ,
87- } ) ,
88- } ) ;
89- const moveAvailability = new Map ( [
90- ...sectionAvailability ( "pinned" ) ,
91- ...( workingShelfEnabled ? [ ] : sectionAvailability ( "active" ) ) ,
77+ reorderableEnvironmentIds :
78+ section === "pinned" ? input . pinReorderEnvironmentIds : input . activeReorderEnvironmentIds ,
79+ ordered : getThreadListV2OrderedSection ( {
80+ threads,
81+ section,
82+ pendingOrder : input . pendingOrder ,
83+ now : input . now ,
84+ settlementEnvironmentIds : input . settlementEnvironmentIds ,
85+ snoozeEnvironmentIds : input . snoozeEnvironmentIds ,
86+ queuedThreadKeys : input . queuedThreadKeys ,
87+ } ) ,
88+ } ) ;
89+ return new Map ( [
90+ ...sectionAvailability ( "pinned" ) ,
91+ ...( workingShelfEnabled ? [ ] : sectionAvailability ( "active" ) ) ,
92+ ] ) ;
93+ } , [
94+ threads ,
95+ input . pendingOrder ,
96+ input . pinReorderEnvironmentIds ,
97+ input . activeReorderEnvironmentIds ,
98+ input . now ,
99+ input . settlementEnvironmentIds ,
100+ input . snoozeEnvironmentIds ,
101+ input . queuedThreadKeys ,
102+ workingShelfEnabled ,
92103 ] ) ;
93104 // Older settled threads load in pages; the shelf lists them only as far as they are loaded.
94- const scopes = olderSettledScopes ( input ) ;
105+ const scopes = useMemo (
106+ ( ) =>
107+ olderSettledScopes ( {
108+ environmentId : input . environmentId ,
109+ projectRefs : input . projectRefs ,
110+ settlementEnvironmentIds : input . settlementEnvironmentIds ,
111+ } ) ,
112+ [ input . environmentId , input . projectRefs , input . settlementEnvironmentIds ] ,
113+ ) ;
95114 const olderSettledShelf = useOlderSettledShelf ( {
96115 scopes,
97116 expanded : input . settledShelfExpanded ,
98117 // A search lists only matches, so the shelf would page forever; loaded pages stay.
99118 visibleCount : input . searchQuery . trim ( ) . length === 0 ? input . settledLimit : 0 ,
100119 } ) ;
101- const layout = buildThreadListV2Items ( {
102- pendingOrder : input . pendingOrder ,
103- threads : threads . filter ( ( thread ) => thread . archivedAt === null ) ,
104- environmentId : input . environmentId ,
105- projectRefs : input . projectRefs ,
106- searchQuery : input . searchQuery ,
107- matchedThreadKeys : new Set ( input . searchMatches . keys ( ) ) ,
108- settlementEnvironmentIds : input . settlementEnvironmentIds ,
109- snoozeEnvironmentIds : input . snoozeEnvironmentIds ,
110- queuedThreadKeys : input . queuedThreadKeys ,
111- settledLimit : input . settledLimit ,
112- now : input . now ,
113- workingShelfEnabled,
114- workingShelfExpanded : input . workingShelfExpanded ,
115- inboxReturnAt : inbox . returnedAt ,
116- isWorking : inbox . isWorking ,
117- snoozedShelfExpanded : input . snoozedShelfExpanded ,
118- settledShelfExpanded : input . settledShelfExpanded ,
119- selectedThreadKey : input . selectedThreadKey ,
120- isListedSettled : olderSettledShelf . isListed ,
121- unloadedSettledCount : olderSettledShelf . unloadedCount (
122- threads . filter (
123- ( thread ) =>
124- thread . archivedAt === null &&
125- scopes . some (
126- ( scope ) =>
127- scope . environmentId === thread . environmentId &&
128- ( scope . projectIds === null || scope . projectIds . includes ( thread . projectId ) ) ,
120+ const { isListed, unloadedCount } = olderSettledShelf ;
121+ const layout = useMemo (
122+ ( ) =>
123+ buildThreadListV2Items ( {
124+ pendingOrder : input . pendingOrder ,
125+ threads : threads . filter ( ( thread ) => thread . archivedAt === null ) ,
126+ environmentId : input . environmentId ,
127+ projectRefs : input . projectRefs ,
128+ searchQuery : input . searchQuery ,
129+ matchedThreadKeys : new Set ( input . searchMatches . keys ( ) ) ,
130+ settlementEnvironmentIds : input . settlementEnvironmentIds ,
131+ snoozeEnvironmentIds : input . snoozeEnvironmentIds ,
132+ queuedThreadKeys : input . queuedThreadKeys ,
133+ settledLimit : input . settledLimit ,
134+ now : input . now ,
135+ workingShelfEnabled,
136+ workingShelfExpanded : input . workingShelfExpanded ,
137+ inboxReturnAt : inbox . returnedAt ,
138+ isWorking : inbox . isWorking ,
139+ snoozedShelfExpanded : input . snoozedShelfExpanded ,
140+ settledShelfExpanded : input . settledShelfExpanded ,
141+ selectedThreadKey : input . selectedThreadKey ,
142+ isListedSettled : isListed ,
143+ unloadedSettledCount : unloadedCount (
144+ threads . filter (
145+ ( thread ) =>
146+ thread . archivedAt === null &&
147+ scopes . some (
148+ ( scope ) =>
149+ scope . environmentId === thread . environmentId &&
150+ ( scope . projectIds === null || scope . projectIds . includes ( thread . projectId ) ) ,
151+ ) ,
129152 ) ,
130- ) ,
131- ) ,
132- } ) ;
153+ ) ,
154+ } ) ,
155+ [
156+ input . pendingOrder ,
157+ threads ,
158+ input . environmentId ,
159+ input . projectRefs ,
160+ input . searchQuery ,
161+ input . searchMatches ,
162+ input . settlementEnvironmentIds ,
163+ input . snoozeEnvironmentIds ,
164+ input . queuedThreadKeys ,
165+ input . settledLimit ,
166+ input . now ,
167+ workingShelfEnabled ,
168+ input . workingShelfExpanded ,
169+ inbox . returnedAt ,
170+ inbox . isWorking ,
171+ input . snoozedShelfExpanded ,
172+ input . settledShelfExpanded ,
173+ input . selectedThreadKey ,
174+ isListed ,
175+ unloadedCount ,
176+ scopes ,
177+ ] ,
178+ ) ;
133179 useOlderSettledShelfGrowth ( olderSettledShelf , layout . listedSettledThreads ) ;
134180 const contentOnlyThreadKeys = useKeySet ( [ ...layout . contentOnlyThreadKeys ] . sort ( ) . join ( "\n" ) ) ;
135- const scopedProjectKeys =
136- input . projectRefs == null
137- ? null
138- : new Set ( input . projectRefs . map ( ( ref ) => scopedProjectKey ( ref . environmentId , ref . projectId ) ) ) ;
139- const searchQuery = input . searchQuery . trim ( ) . toLocaleLowerCase ( ) ;
140- const pendingTasks = input . pendingTasks . filter (
141- ( pendingTask ) =>
142- ( input . environmentId === null || pendingTask . environmentId === input . environmentId ) &&
143- ( scopedProjectKeys === null ||
144- scopedProjectKeys . has (
145- scopedProjectKey ( pendingTask . environmentId , pendingTask . projectId ) ,
146- ) ) &&
147- ( searchQuery . length === 0 || pendingTask . title . toLocaleLowerCase ( ) . includes ( searchQuery ) ) ,
181+ const pendingTasks = useMemo ( ( ) => {
182+ const scopedProjectKeys =
183+ input . projectRefs == null
184+ ? null
185+ : new Set (
186+ input . projectRefs . map ( ( ref ) => scopedProjectKey ( ref . environmentId , ref . projectId ) ) ,
187+ ) ;
188+ const searchQuery = input . searchQuery . trim ( ) . toLocaleLowerCase ( ) ;
189+ return input . pendingTasks . filter (
190+ ( pendingTask ) =>
191+ ( input . environmentId === null || pendingTask . environmentId === input . environmentId ) &&
192+ ( scopedProjectKeys === null ||
193+ scopedProjectKeys . has (
194+ scopedProjectKey ( pendingTask . environmentId , pendingTask . projectId ) ,
195+ ) ) &&
196+ ( searchQuery . length === 0 || pendingTask . title . toLocaleLowerCase ( ) . includes ( searchQuery ) ) ,
197+ ) ;
198+ } , [ input . projectRefs , input . searchQuery , input . pendingTasks , input . environmentId ] ) ;
199+ const items = useMemo (
200+ ( ) =>
201+ buildThreadListV2ListItems ( {
202+ items : layout . items ,
203+ pendingTasks,
204+ workingCount : layout . workingCount ,
205+ workingShelfExpanded : input . workingShelfExpanded ,
206+ workingShelfHeaderIndex : layout . workingShelfHeaderIndex ,
207+ snoozedCount : layout . snoozedCount ,
208+ snoozedShelfExpanded : input . snoozedShelfExpanded ,
209+ snoozedShelfHeaderIndex : layout . snoozedShelfHeaderIndex ,
210+ settledCount : layout . settledCount ,
211+ settledShelfExpanded : input . settledShelfExpanded ,
212+ settledShelfHeaderIndex : layout . settledShelfHeaderIndex ,
213+ snoozeLabelNow : `${ input . now . slice ( 0 , 16 ) } :00.000Z` ,
214+ snoozeEnvironmentIds : input . snoozeEnvironmentIds ,
215+ queuedThreadKeys : input . queuedThreadKeys ,
216+ moveAvailability,
217+ shelfPreferencesLoading : ! input . shelfPreferencesLoaded ,
218+ } ) ,
219+ [
220+ layout ,
221+ pendingTasks ,
222+ input . workingShelfExpanded ,
223+ input . snoozedShelfExpanded ,
224+ input . settledShelfExpanded ,
225+ input . now ,
226+ input . snoozeEnvironmentIds ,
227+ input . queuedThreadKeys ,
228+ moveAvailability ,
229+ input . shelfPreferencesLoaded ,
230+ ] ,
148231 ) ;
149- const items = buildThreadListV2ListItems ( {
150- items : layout . items ,
151- pendingTasks,
152- workingCount : layout . workingCount ,
153- workingShelfExpanded : input . workingShelfExpanded ,
154- workingShelfHeaderIndex : layout . workingShelfHeaderIndex ,
155- snoozedCount : layout . snoozedCount ,
156- snoozedShelfExpanded : input . snoozedShelfExpanded ,
157- snoozedShelfHeaderIndex : layout . snoozedShelfHeaderIndex ,
158- settledCount : layout . settledCount ,
159- settledShelfExpanded : input . settledShelfExpanded ,
160- settledShelfHeaderIndex : layout . settledShelfHeaderIndex ,
161- snoozeLabelNow : `${ input . now . slice ( 0 , 16 ) } :00.000Z` ,
162- snoozeEnvironmentIds : input . snoozeEnvironmentIds ,
163- queuedThreadKeys : input . queuedThreadKeys ,
164- moveAvailability,
165- shelfPreferencesLoading : ! input . shelfPreferencesLoaded ,
166- } ) ;
167232 return { layout, items, contentOnlyThreadKeys, olderSettledShelf } ;
168233}
169234
0 commit comments