diff --git a/app/javascript/flavours/glitch/reducers/slices/collections.ts b/app/javascript/flavours/glitch/reducers/slices/collections.ts index 78b0db862d..57d57ef657 100644 --- a/app/javascript/flavours/glitch/reducers/slices/collections.ts +++ b/app/javascript/flavours/glitch/reducers/slices/collections.ts @@ -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 })), + ), ); } diff --git a/app/javascript/flavours/glitch/utils/batch_array.ts b/app/javascript/flavours/glitch/utils/batch_array.ts new file mode 100644 index 0000000000..9e54895d33 --- /dev/null +++ b/app/javascript/flavours/glitch/utils/batch_array.ts @@ -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(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; +}