mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-08-27 21:55:15 -05:00
Refactor to use resolveRedirect function
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { LfgGroupStatus } from "@prisma/client";
|
||||
import { redirect } from "remix";
|
||||
import invariant from "tiny-invariant";
|
||||
import {
|
||||
BIT_HIGHER_MMR_LIMIT,
|
||||
@@ -13,6 +15,12 @@ import {
|
||||
LookingLoaderDataGroup,
|
||||
} from "~/routes/play/looking";
|
||||
import { Unpacked } from "~/utils";
|
||||
import {
|
||||
sendouQAddPlayersPage,
|
||||
sendouQFrontPage,
|
||||
sendouQLookingPage,
|
||||
sendouQMatchPage,
|
||||
} from "~/utils/urls";
|
||||
import { skillArrayToMMR, teamSkillToExactMMR } from "../mmr/utils";
|
||||
import { canUniteWithGroup } from "./validators";
|
||||
|
||||
@@ -300,3 +308,36 @@ export function calculateDifference({
|
||||
|
||||
throw new Error("Unexpected calculateMMRRelation scenario");
|
||||
}
|
||||
|
||||
export function resolveRedirect({
|
||||
currentStatus = "INACTIVE",
|
||||
currentPage,
|
||||
matchId,
|
||||
}: {
|
||||
currentStatus: LfgGroupStatus;
|
||||
currentPage: LfgGroupStatus;
|
||||
matchId: string | null;
|
||||
}) {
|
||||
if (currentStatus === currentPage) return;
|
||||
switch (currentStatus) {
|
||||
case "INACTIVE": {
|
||||
return redirect(sendouQFrontPage());
|
||||
}
|
||||
case "LOOKING": {
|
||||
return redirect(sendouQLookingPage());
|
||||
}
|
||||
case "MATCH": {
|
||||
invariant(matchId, "Unexpected no match id for redirect");
|
||||
return redirect(sendouQMatchPage(matchId));
|
||||
}
|
||||
case "PRE_ADD": {
|
||||
return redirect(sendouQAddPlayersPage());
|
||||
}
|
||||
default: {
|
||||
const exhaustive: never = currentStatus;
|
||||
throw new Response(`Unknown status: ${JSON.stringify(exhaustive)}`, {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ import {
|
||||
redirect,
|
||||
useLoaderData,
|
||||
} from "remix";
|
||||
import invariant from "tiny-invariant";
|
||||
import { z } from "zod";
|
||||
import { AddPlayers } from "~/components/AddPlayers";
|
||||
import { Alert } from "~/components/Alert";
|
||||
import { Button } from "~/components/Button";
|
||||
import { GroupCard } from "~/components/play/GroupCard";
|
||||
import { resolveRedirect } from "~/core/play/utils";
|
||||
import {
|
||||
canPreAddToGroup,
|
||||
isGroupAdmin,
|
||||
@@ -134,11 +134,12 @@ export const loader: LoaderFunction = async ({ context }) => {
|
||||
User.findTrusters(user.id),
|
||||
]);
|
||||
if (!ownGroup) return redirect("/play");
|
||||
if (ownGroup.status === "MATCH") {
|
||||
invariant(ownGroup.matchId, "Unexpected no matchId but status is MATCH");
|
||||
return redirect(`/play/match/${ownGroup.matchId}`);
|
||||
}
|
||||
if (ownGroup.status === "LOOKING") return redirect("/play/looking");
|
||||
const redirectRes = resolveRedirect({
|
||||
currentStatus: ownGroup.status,
|
||||
currentPage: "PRE_ADD",
|
||||
matchId: ownGroup.matchId,
|
||||
});
|
||||
if (redirectRes) return redirectRes;
|
||||
|
||||
return json<AddPlayersLoaderData>({
|
||||
inviteCode: ownGroup.inviteCode,
|
||||
|
||||
@@ -25,8 +25,7 @@ import * as LFGGroup from "~/models/LFGGroup.server";
|
||||
import * as Skill from "~/models/Skill.server";
|
||||
import { Button } from "~/components/Button";
|
||||
import { useUser } from "~/hooks/common";
|
||||
import { countGroups } from "~/core/play/utils";
|
||||
import invariant from "tiny-invariant";
|
||||
import { countGroups, resolveRedirect } from "~/core/play/utils";
|
||||
import { resolveOwnMMR } from "~/core/mmr/utils";
|
||||
|
||||
export const links: LinksFunction = () => {
|
||||
@@ -116,13 +115,14 @@ export const loader: LoaderFunction = async ({ context }) => {
|
||||
if (!ownGroup) {
|
||||
return json<PlayFrontPageLoader>({ counts: countGroups(groups), ownMMR });
|
||||
}
|
||||
if (ownGroup.status === "MATCH") {
|
||||
invariant(ownGroup.matchId, "Unexpected no matchId but status is MATCH");
|
||||
return redirect(`/play/match/${ownGroup.matchId}`);
|
||||
}
|
||||
if (ownGroup.status === "LOOKING") return redirect("/play/looking");
|
||||
const redirectRes = resolveRedirect({
|
||||
currentStatus: ownGroup.status,
|
||||
currentPage: "INACTIVE",
|
||||
matchId: ownGroup.matchId,
|
||||
});
|
||||
if (redirectRes) return redirectRes;
|
||||
|
||||
return redirect("/play/add-players");
|
||||
throw new Error(`Unexpected state - group status: ${ownGroup.status}`);
|
||||
};
|
||||
|
||||
export default function PlayPage() {
|
||||
|
||||
@@ -21,6 +21,7 @@ import { addInfoFromOldSendouInk } from "~/core/play/playerInfos/playerInfos.ser
|
||||
import {
|
||||
groupExpirationStatus,
|
||||
otherGroupsForResponse,
|
||||
resolveRedirect,
|
||||
uniteGroupInfo,
|
||||
} from "~/core/play/utils";
|
||||
import { canUniteWithGroup, isGroupAdmin } from "~/core/play/validators";
|
||||
@@ -236,11 +237,12 @@ export const loader: LoaderFunction = async ({ context }) => {
|
||||
]);
|
||||
|
||||
if (!ownGroup) return redirect("/play");
|
||||
if (ownGroup.status === "MATCH") {
|
||||
invariant(ownGroup.matchId, "Unexpected no matchId but status is MATCH");
|
||||
return redirect(`/play/match/${ownGroup.matchId}`);
|
||||
}
|
||||
if (ownGroup.status === "PRE_ADD") return redirect("/play/add-players");
|
||||
const redirectRes = resolveRedirect({
|
||||
currentStatus: ownGroup.status,
|
||||
currentPage: "LOOKING",
|
||||
matchId: ownGroup.matchId,
|
||||
});
|
||||
if (redirectRes) return redirectRes;
|
||||
|
||||
const lookingForMatch =
|
||||
ownGroup.type === "VERSUS" &&
|
||||
|
||||
@@ -5,6 +5,9 @@ export function oldSendouInkUserProfile({ discordId }: { discordId: string }) {
|
||||
export function sendouQFrontPage() {
|
||||
return "/play";
|
||||
}
|
||||
export function sendouQLookingPage() {
|
||||
return "/play/looking";
|
||||
}
|
||||
export function sendouQAddPlayersPage() {
|
||||
return "/play/add-players";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user