[Glitch] Fix ValidationError when loading many collections at once

Port 2b6b2fcb6f to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
diondiondion
2026-06-07 14:37:47 +02:00
committed by Claire
parent 45a21db621
commit cdc2d91d43
2 changed files with 24 additions and 4 deletions
@@ -27,6 +27,7 @@ import {
createAppSelector,
createDataLoadingThunk,
} from '@/flavours/glitch/store/typed_functions';
import { batchArray } from '@/flavours/glitch/utils/batch_array';
import { inputToHashtag } from '@/flavours/glitch/utils/hashtags';
type QueryStatus = 'idle' | 'loading' | 'error';
@@ -337,10 +338,14 @@ async function importAccountsForPreviewCard(
)
.filter((id): id is string => !!id);
await dispatch(
fetchAccounts({
accountIds: previewAccountIds,
}),
// fetchAccounts can only process up to 40 item ids, so we'll
// batch the list of ids
const batchedAccountIdLists = batchArray(previewAccountIds, 40);
await Promise.allSettled(
batchedAccountIdLists.map((accountIds) =>
dispatch(fetchAccounts({ accountIds })),
),
);
}
@@ -0,0 +1,15 @@
/**
* Splits a long array so that the resulting nested arrays
* never exceed the `maxLength` provided.
* Useful when dealing with endpoints that accept a limited number
* of parameters
*/
export function batchArray<T>(array: T[], maxLength: number) {
const result: T[][] = [];
for (let i = 0; i < array.length; i += maxLength) {
result.push(array.slice(i, i + maxLength));
}
return result;
}