From 9d5dacaa0e16fedf2b7a6451ec80abbfb29cf638 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Mon, 2 Mar 2026 19:55:24 +0200 Subject: [PATCH] Unify /subs and /looking --- .../TournamentLFGRepository.server.ts | 53 ++++ .../actions/to.$id.looking.server.ts | 49 +++ .../loaders/to.$id.looking.server.ts | 73 ++++- .../routes/to.$id.looking.module.css | 107 +++++++ .../tournament-lfg/routes/to.$id.looking.tsx | 212 ++++++++++++- .../tournament-lfg-schemas.server.ts | 8 + .../TournamentSubRepository.server.ts | 164 ---------- .../actions/to.$id.subs.new.server.ts | 54 ---- .../actions/to.$id.subs.server.ts | 41 --- .../loaders/to.$id.subs.new.server.ts | 29 -- .../loaders/to.$id.subs.server.ts | 21 +- .../routes/to.$id.subs.module.css | 109 ------- .../routes/to.$id.subs.new.module.css | 9 - .../routes/to.$id.subs.new.tsx | 296 ------------------ .../tournament-subs/routes/to.$id.subs.tsx | 198 +----------- .../tournament-subs-constants.ts | 4 - .../tournament-subs-schemas.server.ts | 27 -- .../tournament/TournamentRepository.server.ts | 15 +- app/features/tournament/routes/to.$id.tsx | 16 +- .../user-page/UserRepository.server.ts | 37 --- app/routes.ts | 1 - app/utils/urls.ts | 2 +- 22 files changed, 510 insertions(+), 1015 deletions(-) delete mode 100644 app/features/tournament-subs/TournamentSubRepository.server.ts delete mode 100644 app/features/tournament-subs/actions/to.$id.subs.new.server.ts delete mode 100644 app/features/tournament-subs/actions/to.$id.subs.server.ts delete mode 100644 app/features/tournament-subs/loaders/to.$id.subs.new.server.ts delete mode 100644 app/features/tournament-subs/routes/to.$id.subs.module.css delete mode 100644 app/features/tournament-subs/routes/to.$id.subs.new.module.css delete mode 100644 app/features/tournament-subs/routes/to.$id.subs.new.tsx delete mode 100644 app/features/tournament-subs/tournament-subs-constants.ts delete mode 100644 app/features/tournament-subs/tournament-subs-schemas.server.ts diff --git a/app/features/tournament-lfg/TournamentLFGRepository.server.ts b/app/features/tournament-lfg/TournamentLFGRepository.server.ts index 783f64a8e..ecfc4cc33 100644 --- a/app/features/tournament-lfg/TournamentLFGRepository.server.ts +++ b/app/features/tournament-lfg/TournamentLFGRepository.server.ts @@ -11,6 +11,8 @@ import { import { errorIsSqliteForeignKeyConstraintFailure } from "~/utils/sql"; import { randomTeamName } from "~/utils/team-name"; +// xxx: handle visibility like scrim post + export function startLooking(teamId: number) { return db .updateTable("TournamentTeam") @@ -23,6 +25,7 @@ type CreatePlaceholderTeamArgs = { tournamentId: number; userId: number; isStayAsSub?: boolean; + lfgNote?: string; }; export function createPlaceholderTeam(args: CreatePlaceholderTeamArgs) { return db.transaction().execute(async (trx) => { @@ -34,6 +37,7 @@ export function createPlaceholderTeam(args: CreatePlaceholderTeamArgs) { inviteCode: shortNanoid(), isPlaceholder: 1, isLooking: 1, + lfgNote: args.lfgNote ?? null, }) .returning("id") .executeTakeFirstOrThrow(); @@ -62,6 +66,7 @@ type TournamentLFGMemberObject = { languages: Tables["User"]["languages"]; vc: Tables["User"]["vc"]; pronouns: Tables["User"]["pronouns"]; + country: Tables["User"]["country"]; role: Tables["TournamentTeamMember"]["role"]; isStayAsSub: Tables["TournamentTeamMember"]["isStayAsSub"]; weapons: Tables["User"]["qWeaponPool"]; @@ -104,6 +109,8 @@ export async function findLookingTeamsByTournamentId(tournamentId: number) { languages: eb.ref("User.languages"), vc: eb.ref("User.vc"), pronouns: eb.ref("User.pronouns"), + // xxx: is it better to show country rather than languages? + country: eb.ref("User.country"), role: eb.ref("TournamentTeamMember.role"), isStayAsSub: eb.ref("TournamentTeamMember.isStayAsSub"), weapons: eb.ref("User.qWeaponPool"), @@ -120,6 +127,52 @@ export async function findLookingTeamsByTournamentId(tournamentId: number) { .execute(); } +export async function findSubGroups(tournamentId: number) { + const rows = await db + .selectFrom("TournamentTeam") + .innerJoin( + "TournamentTeamMember", + "TournamentTeamMember.tournamentTeamId", + "TournamentTeam.id", + ) + .innerJoin("User", "User.id", "TournamentTeamMember.userId") + .leftJoin("PlusTier", "PlusTier.userId", "User.id") + .select(({ fn, eb }) => [ + "TournamentTeam.id", + "TournamentTeam.lfgNote as message", + fn + .agg("json_group_array", [ + jsonBuildObject({ + id: eb.ref("User.id"), + username: eb.ref("User.username"), + discordId: eb.ref("User.discordId"), + discordAvatar: eb.ref("User.discordAvatar"), + customUrl: eb.ref("User.customUrl"), + languages: eb.ref("User.languages"), + vc: eb.ref("User.vc"), + pronouns: eb.ref("User.pronouns"), + country: eb.ref("User.country"), + role: eb.ref("TournamentTeamMember.role"), + isStayAsSub: eb.ref("TournamentTeamMember.isStayAsSub"), + weapons: eb.ref("User.qWeaponPool"), + chatNameColor: userChatNameColorForJson, + plusTier: eb.ref("PlusTier.tier"), + }), + ]) + .$castTo() + .as("members"), + ]) + // xxx: is this correct? + .where("TournamentTeam.tournamentId", "=", tournamentId) + .where("TournamentTeam.isPlaceholder", "=", 1) + .where("TournamentTeamMember.isStayAsSub", "=", 1) + .groupBy("TournamentTeam.id") + .having(({ fn }) => fn.countAll(), "=", 1) + .execute(); + + return rows; +} + // xxx: ensure that the team size does not exceed the max, also filter out in frontend export function mergeTeams({ survivingTeamId, diff --git a/app/features/tournament-lfg/actions/to.$id.looking.server.ts b/app/features/tournament-lfg/actions/to.$id.looking.server.ts index 7db1c227f..7b1e89408 100644 --- a/app/features/tournament-lfg/actions/to.$id.looking.server.ts +++ b/app/features/tournament-lfg/actions/to.$id.looking.server.ts @@ -13,6 +13,9 @@ import { lookingSchema } from "../tournament-lfg-schemas.server"; import { survivingTeamId } from "../tournament-lfg-utils"; // xxx: prevent actions in certain cases? like when tournament has started +// xxx: add `requireNotBannedByOrganization` & other checks from deleted to.$id.subs.new.tsx route +// xxx: add `clearTournamentDataCache` + export const action = async ({ request, params }: ActionFunctionArgs) => { const user = requireUser(); const { id: tournamentId } = parseParams({ params, schema: idObject }); @@ -199,6 +202,52 @@ export const action = async ({ request, params }: ActionFunctionArgs) => { break; } + case "ADD_SUB": { + const tournament = await tournamentFromDBCached({ + tournamentId, + user, + }); + errorToastIfFalsy( + tournament.canAddNewSubPost, + "Cannot add sub post at this time", + ); + + const team = tournament.teamMemberOfByUser(user); + errorToastIfFalsy(!team, "Already on a team"); + + const existingSubGroups = + await TournamentLFGRepository.findSubGroups(tournamentId); + const hasExistingSubPost = existingSubGroups.some((g) => + g.members.some((m) => m.id === user.id), + ); + errorToastIfFalsy(!hasExistingSubPost, "Already have a sub post"); + + await TournamentLFGRepository.createPlaceholderTeam({ + tournamentId, + userId: user.id, + isStayAsSub: true, + lfgNote: data.message, + }); + + break; + } + case "DELETE_SUB": { + const tournament = await tournamentFromDBCached({ + tournamentId, + user, + }); + errorToastIfFalsy( + user.id === data.userId || tournament.isOrganizer(user), + "You can only delete your own sub post", + ); + + await TournamentLFGRepository.leaveLfg({ + userId: data.userId, + tournamentId, + }); + + break; + } default: { assertUnreachable(data); } diff --git a/app/features/tournament-lfg/loaders/to.$id.looking.server.ts b/app/features/tournament-lfg/loaders/to.$id.looking.server.ts index 318e48c32..c7618784e 100644 --- a/app/features/tournament-lfg/loaders/to.$id.looking.server.ts +++ b/app/features/tournament-lfg/loaders/to.$id.looking.server.ts @@ -3,15 +3,42 @@ import type { Pronouns } from "~/db/tables"; import { getUser } from "~/features/auth/core/user.server"; import { tournamentFromDBCached } from "~/features/tournament-bracket/core/Tournament.server"; import type { MainWeaponId } from "~/modules/in-game-lists/types"; +import type { SerializeFrom } from "~/utils/remix"; import { parseParams } from "~/utils/remix.server"; import { idObject } from "~/utils/zod"; import type { LFGGroup, LFGGroupMember } from "../components/LFGGroupCard"; import * as TournamentLFGRepository from "../TournamentLFGRepository.server"; +export type SubEntry = Extract< + LookingLoaderData, + { mode: "subs" } +>["subs"][number]; + +export type LookingLoaderData = SerializeFrom; + export const loader = async ({ params }: LoaderFunctionArgs) => { const user = getUser(); const { id: tournamentId } = parseParams({ params, schema: idObject }); + const tournament = await tournamentFromDBCached({ + tournamentId, + user, + }); + + if (tournament.registrationOpen) { + return lookingMode({ tournamentId, user }); + } + + return subsMode({ tournamentId, user }); +}; + +async function lookingMode({ + tournamentId, + user, +}: { + tournamentId: number; + user: ReturnType; +}) { const rawGroups = await TournamentLFGRepository.findLookingTeamsByTournamentId(tournamentId); @@ -45,14 +72,56 @@ export const loader = async ({ params }: LoaderFunctionArgs) => { }); return { + mode: "looking" as const, groups: otherGroups, ownGroup, ownTeam, likes, - lastUpdated: Date.now(), tournamentId, }; -}; +} + +async function subsMode({ + tournamentId, + user, +}: { + tournamentId: number; + user: ReturnType; +}) { + const rawSubGroups = + await TournamentLFGRepository.findSubGroups(tournamentId); + + const subs = rawSubGroups.map((group) => { + const member = group.members[0]; + const weapons = parseWeapons(member.weapons); + + const languages = + typeof member.languages === "string" + ? member.languages.split(",").filter(Boolean) + : []; + + return { + teamId: group.id, + userId: member.id, + username: member.username, + discordId: member.discordId, + discordAvatar: member.discordAvatar, + customUrl: member.customUrl, + vc: member.vc, + languages, + plusTier: member.plusTier, + weapons, + message: group.message ?? null, + }; + }); + + return { + mode: "subs" as const, + subs, + hasOwnSubPost: subs.some((sub) => sub.userId === user?.id), + tournamentId, + }; +} async function resolveOwnTeam({ user, diff --git a/app/features/tournament-lfg/routes/to.$id.looking.module.css b/app/features/tournament-lfg/routes/to.$id.looking.module.css index 5baa5eceb..4b4d7fb0b 100644 --- a/app/features/tournament-lfg/routes/to.$id.looking.module.css +++ b/app/features/tournament-lfg/routes/to.$id.looking.module.css @@ -34,3 +34,110 @@ margin: 5px; border-radius: var(--rounded); } + +.subsContainer { + max-width: 52rem; + display: flex; + flex-direction: column; + gap: var(--s-8); + margin-inline: auto; +} + +.subsTextarea { + width: 100%; + resize: vertical; +} + +.subsSection { + display: grid; + grid-template-columns: max-content 1fr; + grid-template-areas: + "avatar name" + "avatar info" + "spacer spacer" + "weapon-top-text weapon-top-images" + "message message"; + border-radius: var(--rounded); + background-color: var(--bg-lighter); + padding: var(--s-4); + column-gap: var(--s-2-5); + border: var(--border-style); +} + +.subsSectionAvatar { + grid-area: avatar; + align-self: center; +} + +.subsSectionName { + grid-area: name; + align-self: flex-end; +} + +.subsSectionInfo { + grid-area: info; + font-size: var(--fonts-sm); + gap: var(--s-2); + color: var(--text-lighter); + display: flex; + height: 32px; + white-space: nowrap; + align-items: center; +} + +.subsInfoVc { + display: flex; + align-items: center; + gap: var(--s-0-5); +} + +.subsInfoVc > svg { + width: 18px; +} + +.subsSectionMessage { + grid-area: message; + font-size: var(--fonts-sm); + padding-block-start: var(--s-4); + white-space: pre-wrap; +} + +.subsSectionWeaponText { + font-weight: var(--semi-bold); + font-size: var(--fonts-xs); + align-self: center; +} + +.subsSectionWeaponTopText { + grid-area: weapon-top-text; +} + +.subsSectionWeaponImages { + display: flex; + gap: var(--s-1); + width: 11rem; + align-items: center; +} + +.subsSectionWeaponTopImages { + grid-area: weapon-top-images; +} + +.subsSectionSpacer { + grid-area: spacer; + height: 18px; +} + +@media screen and (min-width: 640px) { + .subsSection { + grid-template-columns: max-content 1fr max-content max-content; + grid-template-areas: + "avatar name weapon-top-text weapon-top-images" + "avatar info . ." + "message message message message"; + } + + .subsSectionSpacer { + display: none; + } +} diff --git a/app/features/tournament-lfg/routes/to.$id.looking.tsx b/app/features/tournament-lfg/routes/to.$id.looking.tsx index a95050c74..82f4fbe25 100644 --- a/app/features/tournament-lfg/routes/to.$id.looking.tsx +++ b/app/features/tournament-lfg/routes/to.$id.looking.tsx @@ -1,8 +1,9 @@ import clsx from "clsx"; -import type * as React from "react"; +import * as React from "react"; import { Flipper } from "react-flip-toolkit"; import { useTranslation } from "react-i18next"; -import { useFetcher, useLoaderData } from "react-router"; +import { Link, useFetcher, useLoaderData } from "react-router"; +import { Avatar } from "~/components/Avatar"; import { SendouButton } from "~/components/elements/Button"; import { SendouTab, @@ -11,25 +12,34 @@ import { SendouTabs, } from "~/components/elements/Tabs"; import { FormWithConfirm } from "~/components/FormWithConfirm"; +import { WeaponImage } from "~/components/Image"; +import { MicrophoneIcon } from "~/components/icons/Microphone"; +import { TrashIcon } from "~/components/icons/Trash"; import { Placeholder } from "~/components/Placeholder"; import { SubmitButton } from "~/components/SubmitButton"; +import { useUser } from "~/features/auth/core/user"; import { useTournament } from "~/features/tournament/routes/to.$id"; import { SendouForm } from "~/form/SendouForm"; -import { useAutoRefresh } from "~/hooks/useAutoRefresh"; import { useIsMounted } from "~/hooks/useIsMounted"; import { useWindowSize } from "~/hooks/useWindowSize"; import type { SendouRouteHandle } from "~/utils/remix.server"; +import { userPage } from "~/utils/urls"; import { LFGGroupCard } from "../components/LFGGroupCard"; -import type { loader } from "../loaders/to.$id.looking.server"; +import { + type LookingLoaderData, + loader, + type SubEntry, +} from "../loaders/to.$id.looking.server"; +import { TOURNAMENT_LFG } from "../tournament-lfg-constants"; import { joinQueueFormSchema } from "../tournament-lfg-schemas"; export { action } from "../actions/to.$id.looking.server"; -export { loader } from "../loaders/to.$id.looking.server"; +export { loader }; import styles from "./to.$id.looking.module.css"; export const handle: SendouRouteHandle = { - i18n: ["q", "tournament", "forms"], + i18n: ["q", "tournament", "forms", "common"], }; export default function TournamentLFGShell() { @@ -45,14 +55,19 @@ export default function TournamentLFGShell() { // xxx: can we reuse the column layout with sendouq? function TournamentLFGPage() { const data = useLoaderData(); - useAutoRefresh(data.lastUpdated); + if (data.mode === "subs") { + return ; + } - return ; + return ; } -function Groups() { +function GroupsView({ + data, +}: { + data: Extract; +}) { const { t } = useTranslation(["q"]); - const data = useLoaderData(); const tournament = useTournament(); const { width } = useWindowSize(); @@ -215,6 +230,178 @@ function Groups() { ); } +function SubsView({ + data, +}: { + data: Extract; +}) { + const user = useUser(); + const tournament = useTournament(); + + const isOnTeam = Boolean(tournament.teamMemberOfByUser(user)); + + return ( +
+ {tournament.canAddNewSubPost && + !data.hasOwnSubPost && + user && + !isOnTeam ? ( + + ) : null} + {data.subs.map((sub) => ( + + ))} +
+ ); +} + +// xxx: use SendouForm +function AddSubForm() { + const { t } = useTranslation(["tournament", "common"]); + const fetcher = useFetcher(); + const [showForm, setShowForm] = React.useState(false); + + if (!showForm) { + return ( +
+ setShowForm(true)}> + {t("tournament:subs.addPost")} + +
+ ); + } + + return ( + +