You've already forked AstralRinth
forked from didirus/AstralRinth
Add segmentation to reports list to fix it (#3772)
This commit is contained in:
26
apps/frontend/src/utils/fetch-helpers.ts
Normal file
26
apps/frontend/src/utils/fetch-helpers.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
function segmentData<T>(data: T[], segmentSize: number): T[][] {
|
||||
return data.reduce((acc: T[][], curr, index) => {
|
||||
const segment = Math.floor(index / segmentSize);
|
||||
|
||||
if (!acc[segment]) {
|
||||
acc[segment] = [];
|
||||
}
|
||||
acc[segment].push(curr);
|
||||
return acc;
|
||||
}, []);
|
||||
}
|
||||
|
||||
export function fetchSegmented<T>(
|
||||
data: T[],
|
||||
createUrl: (ids: T[]) => string,
|
||||
options = {},
|
||||
segmentSize = 800,
|
||||
): Promise<any> {
|
||||
return Promise.all(
|
||||
segmentData(data, segmentSize).map((ids) => useBaseFetch(createUrl(ids), options)),
|
||||
).then((results) => results.flat());
|
||||
}
|
||||
|
||||
export function asEncodedJsonArray<T>(data: T[]): string {
|
||||
return encodeURIComponent(JSON.stringify(data));
|
||||
}
|
||||
Reference in New Issue
Block a user