@@ -11,6 +11,8 @@ const baseUrl = `${EnvironmentConfig.API.V5}/projects`
1111
1212export type ProjectsResponse = SWRResponse < Project [ ] , Project [ ] >
1313
14+ const sleep = ( ms : number ) : Promise < ( ) => void > => new Promise ( resolve => { setTimeout ( resolve , ms ) } )
15+
1416/**
1517 * Custom hook to fetch and manage projects data.
1618 *
@@ -24,13 +26,26 @@ export const useProjects = (search?: string, config?: {isPaused?: () => boolean,
2426 const params = { name : search , ...config ?. filter }
2527 const url = buildUrl ( baseUrl , params )
2628
27- const fetcher = ( ) : Promise < Project [ ] > => {
28- if ( config ?. filter ?. id && Array . isArray ( config . filter . id ) ) {
29- const chunks = chunk ( config . filter . id , 20 )
30- return Promise . all (
31- chunks . map ( page => xhrGetAsync < Project [ ] > ( buildUrl ( baseUrl , { ...params , id : page } ) ) ) ,
32- )
33- . then ( responses => responses . flat ( ) )
29+ const fetcher = async ( ) : Promise < Project [ ] > => {
30+ const ids = config ?. filter ?. id
31+
32+ if ( Array . isArray ( ids ) ) {
33+ const idChunks = chunk ( ids , 20 )
34+ const allResults : Project [ ] = [ ]
35+
36+ for ( const chunkIds of idChunks ) {
37+ // eslint-disable-next-line no-await-in-loop
38+ const response = await xhrGetAsync < Project [ ] > (
39+ buildUrl ( baseUrl , { ...params , id : chunkIds } ) ,
40+ )
41+ allResults . push ( ...response )
42+
43+ // Rate limit: delay 200ms between calls
44+ // eslint-disable-next-line no-await-in-loop
45+ await sleep ( 200 )
46+ }
47+
48+ return allResults
3449 }
3550
3651 return xhrGetAsync < Project [ ] > ( url )
0 commit comments