Skip to content

Commit 0873b49

Browse files
authored
Merge pull request #1141 from kenkoooo/pr/MatsuTaku/1137
Pr/matsu taku/1137
2 parents f751354 + 4701c08 commit 0873b49

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

atcoder-problems-frontend/src/api/APIClient.ts

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,25 +295,45 @@ export const useVirtualContestSubmissions = (
295295
) => {
296296
const PROBLEM_CHUNK_SIZE = 10;
297297
const USER_CHUNK_SIZE = 10;
298-
const requestCount =
299-
Math.ceil(users.length / USER_CHUNK_SIZE) *
300-
Math.ceil(problems.length / PROBLEM_CHUNK_SIZE);
301-
302-
const refreshInterval = enableAutoRefresh
303-
? Math.max(1, requestCount / 10) * 60_000
304-
: 1_000_000_000;
298+
const SEPARATOR = "###";
305299

306-
const userChunks = toChunks(users, USER_CHUNK_SIZE);
307-
const problemChunks = toChunks(problems, PROBLEM_CHUNK_SIZE);
308300
const singleFetch = async (users: UserId[], problems: ProblemId[]) => {
309301
const userList = users.join(",");
310302
const problemList = problems.join(",");
311303
const url = `${ATCODER_API_URL}/v3/users_and_time?users=${userList}&problems=${problemList}&from=${fromSecond}&to=${toSecond}`;
312304
const submissions = await fetchTypedArray(url, isSubmission);
313305
return submissions.filter((submission) => isValidResult(submission.result));
314306
};
315-
316-
const fetcher = async () => {
307+
const serialize = (users: UserId[], problems: ProblemId[]) => {
308+
const sortedUsers = Array.from(users);
309+
sortedUsers.sort();
310+
const userKey = sortedUsers.join(",");
311+
312+
const sortedProblems = Array.from(problems);
313+
sortedProblems.sort();
314+
const problemKey = sortedProblems.join(",");
315+
316+
return (
317+
"useVirtualContestSubmissions" +
318+
SEPARATOR +
319+
userKey +
320+
SEPARATOR +
321+
problemKey
322+
);
323+
};
324+
const deserialize = (key: string) => {
325+
const keys = key.split("###");
326+
const userKey = keys[1];
327+
const problemKey = keys[2];
328+
329+
const users = userKey.split(",");
330+
const problems = problemKey.split(",");
331+
return { users, problems };
332+
};
333+
const fetcher = async (key: string) => {
334+
const { users, problems } = deserialize(key);
335+
const userChunks = toChunks(users, USER_CHUNK_SIZE);
336+
const problemChunks = toChunks(problems, PROBLEM_CHUNK_SIZE);
317337
const promises = userChunks
318338
.flatMap((users) =>
319339
problemChunks.map((problems) => ({ users, problems }))
@@ -323,13 +343,15 @@ export const useVirtualContestSubmissions = (
323343
return submissionChunks.flatMap((x) => x);
324344
};
325345

326-
return useSWRData(
327-
"useVirtualContestSubmissions",
328-
() => (users.length > 0 ? fetcher() : Promise.resolve([])),
329-
{
330-
refreshInterval,
331-
}
332-
);
346+
const requestCount =
347+
Math.ceil(users.length / USER_CHUNK_SIZE) *
348+
Math.ceil(problems.length / PROBLEM_CHUNK_SIZE);
349+
const refreshInterval = enableAutoRefresh
350+
? Math.max(1, requestCount / 10) * 60_000
351+
: 1_000_000_000;
352+
353+
const customKey = serialize(users, problems);
354+
return useSWRData(customKey, fetcher, { refreshInterval });
333355
};
334356

335357
export const useRecentSubmissions = () => {

atcoder-problems-frontend/src/pages/Internal/VirtualContest/ShowContest/ContestTable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ export const ContestTable = (props: Props) => {
154154
end,
155155
props.enableAutoRefresh
156156
);
157-
if (!submissions.data && !submissions.error) {
158-
return <Spinner />;
157+
if (submissions.error) {
158+
return <Alert color="danger">Failed to fetch submissions.</Alert>;
159159
}
160160
if (!submissions.data) {
161-
return <Alert color="danger">Failed to fetch submissions.</Alert>;
161+
return <Spinner />;
162162
}
163163

164164
const modelArray = consolidateModels(problems, problemMap, problemModels);

atcoder-problems-frontend/src/pages/Internal/VirtualContest/ShowContest/index.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,22 @@ const Problems = (props: {
6868
}) => {
6969
const { alreadyJoined } = props;
7070
const submissions = useVirtualContestSubmissions(
71-
[props.atCoderUserId],
71+
alreadyJoined ? [props.atCoderUserId] : [],
7272
props.problems.map((p) => p.item.id),
7373
props.start,
7474
props.end,
7575
false
7676
);
7777
const pointOverrideMap = constructPointOverrideMap(props.problems);
78-
const showUserResults =
79-
props.atCoderUserId !== "" &&
80-
submissions.data !== null &&
81-
submissions.data !== undefined;
78+
const showUserResults = alreadyJoined && submissions.data;
8279
const results = submissions.data
8380
? reduceUserContestResult(submissions.data, (id) =>
8481
pointOverrideMap.get(id)
8582
)
8683
: new Map<UserId, ReducedProblemResult>();
8784
const ResultIcon = (props: { id: ProblemId }) => {
8885
const result = results.get(props.id);
89-
if (!alreadyJoined || !result) return null;
86+
if (!result) return null;
9087
if (result.accepted) {
9188
return <FcCheckmark />;
9289
} else {
@@ -210,7 +207,7 @@ const Standings = (props: StandingsProps) => {
210207
checked={showRating}
211208
onChange={(): void => setShowRating(!showRating)}
212209
/>
213-
{alreadyJoined && (
210+
{alreadyJoined && contestInfo.mode === null && (
214211
<CustomInput
215212
type="switch"
216213
id="pinMe"

0 commit comments

Comments
 (0)