@@ -25,6 +25,19 @@ type ItemWithIndex = {
2525 index : number ;
2626} ;
2727
28+ type ResourceItemResult < TResource > = {
29+ index : number ;
30+ id : string ;
31+ item : TResource ;
32+ } ;
33+
34+ type ContinuousPageChainResult < TResource > = {
35+ data : TResource [ ] ;
36+ hasNextPage : boolean ;
37+ hasPreviousPage : boolean ;
38+ resourceItem ?: ResourceItemResult < TResource > ;
39+ } ;
40+
2841/**
2942 * Finds the id and index in sortedItems of the first item in the given page that's present in sortedItems.
3043 */
@@ -163,15 +176,30 @@ function mergeAndSortContinuousPages<TResource>(sortedItems: TResource[], pages:
163176 *
164177 * Note: sortedItems should be sorted in descending order.
165178 */
166- function getContinuousChain < TResource > (
167- sortedItems : TResource [ ] ,
168- pages : Pages ,
169- getID : ( item : TResource ) => string ,
170- id ?: string ,
171- ) : { data : TResource [ ] ; hasNextPage : boolean ; hasPreviousPage : boolean } {
179+ function getContinuousChain < TResource > ( sortedItems : TResource [ ] , pages : Pages , getID : ( item : TResource ) => string , id ?: string ) : ContinuousPageChainResult < TResource > {
180+ // If an id is provided, find the index of the item with that id
181+ let index = - 1 ;
182+
183+ if ( id ) {
184+ index = sortedItems . findIndex ( ( item ) => getID ( item ) === id ) ;
185+ }
186+ const didFindItem = index !== - 1 ;
187+
188+ // Return the found resource item if it exists
189+ let resourceItem : ResourceItemResult < TResource > | undefined ;
190+ if ( didFindItem ) {
191+ const item = sortedItems . at ( index ) ;
192+ if ( item ) {
193+ resourceItem = {
194+ index,
195+ item,
196+ id : getID ( item ) ,
197+ } ;
198+ }
199+ }
200+
172201 if ( pages . length === 0 ) {
173- const dataItem = sortedItems . find ( ( item ) => getID ( item ) === id ) ;
174- return { data : id && ! dataItem ? [ ] : sortedItems , hasNextPage : false , hasPreviousPage : false } ;
202+ return { data : ! ! id && ! didFindItem ? [ ] : sortedItems , hasNextPage : false , hasPreviousPage : false , resourceItem} ;
175203 }
176204
177205 const pagesWithIndexes = getPagesWithIndexes ( sortedItems , pages , getID ) ;
@@ -184,37 +212,42 @@ function getContinuousChain<TResource>(
184212 lastIndex : 0 ,
185213 } ;
186214
215+ // If we found an item with the resource id, we want link to the specific page with the item
187216 if ( id ) {
188- const index = sortedItems . findIndex ( ( item ) => getID ( item ) === id ) ;
189-
190- // If we are linking to an action that doesn't exist in Onyx, return an empty array
191- if ( index === - 1 ) {
192- return { data : [ ] , hasNextPage : false , hasPreviousPage : false } ;
217+ // If we are searching for an item with a specific resource id and
218+ // we are linking to an action that doesn't exist in Onyx, return an empty array
219+ if ( ! didFindItem ) {
220+ return { data : [ ] , hasNextPage : false , hasPreviousPage : false , resourceItem} ;
193221 }
194222
195223 const linkedPage = pagesWithIndexes . find ( ( pageIndex ) => index >= pageIndex . firstIndex && index <= pageIndex . lastIndex ) ;
196224
197- const item = sortedItems . at ( index ) ;
198225 // If we are linked to an action in a gap return it by itself
199- if ( ! linkedPage && item ) {
200- return { data : [ item ] , hasNextPage : false , hasPreviousPage : false } ;
226+ if ( ! linkedPage && resourceItem ) {
227+ return { data : [ resourceItem . item ] , hasNextPage : false , hasPreviousPage : false , resourceItem } ;
201228 }
202229
203230 if ( linkedPage ) {
204231 page = linkedPage ;
205232 }
206233 } else {
234+ // If we did not find an item with the resource id, we want to link to the first page
207235 const pageAtIndex0 = pagesWithIndexes . at ( 0 ) ;
208236 if ( pageAtIndex0 ) {
209237 page = pageAtIndex0 ;
210238 }
211239 }
212240
213241 if ( ! page ) {
214- return { data : sortedItems , hasNextPage : false , hasPreviousPage : false } ;
242+ return { data : sortedItems , hasNextPage : false , hasPreviousPage : false , resourceItem } ;
215243 }
216244
217- return { data : sortedItems . slice ( page . firstIndex , page . lastIndex + 1 ) , hasNextPage : page . lastID !== CONST . PAGINATION_END_ID , hasPreviousPage : page . firstID !== CONST . PAGINATION_START_ID } ;
245+ return {
246+ data : sortedItems . slice ( page . firstIndex , page . lastIndex + 1 ) ,
247+ hasNextPage : page . lastID !== CONST . PAGINATION_END_ID ,
248+ hasPreviousPage : page . firstID !== CONST . PAGINATION_START_ID ,
249+ resourceItem,
250+ } ;
218251}
219252
220253export default { mergeAndSortContinuousPages, getContinuousChain} ;
0 commit comments