From 5fda22c36a64abbc7b11d225ae646c379fcdc446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EB=8B=A4=ED=98=84=20=28Dahyeon=20Park=29?= Date: Mon, 27 Apr 2026 01:26:00 +0900 Subject: [PATCH] feat(web): add match admin page --- apps/web/src/pages/admin/MatchAdminPage.tsx | 451 ++++++++++++++++++++ 1 file changed, 451 insertions(+) create mode 100644 apps/web/src/pages/admin/MatchAdminPage.tsx diff --git a/apps/web/src/pages/admin/MatchAdminPage.tsx b/apps/web/src/pages/admin/MatchAdminPage.tsx new file mode 100644 index 00000000..ac094df7 --- /dev/null +++ b/apps/web/src/pages/admin/MatchAdminPage.tsx @@ -0,0 +1,451 @@ +import { useMemo, useState } from 'react'; +import { useQueryClient } from '@tanstack/react-query'; +import { Link, useParams } from 'react-router'; +import { + useCreateMatch, + useDeleteMatch, + useMatches, + useMe, + useUpdateMatch, +} from '../../api/hooks'; +import type { + ApiMatch, + ApiMatchScheduleItem, + MatchGameMode, + MatchMutationRequest, +} from '../../api/types'; +import { DEFAULT_COMMUNITY_ID } from '../../config'; + +const DEFAULT_FORM: MatchMutationRequest = { + title: '', + description: '', + mapId: 11, + scheduledAt: toInputValue(new Date(Date.now() + 24 * 60 * 60 * 1000)), + maxParticipants: 10, + gameCount: 3, + gameMode: 'pair', + prizes: [], + notices: [], + schedule: [], +}; + +const GAME_MODE_LABEL: Record = { + pair: '페어', + bracket: '브래킷', + 'round-robin': '라운드 로빈', +}; + +function RequiredBadge() { + return ( + + 필수 + + ); +} + +function toInputValue(value: string | Date) { + const date = value instanceof Date ? value : new Date(value); + const offset = date.getTimezoneOffset(); + return new Date(date.getTime() - offset * 60_000).toISOString().slice(0, 16); +} + +function toIsoFromInput(value: string) { + return new Date(value).toISOString(); +} + +function formatDate(value: string) { + return new Intl.DateTimeFormat('ko-KR', { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + }).format(new Date(value)); +} + +function parseLines(value: string) { + return value + .split(/\r?\n/u) + .map((line) => line.trim()) + .filter(Boolean); +} + +function parseSchedule(value: string): ApiMatchScheduleItem[] { + return parseLines(value).map((line) => { + const [time, ...labelParts] = line.split('|').map((part) => part.trim()); + return { + time: time || '00:00', + label: labelParts.join('|') || '일정', + }; + }); +} + +function scheduleToText(schedule: ApiMatchScheduleItem[] = []) { + return schedule.map((item) => `${item.time}|${item.label}`).join('\n'); +} + +function makeForm(match?: ApiMatch): MatchMutationRequest & { + prizesText: string; + noticesText: string; + scheduleText: string; +} { + if (!match) { + return { + ...DEFAULT_FORM, + prizesText: '', + noticesText: '', + scheduleText: '', + }; + } + + return { + title: match.title, + description: match.description, + mapId: match.mapId, + scheduledAt: toInputValue(match.scheduledAt), + maxParticipants: match.maxParticipants, + gameCount: match.gameCount, + gameMode: match.gameMode ?? 'pair', + prizes: match.prizes, + notices: match.notices, + schedule: match.schedule, + prizesText: match.prizes.join('\n'), + noticesText: match.notices.join('\n'), + scheduleText: scheduleToText(match.schedule), + }; +} + +export default function MatchAdminPage() { + const { cid } = useParams(); + const communityId = cid ?? DEFAULT_COMMUNITY_ID; + const queryClient = useQueryClient(); + const meQuery = useMe(); + const matchesQuery = useMatches({ limit: 200 }, communityId); + const createMutation = useCreateMatch(communityId); + const updateMutation = useUpdateMatch(communityId); + const deleteMutation = useDeleteMatch(communityId); + const [editing, setEditing] = useState(null); + const [showForm, setShowForm] = useState(false); + const [form, setForm] = useState(makeForm()); + const [message, setMessage] = useState(null); + + const currentUser = + meQuery.data?.memberships.find( + (membership) => membership.community?._id === communityId, + )?.user ?? null; + const canManage = currentUser?.role === 'owner' || currentUser?.role === 'admin'; + const matches = useMemo( + () => [...(matchesQuery.data ?? [])].sort((a, b) => new Date(b.scheduledAt).getTime() - new Date(a.scheduledAt).getTime()), + [matchesQuery.data], + ); + + async function invalidate() { + await Promise.all([ + queryClient.invalidateQueries({ queryKey: ['matches', communityId] }), + queryClient.invalidateQueries({ queryKey: ['match', communityId] }), + ]); + } + + function openCreate() { + setEditing(null); + setForm(makeForm()); + setShowForm(true); + setMessage(null); + } + + function openEdit(match: ApiMatch) { + setEditing(match); + setForm(makeForm(match)); + setShowForm(true); + setMessage(null); + } + + function buildPayload(): MatchMutationRequest { + return { + title: form.title.trim(), + description: form.description?.trim() ?? '', + mapId: Number(form.mapId ?? 11), + scheduledAt: toIsoFromInput(form.scheduledAt), + maxParticipants: Number(form.maxParticipants ?? 10), + gameCount: Number(form.gameCount ?? 3), + gameMode: form.gameMode, + prizes: parseLines(form.prizesText), + notices: parseLines(form.noticesText), + schedule: parseSchedule(form.scheduleText), + }; + } + + function submit() { + setMessage(null); + if (!form.title.trim()) { + setMessage('제목을 입력해주세요.'); + return; + } + if (!form.scheduledAt) { + setMessage('시작 시간을 입력해주세요.'); + return; + } + + const payload = buildPayload(); + + const callbacks = { + onSuccess: async () => { + await invalidate(); + setShowForm(false); + setEditing(null); + setMessage(editing ? '내전을 수정했습니다.' : '내전을 만들었습니다.'); + }, + onError: (error: Error) => setMessage(error.message), + }; + + if (editing) { + updateMutation.mutate({ matchId: editing._id, body: payload }, callbacks); + return; + } + createMutation.mutate(payload, callbacks); + } + + if (!canManage && !meQuery.isPending) { + return ( +
+

권한이 필요합니다

+

+ 내전 관리는 owner/admin만 사용할 수 있습니다. +

+
+ ); + } + + return ( +
+ + ← 목록으로 + +
+
+

내전 관리

+

+ 내전 생성, 모집 정원, 게임 모드와 공지 내용을 관리합니다. +

+
+ +
+ + {message && ( +
+ {message} +
+ )} + + {showForm && ( +
+
+

+ {editing ? '내전 수정' : '새 내전'} +

+ +
+ +
+ +