[Glitch] Fix ValidationError when loading many collections at once
Port 2b6b2fcb6ff7f0c066260c0aa3f14f34e4bc3588 to glitch-soc Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
45a21db621
commit
cdc2d91d43
@ -27,6 +27,7 @@ import {
|
|||||||
createAppSelector,
|
createAppSelector,
|
||||||
createDataLoadingThunk,
|
createDataLoadingThunk,
|
||||||
} from '@/flavours/glitch/store/typed_functions';
|
} from '@/flavours/glitch/store/typed_functions';
|
||||||
|
import { batchArray } from '@/flavours/glitch/utils/batch_array';
|
||||||
import { inputToHashtag } from '@/flavours/glitch/utils/hashtags';
|
import { inputToHashtag } from '@/flavours/glitch/utils/hashtags';
|
||||||
|
|
||||||
type QueryStatus = 'idle' | 'loading' | 'error';
|
type QueryStatus = 'idle' | 'loading' | 'error';
|
||||||
@ -337,10 +338,14 @@ async function importAccountsForPreviewCard(
|
|||||||
)
|
)
|
||||||
.filter((id): id is string => !!id);
|
.filter((id): id is string => !!id);
|
||||||
|
|
||||||
await dispatch(
|
// fetchAccounts can only process up to 40 item ids, so we'll
|
||||||
fetchAccounts({
|
// batch the list of ids
|
||||||
accountIds: previewAccountIds,
|
const batchedAccountIdLists = batchArray(previewAccountIds, 40);
|
||||||
}),
|
|
||||||
|
await Promise.allSettled(
|
||||||
|
batchedAccountIdLists.map((accountIds) =>
|
||||||
|
dispatch(fetchAccounts({ accountIds })),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
app/javascript/flavours/glitch/utils/batch_array.ts
Normal file
15
app/javascript/flavours/glitch/utils/batch_array.ts
Normal file
@ -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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user