Files
lolympus-tournament-support/apps/web/src/pages/HomePage.tsx
T

768 lines
27 KiB
TypeScript

import { useState } from 'react';
import { ChevronLeft, ChevronRight } from 'lucide-react';
import { Link } from 'react-router';
import { mapApiMatchToMatchDetail } from '../api/mappers';
import { useMatches, useMatchesPage, useMe } from '../api/hooks';
import { DEFAULT_COMMUNITY_ID } from '../config';
const UPCOMING_GRID_MATCH_LIMIT = 3;
const UPCOMING_MATCH_LIST_LIMIT = UPCOMING_GRID_MATCH_LIMIT + 1;
const HISTORY_MATCH_PAGE_SIZE = 10;
const EMPTY_SCHEDULE_TEXT = '예정된 매치가 없습니다.';
const HISTORY_TABLE_ROW_CLASS =
'h-10 border-b border-base-content/5 last:border-b-0';
const SECTION_HEADING_CLASS =
'text-xs font-bold text-base-content/40 uppercase tracking-widest';
const ADMIN_LINK_CLASS =
'text-xs font-bold text-base-content/35 hover:text-primary';
const STATS_LINK_CLASS = 'text-xs font-bold text-primary hover:underline';
const STATUS_BADGE_CLASS =
'inline-flex items-center whitespace-nowrap text-xs font-semibold px-1.5 py-0.5 border';
const MAP_BADGE_CLASS =
'inline-flex items-center whitespace-nowrap text-xs px-1.5 py-0.5 border';
const NEXT_MATCH_TITLE_CLASS =
'text-xl sm:text-2xl font-bold text-base-content leading-tight';
const NEXT_MATCH_DESCRIPTION_CLASS =
'text-sm text-base-content/50 max-w-xl leading-relaxed';
const NEXT_MATCH_META_CLASS =
'flex items-center gap-3 text-xs text-base-content/40 flex-wrap';
const NEXT_MATCH_PARTICIPANT_BADGE_CLASS =
'inline-flex items-center whitespace-nowrap bg-accent text-accent-content px-1.5 py-0.5 font-semibold';
const DETAIL_BUTTON_CLASS = 'btn btn-secondary btn-sm px-6 text-xs font-semibold';
const UPCOMING_CARD_FRAME_CLASS = 'bg-base-100 h-full p-4 min-h-[135px]';
const UPCOMING_CARD_TITLE_CLASS =
'text-sm font-bold text-base-content truncate mr-2';
const UPCOMING_CARD_META_CLASS = 'space-y-1 text-xs text-base-content/40';
const HISTORY_MOBILE_DATE_CLASS = 'text-xs font-medium text-base-content/40';
const HISTORY_MOBILE_TITLE_CLASS =
'mt-1 text-sm font-semibold leading-snug text-base-content break-words';
const HISTORY_MOBILE_META_CLASS =
'flex flex-wrap items-center gap-x-1.5 gap-y-1 text-xs text-base-content/45';
function SkeletonText({
children,
className = '',
skeletonClassName,
}: {
children: string;
className?: string;
skeletonClassName: string;
}) {
return (
<span className={`relative block ${className}`}>
<span className="invisible">{children}</span>
<span
aria-hidden="true"
className={`absolute left-0 top-1/2 -translate-y-1/2 animate-pulse bg-base-content/10 ${skeletonClassName}`}
/>
</span>
);
}
function SkeletonBadge({
children,
className,
}: {
children: string;
className: string;
}) {
return (
<span className={`relative ${className}`}>
<span className="invisible">{children}</span>
<span
aria-hidden="true"
className="absolute inset-0 animate-pulse bg-base-content/10"
/>
</span>
);
}
function HistoryTableColGroup() {
return (
<colgroup>
<col className="w-[156px]" />
<col />
<col className="w-[88px]" />
<col className="w-[132px]" />
<col className="w-[72px]" />
<col className="w-[116px]" />
</colgroup>
);
}
function HistoryTableHeader() {
return (
<thead>
<tr className="bg-base-200/60 border-b border-base-content/10 text-xs uppercase tracking-wider text-base-content/40">
<th className="py-2.5 px-4 font-semibold">날짜</th>
<th className="py-2.5 px-4 font-semibold">내전</th>
<th className="py-2.5 px-4 font-semibold">상태</th>
<th className="py-2.5 px-4 font-semibold">맵</th>
<th className="py-2.5 px-4 font-semibold">참가</th>
<th className="py-2.5 px-4 font-semibold">결과</th>
</tr>
</thead>
);
}
function HistoryRecordsSkeleton({
rows = HISTORY_MATCH_PAGE_SIZE,
}: {
rows?: number;
}) {
const historySkeletons = Array.from({ length: rows });
return (
<>
<div className="hidden md:block border border-base-content/10 overflow-x-auto">
<table className="w-full table-fixed text-left">
<HistoryTableColGroup />
<HistoryTableHeader />
<tbody>
{historySkeletons.map((_, index) => (
<tr
key={index}
className={HISTORY_TABLE_ROW_CLASS}
>
<td className="py-2.5 px-4 text-xs text-base-content/40 whitespace-nowrap">
<SkeletonText
skeletonClassName="h-[1em] w-20"
>
2026년 6월 8일 월
</SkeletonText>
</td>
<td className="py-2.5 px-4">
<SkeletonText
className="truncate text-sm font-medium"
skeletonClassName="h-[1em] w-36 max-w-full"
>
정기 내전
</SkeletonText>
</td>
<td className="py-2.5 px-4">
<SkeletonBadge className={`${STATUS_BADGE_CLASS} border-transparent`}>
팀빌딩
</SkeletonBadge>
</td>
<td className="py-2.5 px-4">
<SkeletonBadge className={`${MAP_BADGE_CLASS} border-transparent`}>
소환사의 협곡
</SkeletonBadge>
</td>
<td className="py-2.5 px-4 text-xs text-base-content/50 whitespace-nowrap">
<SkeletonText
skeletonClassName="h-[1em] w-8"
>
10명
</SkeletonText>
</td>
<td className="py-2.5 px-4 text-xs text-base-content/50 whitespace-nowrap">
<SkeletonText
className="truncate"
skeletonClassName="h-[1em] w-14"
>
1팀 승리
</SkeletonText>
</td>
</tr>
))}
</tbody>
</table>
</div>
<div className="md:hidden border border-base-content/10">
{historySkeletons.map((_, index) => (
<div
key={index}
className="border-b border-base-content/5 last:border-b-0 p-3"
>
<div className="flex items-start justify-between gap-3">
<div className="min-w-0 flex-1">
<SkeletonText
className={HISTORY_MOBILE_DATE_CLASS}
skeletonClassName="h-[1em] w-28"
>
6/8 (월) 21:00
</SkeletonText>
<SkeletonText
className={HISTORY_MOBILE_TITLE_CLASS}
skeletonClassName="h-[1em] w-32 max-w-full"
>
정기 내전
</SkeletonText>
</div>
<div className="shrink-0">
<SkeletonBadge className={`${STATUS_BADGE_CLASS} border-transparent`}>
팀빌딩
</SkeletonBadge>
</div>
</div>
<div className="mt-2">
<div className={HISTORY_MOBILE_META_CLASS}>
<SkeletonText skeletonClassName="h-[1em] w-20">
소환사의 협곡
</SkeletonText>
<span className="invisible text-base-content/20">·</span>
<SkeletonText skeletonClassName="h-[1em] w-8">
10명
</SkeletonText>
<span className="invisible text-base-content/20">·</span>
<SkeletonText skeletonClassName="h-[1em] w-14">
1팀 승리
</SkeletonText>
</div>
</div>
</div>
))}
</div>
</>
);
}
function HomePageSkeleton() {
const upcomingSkeletons = Array.from({ length: UPCOMING_GRID_MATCH_LIMIT });
return (
<main
className="max-w-5xl mx-auto px-4 sm:px-6"
aria-busy="true"
aria-live="polite"
>
<span className="sr-only">매치 목록을 불러오는 중...</span>
<section className="pt-8 pb-6">
<div className="flex items-center justify-between gap-3 mb-3">
<h2 className={SECTION_HEADING_CLASS}>
다음 내전
</h2>
<div className="flex items-center gap-3">
<Link
to={`/communities/${DEFAULT_COMMUNITY_ID}/internal-stats`}
className={STATS_LINK_CLASS}
>
내전 전적 보기
</Link>
</div>
</div>
<div className="border border-base-content/10 bg-base-200/50">
<div className="p-5 sm:p-6 min-h-[150px]">
<div className="flex flex-col sm:flex-row sm:items-start sm:justify-between gap-4">
<div className="space-y-2.5 flex-1">
<div className="flex items-center gap-2.5 flex-wrap">
<SkeletonText
className={NEXT_MATCH_TITLE_CLASS}
skeletonClassName="h-[1em] w-52 sm:w-64 max-w-full"
>
롤림푸스 경매 테스트 매치
</SkeletonText>
<SkeletonBadge className={`${STATUS_BADGE_CLASS} border-transparent`}>
모집중
</SkeletonBadge>
</div>
<div className={NEXT_MATCH_DESCRIPTION_CLASS}>
<SkeletonText skeletonClassName="h-[1em] w-full max-w-xl">
Socket.IO 경매 페이지 검증용으로 생성된 매치입니다.
</SkeletonText>
</div>
<div className={NEXT_MATCH_META_CLASS}>
<SkeletonText skeletonClassName="h-[1em] w-24">
6월 11일 21:00
</SkeletonText>
<span className="invisible text-base-content/15">|</span>
<SkeletonBadge className={NEXT_MATCH_PARTICIPANT_BADGE_CLASS}>
10/10명
</SkeletonBadge>
<span className="invisible text-base-content/15">|</span>
<SkeletonBadge className={`${MAP_BADGE_CLASS} border-transparent`}>
소환사의 협곡
</SkeletonBadge>
</div>
</div>
<div className="sm:shrink-0">
<SkeletonBadge className={DETAIL_BUTTON_CLASS}>
상세 보기
</SkeletonBadge>
</div>
</div>
</div>
</div>
</section>
<section className="pb-6">
<h2 className={`${SECTION_HEADING_CLASS} mb-3`}>
예정된 내전
</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-px bg-base-content/10 border border-base-content/10">
{upcomingSkeletons.map((_, index) => (
<div key={index} className={UPCOMING_CARD_FRAME_CLASS}>
<div className="flex items-center justify-between mb-2.5 gap-3">
<SkeletonText
className={UPCOMING_CARD_TITLE_CLASS}
skeletonClassName="h-[1em] w-28 max-w-full"
>
정기 내전
</SkeletonText>
<SkeletonBadge className={`${STATUS_BADGE_CLASS} border-transparent shrink-0`}>
모집중
</SkeletonBadge>
</div>
<div className={UPCOMING_CARD_META_CLASS}>
<SkeletonText skeletonClassName="h-[1em] w-24">
6월 11일 21:00
</SkeletonText>
<SkeletonText skeletonClassName="h-[1em] w-12">
10/10명
</SkeletonText>
</div>
<div className="mt-2.5">
<SkeletonBadge className={`${MAP_BADGE_CLASS} border-transparent`}>
소환사의 협곡
</SkeletonBadge>
</div>
</div>
))}
</div>
</section>
<section className="pb-12">
<h2 className={`${SECTION_HEADING_CLASS} mb-3`}>
지난 내전 기록
</h2>
<HistoryRecordsSkeleton />
</section>
</main>
);
}
function StatusBadge({ status }: { status: string }) {
const styles: Record<string, string> = {
모집중: 'bg-green-500/15 text-green-500 border-green-500/20',
팀빌딩: 'bg-amber-500/15 text-amber-500 border-amber-500/20',
진행중: 'bg-blue-500/15 text-blue-400 border-blue-500/20',
종료: 'bg-base-content/8 text-base-content/40 border-base-content/10',
};
return (
<span
className={`${STATUS_BADGE_CLASS} ${styles[status] ?? 'bg-base-content/10 text-base-content/50 border-base-content/10'}`}
>
{status}
</span>
);
}
function MapBadge({ map }: { map: string }) {
return (
<span className={`${MAP_BADGE_CLASS} border-base-content/10 text-base-content/50`}>
{map}
</span>
);
}
function getStatusLabel(status: string) {
switch (status) {
case 'recruiting':
return '모집중';
case 'team-built':
return '팀빌딩';
case 'in-progress':
return '진행중';
case 'finished':
return '종료';
default:
return status;
}
}
function formatCompactDateTime(dateLike: string) {
const date = new Date(dateLike);
if (Number.isNaN(date.getTime())) return '';
const weekday = new Intl.DateTimeFormat('ko-KR', {
weekday: 'short',
}).format(date);
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
return `${date.getMonth() + 1}/${date.getDate()} (${weekday}) ${hours}:${minutes}`;
}
function HistoryMetaText({
map,
participants,
}: {
map: string;
participants: number;
}) {
return (
<div className={HISTORY_MOBILE_META_CLASS}>
<span>{map}</span>
<span className="text-base-content/20">·</span>
<span>{participants}명</span>
</div>
);
}
interface HistoryPaginationProps {
page: number;
pageCount: number;
total: number;
isFetching: boolean;
onPrevious: () => void;
onNext: () => void;
}
function HistoryPagination({
page,
pageCount,
total,
isFetching,
onPrevious,
onNext,
}: HistoryPaginationProps) {
const hasPrevious = page > 1;
const hasNext = page < pageCount;
return (
<div className="flex flex-wrap items-center gap-2 text-xs text-base-content/40">
<span className="font-medium whitespace-nowrap">
총 {total}개 · {page}/{pageCount}페이지
</span>
<div className="flex items-center gap-1">
<button
type="button"
className="btn btn-ghost btn-xs px-2"
disabled={!hasPrevious || isFetching}
onClick={onPrevious}
>
<ChevronLeft className="h-3.5 w-3.5" aria-hidden="true" />
이전
</button>
<button
type="button"
className="btn btn-ghost btn-xs px-2"
disabled={!hasNext || isFetching}
onClick={onNext}
>
다음
<ChevronRight className="h-3.5 w-3.5" aria-hidden="true" />
</button>
</div>
</div>
);
}
export default function HomePage() {
const [timelineNow] = useState(() => new Date().toISOString());
const [historyPage, setHistoryPage] = useState(1);
const upcomingMatchesQuery = useMatches(
{
limit: UPCOMING_MATCH_LIST_LIMIT,
scheduledFrom: timelineNow,
sort: 'asc',
},
DEFAULT_COMMUNITY_ID,
);
const historyMatchesQuery = useMatchesPage(
{
limit: HISTORY_MATCH_PAGE_SIZE,
page: historyPage,
scheduledTo: timelineNow,
sort: 'desc',
},
DEFAULT_COMMUNITY_ID,
);
const meQuery = useMe();
if (!DEFAULT_COMMUNITY_ID) {
return (
<main className="max-w-5xl mx-auto px-4 sm:px-6 py-16">
<div className="border border-amber-500/20 bg-amber-500/10 p-5 text-sm text-amber-700">
`apps/web/.env.development`의 `VITE_DEFAULT_COMMUNITY_ID`를 먼저 설정해주세요.
</div>
</main>
);
}
if (upcomingMatchesQuery.isLoading) {
return <HomePageSkeleton />;
}
if (upcomingMatchesQuery.error) {
return (
<main className="max-w-5xl mx-auto px-4 sm:px-6 py-20 text-center">
<span className="text-sm text-red-500">
매치 목록을 불러오지 못했습니다.
</span>
</main>
);
}
const upcomingMatches =
upcomingMatchesQuery.data?.map(mapApiMatchToMatchDetail) ?? [];
const historyMatches =
historyMatchesQuery.data?.matches.map((match) => ({
...mapApiMatchToMatchDetail(match),
compactDateTime: formatCompactDateTime(match.scheduledAt),
})) ?? [];
const historyTotal = historyMatchesQuery.data?.total ?? 0;
const historyLimit =
historyMatchesQuery.data?.limit ?? HISTORY_MATCH_PAGE_SIZE;
const historyPageCount = Math.max(
1,
Math.ceil(historyTotal / historyLimit),
);
const isHistoryPageLoading =
historyMatchesQuery.isLoading ||
(historyMatchesQuery.isFetching && historyMatchesQuery.isPlaceholderData);
const hasHistoryError =
Boolean(historyMatchesQuery.error) && !historyMatchesQuery.data;
const goToPreviousHistoryPage = () =>
setHistoryPage((page) => Math.max(1, page - 1));
const goToNextHistoryPage = () => setHistoryPage((page) => page + 1);
const nextMatch = upcomingMatches[0];
const upcomingCards = (nextMatch
? upcomingMatches.slice(1)
: upcomingMatches
).slice(0, UPCOMING_GRID_MATCH_LIMIT);
const currentUser =
meQuery.data?.memberships.find(
(membership) => membership.community?._id === DEFAULT_COMMUNITY_ID,
)?.user ?? null;
const canManage = currentUser?.role === 'owner' || currentUser?.role === 'admin';
return (
<main className="max-w-5xl mx-auto px-4 sm:px-6">
<section className="pt-8 pb-6">
<div className="flex items-center justify-between gap-3 mb-3">
<h2 className={SECTION_HEADING_CLASS}>
다음 내전
</h2>
<div className="flex items-center gap-3">
{canManage && (
<Link
to={`/admin/communities/${DEFAULT_COMMUNITY_ID}/matches`}
className={ADMIN_LINK_CLASS}
>
내전 관리
</Link>
)}
<Link
to={`/communities/${DEFAULT_COMMUNITY_ID}/internal-stats`}
className={STATS_LINK_CLASS}
>
내전 전적 보기
</Link>
</div>
</div>
{nextMatch ? (
<Link to={`/match/${nextMatch.id}`} className="block group">
<div className="border border-base-content/10 bg-base-200/50 hover:bg-base-200 transition-colors">
<div className="p-5 sm:p-6 min-h-[150px]">
<div className="flex flex-col sm:flex-row sm:items-start sm:justify-between gap-4">
<div className="space-y-2.5">
<div className="flex items-center gap-2.5 flex-wrap">
<h3 className={NEXT_MATCH_TITLE_CLASS}>
{nextMatch.title}
</h3>
<StatusBadge status={getStatusLabel(nextMatch.status)} />
</div>
<p className={NEXT_MATCH_DESCRIPTION_CLASS}>
{nextMatch.description}
</p>
<div className={NEXT_MATCH_META_CLASS}>
<span>{nextMatch.date} {nextMatch.time}</span>
<span className="text-base-content/15">|</span>
<span className={NEXT_MATCH_PARTICIPANT_BADGE_CLASS}>
{nextMatch.participants.length}/{nextMatch.maxParticipants}명
</span>
<span className="text-base-content/15">|</span>
<MapBadge map={nextMatch.map} />
</div>
</div>
<div className="sm:shrink-0">
<span className={DETAIL_BUTTON_CLASS}>
상세 보기
</span>
</div>
</div>
</div>
</div>
</Link>
) : (
<div className="border border-base-content/10 bg-base-200/50">
<div className="p-5 sm:p-6 min-h-[150px] flex items-center justify-center text-center text-sm text-base-content/40">
{EMPTY_SCHEDULE_TEXT}
</div>
</div>
)}
</section>
<section className="pb-6">
<h2 className={`${SECTION_HEADING_CLASS} mb-3`}>
예정된 내전
</h2>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-px bg-base-content/10 border border-base-content/10">
{upcomingCards.length > 0 ? (
upcomingCards.map((match) => (
<Link key={match.id} to={`/match/${match.id}`}>
<div className={`${UPCOMING_CARD_FRAME_CLASS} hover:bg-base-200/50 transition-colors cursor-pointer`}>
<div className="flex items-center justify-between mb-2.5">
<h3 className={UPCOMING_CARD_TITLE_CLASS}>
{match.title}
</h3>
<StatusBadge status={getStatusLabel(match.status)} />
</div>
<div className={UPCOMING_CARD_META_CLASS}>
<div>{match.date} {match.time}</div>
<div>
{match.participants.length}/{match.maxParticipants}명
</div>
</div>
<div className="mt-2.5">
<MapBadge map={match.map} />
</div>
</div>
</Link>
))
) : (
<div className={`${UPCOMING_CARD_FRAME_CLASS} sm:col-span-2 lg:col-span-3 flex items-center justify-center text-center text-sm text-base-content/35`}>
{EMPTY_SCHEDULE_TEXT}
</div>
)}
</div>
</section>
<section
className="pb-12"
aria-busy={isHistoryPageLoading}
aria-live="polite"
>
<h2 className={`${SECTION_HEADING_CLASS} mb-3`}>
지난 내전 기록
</h2>
{isHistoryPageLoading ? (
<HistoryRecordsSkeleton rows={HISTORY_MATCH_PAGE_SIZE} />
) : hasHistoryError ? (
<div className="border border-base-content/10 px-4 py-8 text-center text-sm text-red-500">
지난 내전 기록을 불러오지 못했습니다.
</div>
) : (
<>
{/* 데스크톱: 테이블 */}
<div className="hidden md:block border border-base-content/10 overflow-x-auto">
<table className="w-full table-fixed text-left">
<HistoryTableColGroup />
<HistoryTableHeader />
<tbody>
{historyMatches.length > 0 ? (
historyMatches.map((match) => (
<tr
key={match.id}
className={`${HISTORY_TABLE_ROW_CLASS} hover:bg-base-content/[0.02] transition-colors`}
>
<td className="py-2.5 px-4 text-xs text-base-content/40 whitespace-nowrap">
{match.date}
</td>
<td className="py-2.5 px-4">
<Link
to={`/match/${match.id}`}
className="block truncate text-sm font-medium text-base-content hover:text-primary transition-colors"
>
{match.title}
</Link>
</td>
<td className="py-2.5 px-4">
<StatusBadge status={getStatusLabel(match.status)} />
</td>
<td className="py-2.5 px-4">
<MapBadge map={match.map} />
</td>
<td className="py-2.5 px-4 text-xs text-base-content/50 whitespace-nowrap">
{match.participants.length}명
</td>
<td className="py-2.5 px-4 text-xs text-base-content/50 whitespace-nowrap">
<Link
to={`/match/${match.id}`}
className="font-semibold text-base-content/45 hover:text-primary"
>
공식 결과
</Link>
</td>
</tr>
))
) : (
<tr>
<td
colSpan={6}
className="py-6 px-4 text-sm text-base-content/35 text-center"
>
아직 내전 기록이 없습니다.
</td>
</tr>
)}
</tbody>
</table>
</div>
{/* 모바일: 카드 리스트 */}
<div className="md:hidden border border-base-content/10">
{historyMatches.length > 0 ? (
historyMatches.map((match) => {
return (
<Link
key={match.id}
to={`/match/${match.id}`}
className="block border-b border-base-content/5 last:border-b-0 p-3 hover:bg-base-content/[0.02] transition-colors"
>
<div className="flex items-start justify-between gap-3">
<div className="min-w-0 flex-1">
<div className={HISTORY_MOBILE_DATE_CLASS}>
{match.compactDateTime ||
`${match.date} ${match.time}`}
</div>
<div className={HISTORY_MOBILE_TITLE_CLASS}>
{match.title}
</div>
</div>
<div className="shrink-0">
<StatusBadge
status={getStatusLabel(match.status)}
/>
</div>
</div>
<div className="mt-2">
<HistoryMetaText
map={match.map}
participants={match.participants.length}
/>
</div>
</Link>
);
})
) : (
<div className="py-6 px-4 text-sm text-base-content/35 text-center">
아직 내전 기록이 없습니다.
</div>
)}
</div>
</>
)}
{!hasHistoryError && historyTotal > 0 && (
<div className="mt-3 flex justify-end">
<HistoryPagination
page={historyPage}
pageCount={historyPageCount}
total={historyTotal}
isFetching={historyMatchesQuery.isFetching}
onPrevious={goToPreviousHistoryPage}
onNext={goToNextHistoryPage}
/>
</div>
)}
</section>
</main>
);
}