From 3ab1ceaf60334ff7c5a8579cd6758e44bcbcf029 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 2 Mar 2025 11:43:23 +0200 Subject: [PATCH] Fix unexpected server error when trying to access non-existing tournament team page --- .../tournament/routes/to.$id.teams.$tid.tsx | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/app/features/tournament/routes/to.$id.teams.$tid.tsx b/app/features/tournament/routes/to.$id.teams.$tid.tsx index ee1ed8a0b..355ba4903 100644 --- a/app/features/tournament/routes/to.$id.teams.$tid.tsx +++ b/app/features/tournament/routes/to.$id.teams.$tid.tsx @@ -5,12 +5,12 @@ import { useTranslation } from "react-i18next"; import { Avatar } from "~/components/Avatar"; import { ModeImage, StageImage } from "~/components/Image"; import { Placement } from "~/components/Placement"; -import { Redirect } from "~/components/Redirect"; import { SendouButton } from "~/components/elements/Button"; import { SendouPopover } from "~/components/elements/Popover"; -import type { - TournamentData, - TournamentDataTeam, +import { + type TournamentData, + type TournamentDataTeam, + tournamentDataCached, } from "~/features/tournament-bracket/core/Tournament.server"; import { tournamentTeamPageParamsSchema } from "~/features/tournament-bracket/tournament-bracket-schemas.server"; import type { TournamentMaplistSource } from "~/modules/tournament-map-list-generator"; @@ -19,7 +19,6 @@ import { parseParams } from "~/utils/remix.server"; import { teamPage, tournamentMatchPage, - tournamentPage, tournamentTeamPage, userPage, userSubmittedImage, @@ -56,19 +55,27 @@ export const meta: MetaFunction = (args) => { }); }; -export const loader = ({ params }: LoaderFunctionArgs) => { +export const loader = async ({ params }: LoaderFunctionArgs) => { const tournamentId = tournamentIdFromParams(params); const tournamentTeamId = parseParams({ params, schema: tournamentTeamPageParamsSchema, }).tid; + const tournament = await tournamentDataCached({ tournamentId }); + if ( + !tournament || + !tournament.ctx.teams.some((t) => t.id === tournamentTeamId) + ) { + throw new Response(null, { status: 404 }); + } + + // TODO: could be inferred from tournament data (winCounts too) const sets = tournamentTeamSets({ tournamentTeamId, tournamentId }); return { tournamentTeamId, sets, - // TODO: could be inferred from tournament data winCounts: winCounts(sets), }; }; @@ -79,10 +86,7 @@ export default function TournamentTeamPage() { const teamIndex = tournament.ctx.teams.findIndex( (t) => t.id === data.tournamentTeamId, ); - const team = tournament.teamById(data.tournamentTeamId); - if (!team) { - return ; - } + const team = tournament.teamById(data.tournamentTeamId)!; return (