Skip to content

Commit 6741aba

Browse files
authored
Add segmentation to reports list to fix it (#3772)
1 parent ee8ee7a commit 6741aba

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

apps/frontend/src/components/ui/report/ReportsList.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<script setup>
1919
import ReportInfo from "~/components/ui/report/ReportInfo.vue";
2020
import { addReportMessage } from "~/helpers/threads.js";
21+
import { asEncodedJsonArray, fetchSegmented } from "~/utils/fetch-helpers.ts";
2122
2223
defineProps({
2324
moderation: {
@@ -53,13 +54,13 @@ const threadIds = [
5354
5455
const [{ data: users }, { data: versions }, { data: threads }] = await Promise.all([
5556
await useAsyncData(`users?ids=${JSON.stringify(userIds)}`, () =>
56-
useBaseFetch(`users?ids=${encodeURIComponent(JSON.stringify(userIds))}`),
57+
fetchSegmented(userIds, (ids) => `users?ids=${asEncodedJsonArray(ids)}`),
5758
),
5859
await useAsyncData(`versions?ids=${JSON.stringify(versionIds)}`, () =>
59-
useBaseFetch(`versions?ids=${encodeURIComponent(JSON.stringify(versionIds))}`),
60+
fetchSegmented(versionIds, (ids) => `versions?ids=${asEncodedJsonArray(ids)}`),
6061
),
6162
await useAsyncData(`threads?ids=${JSON.stringify(threadIds)}`, () =>
62-
useBaseFetch(`threads?ids=${encodeURIComponent(JSON.stringify(threadIds))}`),
63+
fetchSegmented(threadIds, (ids) => `threads?ids=${asEncodedJsonArray(ids)}`),
6364
),
6465
]);
6566
@@ -70,7 +71,7 @@ const versionProjects = versions.value.map((version) => version.project_id);
7071
const projectIds = [...new Set(reportedProjects.concat(versionProjects))];
7172
7273
const { data: projects } = await useAsyncData(`projects?ids=${JSON.stringify(projectIds)}`, () =>
73-
useBaseFetch(`projects?ids=${encodeURIComponent(JSON.stringify(projectIds))}`),
74+
fetchSegmented(projectIds, (ids) => `projects?ids=${asEncodedJsonArray(ids)}`),
7475
);
7576
7677
reports.value = rawReports.map((report) => {

apps/frontend/src/pages/moderation/review.vue

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ import {
115115
import Avatar from "~/components/ui/Avatar.vue";
116116
import Badge from "~/components/ui/Badge.vue";
117117
import { formatProjectType } from "~/plugins/shorthands.js";
118+
import { asEncodedJsonArray, fetchSegmented } from "~/utils/fetch-helpers.ts";
118119
119120
useHead({
120121
title: "Review projects - Modrinth",
@@ -170,28 +171,6 @@ const projectTypes = computed(() => {
170171
return [...set];
171172
});
172173
173-
function segmentData(data, segmentSize = 800) {
174-
return data.reduce((acc, curr, index) => {
175-
const segment = Math.floor(index / segmentSize);
176-
177-
if (!acc[segment]) {
178-
acc[segment] = [];
179-
}
180-
acc[segment].push(curr);
181-
return acc;
182-
}, []);
183-
}
184-
185-
function fetchSegmented(data, createUrl, options = {}) {
186-
return Promise.all(segmentData(data).map((ids) => useBaseFetch(createUrl(ids), options))).then(
187-
(results) => results.flat(),
188-
);
189-
}
190-
191-
function asEncodedJsonArray(data) {
192-
return encodeURIComponent(JSON.stringify(data));
193-
}
194-
195174
if (projects.value) {
196175
const teamIds = projects.value.map((x) => x.team_id);
197176
const orgIds = projects.value.filter((x) => x.organization).map((x) => x.organization);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function segmentData<T>(data: T[], segmentSize: number): T[][] {
2+
return data.reduce((acc: T[][], curr, index) => {
3+
const segment = Math.floor(index / segmentSize);
4+
5+
if (!acc[segment]) {
6+
acc[segment] = [];
7+
}
8+
acc[segment].push(curr);
9+
return acc;
10+
}, []);
11+
}
12+
13+
export function fetchSegmented<T>(
14+
data: T[],
15+
createUrl: (ids: T[]) => string,
16+
options = {},
17+
segmentSize = 800,
18+
): Promise<any> {
19+
return Promise.all(
20+
segmentData(data, segmentSize).map((ids) => useBaseFetch(createUrl(ids), options)),
21+
).then((results) => results.flat());
22+
}
23+
24+
export function asEncodedJsonArray<T>(data: T[]): string {
25+
return encodeURIComponent(JSON.stringify(data));
26+
}

0 commit comments

Comments
 (0)