Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/apps/copilots/src/services/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const baseUrl = `${EnvironmentConfig.API.V5}/projects`

export type ProjectsResponse = SWRResponse<Project[], Project[]>

const sleep = (ms: number): Promise<()=> void> => new Promise(resolve => { setTimeout(resolve, ms) })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of the sleep function is incorrect. It should be Promise<void> instead of Promise<()=> void>. The function resolves without returning any value, so the correct type should reflect that.


/**
* Custom hook to fetch and manage projects data.
*
Expand All @@ -24,13 +26,26 @@ export const useProjects = (search?: string, config?: {isPaused?: () => boolean,
const params = { name: search, ...config?.filter }
const url = buildUrl(baseUrl, params)

const fetcher = (): Promise<Project[]> => {
if (config?.filter?.id && Array.isArray(config.filter.id)) {
const chunks = chunk(config.filter.id, 20)
return Promise.all(
chunks.map(page => xhrGetAsync<Project[]>(buildUrl(baseUrl, { ...params, id: page }))),
)
.then(responses => responses.flat())
const fetcher = async (): Promise<Project[]> => {
const ids = config?.filter?.id

if (Array.isArray(ids)) {
const idChunks = chunk(ids, 20)
const allResults: Project[] = []

for (const chunkIds of idChunks) {
// eslint-disable-next-line no-await-in-loop
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing the // eslint-disable-next-line no-await-in-loop comment and refactoring the code to avoid using await inside a loop. This can be achieved by using Promise.all with map to handle asynchronous operations concurrently, which can improve performance.

const response = await xhrGetAsync<Project[]>(
buildUrl(baseUrl, { ...params, id: chunkIds }),
)
allResults.push(...response)

// Rate limit: delay 200ms between calls
// eslint-disable-next-line no-await-in-loop
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The // eslint-disable-next-line no-await-in-loop comment indicates a potential issue with using await inside a loop. Consider refactoring the code to avoid this pattern, possibly by using Promise.all to handle asynchronous operations concurrently.

await sleep(200)
}

return allResults
}

return xhrGetAsync<Project[]>(url)
Expand Down