113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
interface RevisionedSnapshot {
|
|
revision: number;
|
|
}
|
|
|
|
export type TeamProgressErrorContent = {
|
|
kind:
|
|
| "unauthorized"
|
|
| "forbidden"
|
|
| "not-found"
|
|
| "not-ready"
|
|
| "server"
|
|
| "network"
|
|
| "unknown";
|
|
title: string;
|
|
description: string;
|
|
};
|
|
|
|
export function isConsistentTeamProgressSnapshot(
|
|
competition: RevisionedSnapshot | null | undefined,
|
|
teamView: RevisionedSnapshot | null | undefined,
|
|
): boolean {
|
|
return Boolean(
|
|
competition &&
|
|
teamView &&
|
|
competition.revision === teamView.revision,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* A visitor who is not signed in (401) — or is signed in but belongs to no team
|
|
* in this Competition (403) — can still read a team view by naming the team.
|
|
* Offer the team picker instead of an error until they have named one.
|
|
*/
|
|
export function shouldOfferTeamSelection(
|
|
errors: readonly unknown[],
|
|
selectedTeamId: string | undefined,
|
|
): boolean {
|
|
if (selectedTeamId) return false;
|
|
const statuses = errorStatuses(errors);
|
|
return statuses.includes(401) || statuses.includes(403);
|
|
}
|
|
|
|
export function getTeamProgressErrorContent(
|
|
errors: readonly unknown[],
|
|
): TeamProgressErrorContent {
|
|
const statuses = errorStatuses(errors);
|
|
|
|
if (statuses.includes(401)) {
|
|
return {
|
|
kind: "unauthorized",
|
|
title: "로그인이 필요합니다",
|
|
description: "팀 진행 정보를 보려면 로그인한 뒤 다시 시도해 주세요.",
|
|
};
|
|
}
|
|
if (statuses.includes(403)) {
|
|
return {
|
|
kind: "forbidden",
|
|
title: "팀 진행 정보를 볼 권한이 없습니다",
|
|
description:
|
|
"이 내전에 참여한 팀원 계정으로 로그인했는지 확인해 주세요.",
|
|
};
|
|
}
|
|
if (statuses.includes(404)) {
|
|
return {
|
|
kind: "not-found",
|
|
title: "팀 진행 정보를 찾을 수 없습니다",
|
|
description: "내전이 삭제되었거나 Competition이 아직 준비되지 않았습니다.",
|
|
};
|
|
}
|
|
if (statuses.includes(409)) {
|
|
return {
|
|
kind: "not-ready",
|
|
title: "Competition이 아직 준비되지 않았습니다",
|
|
description: "팀 구성이 완료된 뒤 다시 시도해 주세요.",
|
|
};
|
|
}
|
|
if (statuses.some((status) => status >= 500 && status <= 599)) {
|
|
return {
|
|
kind: "server",
|
|
title: "서버에서 팀 진행 정보를 불러오지 못했습니다",
|
|
description: "잠시 후 다시 시도해 주세요.",
|
|
};
|
|
}
|
|
if (errors.some(isNetworkError)) {
|
|
return {
|
|
kind: "network",
|
|
title: "네트워크에 연결할 수 없습니다",
|
|
description: "인터넷 연결을 확인한 뒤 다시 시도해 주세요.",
|
|
};
|
|
}
|
|
return {
|
|
kind: "unknown",
|
|
title: "팀 진행 정보를 불러오지 못했습니다",
|
|
description: "일시적인 오류일 수 있습니다. 다시 시도해 주세요.",
|
|
};
|
|
}
|
|
|
|
function errorStatuses(errors: readonly unknown[]): number[] {
|
|
return errors.flatMap((error) => {
|
|
if (typeof error !== "object" || error === null || !("status" in error)) {
|
|
return [];
|
|
}
|
|
return typeof error.status === "number" ? [error.status] : [];
|
|
});
|
|
}
|
|
|
|
function isNetworkError(error: unknown): boolean {
|
|
return (
|
|
error instanceof TypeError ||
|
|
(error instanceof Error && error.name === "TypeError")
|
|
);
|
|
}
|