1- import { useMemo } from 'react' ;
2-
31import type { ApiResult } from 'sentry/api' ;
42import type { Repository } from 'sentry/types/integrations' ;
3+ import parseLinkHeader from 'sentry/utils/parseLinkHeader' ;
54import {
65 fetchDataQuery ,
76 useInfiniteQuery ,
@@ -12,15 +11,18 @@ import useOrganization from 'sentry/utils/useOrganization';
1211
1312type QueryKey = [ url : string , endpointOptions : QueryKeyEndpointOptions ] ;
1413
15- type Props = {
14+ export type UseInfiniteRepositoriesOptions = {
1615 integrationId : string ;
17- term ?: string ;
16+ searchTerm ?: string ;
1817} ;
1918
20- export function useInfiniteRepositories ( { integrationId, term} : Props ) {
19+ export function useInfiniteRepositories ( {
20+ integrationId,
21+ searchTerm,
22+ } : UseInfiniteRepositoriesOptions ) {
2123 const organization = useOrganization ( ) ;
2224
23- const { data , ... rest } = useInfiniteQuery <
25+ return useInfiniteQuery <
2426 ApiResult < Repository [ ] > ,
2527 Error ,
2628 InfiniteData < ApiResult < Repository [ ] > > ,
@@ -32,7 +34,7 @@ export function useInfiniteRepositories({integrationId, term}: Props) {
3234 query : {
3335 integration_id : integrationId || undefined ,
3436 status : 'active' ,
35- query : term || undefined ,
37+ query : searchTerm || undefined ,
3638 } ,
3739 } ,
3840 ] ,
@@ -43,9 +45,7 @@ export function useInfiniteRepositories({integrationId, term}: Props) {
4345 signal,
4446 meta,
4547 } ) : Promise < ApiResult < Repository [ ] > > => {
46- // eslint-disable-next-line no-console
47- console . log ( 'Fetching page with cursor:' , pageParam ) ;
48- const result = await fetchDataQuery ( {
48+ return fetchDataQuery ( {
4949 queryKey : [
5050 url ,
5151 {
@@ -59,84 +59,21 @@ export function useInfiniteRepositories({integrationId, term}: Props) {
5959 signal,
6060 meta,
6161 } ) ;
62-
63- // eslint-disable-next-line no-console
64- console . log ( 'Fetched page, result length:' , ( result as any ) [ 0 ] ?. length ) ;
65- return result as ApiResult < Repository [ ] > ;
6662 } ,
6763 getNextPageParam : _lastPage => {
68- // The /repos/ endpoint uses Link header pagination
6964 const [ , , responseMeta ] = _lastPage ;
70- const linkHeader = responseMeta ?. getResponseHeader ( 'Link' ) ;
71- // eslint-disable-next-line no-console
72- console . log ( 'getNextPageParam - Link header:' , linkHeader ) ;
73- if ( ! linkHeader ) {
74- return undefined ;
75- }
76-
77- // Parse Link header for next page cursor and check if results="true"
78- const nextMatch = linkHeader . match (
79- / < [ ^ > ] * [ ? & ] c u r s o r = ( [ ^ & > ] + ) [ ^ > ] * > ; \s * r e l = " n e x t " ; \s * r e s u l t s = " ( [ ^ " ] + ) " /
80- ) ;
81- if ( ! nextMatch ) {
82- return undefined ;
83- }
84-
85- const nextCursor = nextMatch [ 1 ] ;
86- const hasResults = nextMatch [ 2 ] === 'true' ;
87-
88- // eslint-disable-next-line no-console
89- console . log (
90- 'getNextPageParam - next cursor:' ,
91- nextCursor ,
92- 'hasResults:' ,
93- hasResults
94- ) ;
95-
96- // Only return cursor if there are actually results
97- return hasResults ? nextCursor : undefined ;
65+ const linkHeader = responseMeta ?. getResponseHeader ( 'Link' ) ?? null ;
66+ const links = parseLinkHeader ( linkHeader ) ;
67+ return links . next ?. results ? links . next . cursor : undefined ;
9868 } ,
9969 getPreviousPageParam : _lastPage => {
100- // The /repos/ endpoint uses Link header pagination
10170 const [ , , responseMeta ] = _lastPage ;
102- const linkHeader = responseMeta ?. getResponseHeader ( 'Link' ) ;
103- if ( ! linkHeader ) {
104- return undefined ;
105- }
106-
107- // Parse Link header for previous page cursor and check if results="true"
108- const prevMatch = linkHeader . match (
109- / < [ ^ > ] * [ ? & ] c u r s o r = ( [ ^ & > ] + ) [ ^ > ] * > ; \s * r e l = " p r e v i o u s " ; \s * r e s u l t s = " ( [ ^ " ] + ) " /
110- ) ;
111- if ( ! prevMatch ) {
112- return undefined ;
113- }
114-
115- const prevCursor = prevMatch [ 1 ] ;
116- const hasResults = prevMatch [ 2 ] === 'true' ;
117-
118- // Only return cursor if there are actually results
119- return hasResults ? prevCursor : undefined ;
71+ const linkHeader = responseMeta ?. getResponseHeader ( 'Link' ) ?? null ;
72+ const links = parseLinkHeader ( linkHeader ) ;
73+ return links . previous ?. results ? links . previous . cursor : undefined ;
12074 } ,
12175 initialPageParam : undefined ,
12276 enabled : Boolean ( integrationId ) ,
12377 staleTime : 0 ,
12478 } ) ;
125-
126- const memoizedData = useMemo ( ( ) => {
127- const flattened = data ?. pages ?. flatMap ( ( [ pageData ] ) => pageData ) ?? [ ] ;
128- // eslint-disable-next-line no-console
129- console . log (
130- 'memoizedData - pages:' ,
131- data ?. pages ?. length ,
132- 'total repos:' ,
133- flattened . length
134- ) ;
135- return flattened ;
136- } , [ data ] ) ;
137-
138- return {
139- data : memoizedData ,
140- ...rest ,
141- } ;
14279}
0 commit comments