@@ -11,6 +11,8 @@ const baseUrl = `${EnvironmentConfig.API.V5}/projects`
11
11
12
12
export type ProjectsResponse = SWRResponse < Project [ ] , Project [ ] >
13
13
14
+ const sleep = ( ms : number ) : Promise < ( ) => void > => new Promise ( resolve => { setTimeout ( resolve , ms ) } )
15
+
14
16
/**
15
17
* Custom hook to fetch and manage projects data.
16
18
*
@@ -24,13 +26,26 @@ export const useProjects = (search?: string, config?: {isPaused?: () => boolean,
24
26
const params = { name : search , ...config ?. filter }
25
27
const url = buildUrl ( baseUrl , params )
26
28
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
34
49
}
35
50
36
51
return xhrGetAsync < Project [ ] > ( url )
0 commit comments