feat(web): add community stats pages

This commit is contained in:
dahyeon committed 2026-04-25 01:38:00 +09:00
1 parent 956e45ee59
commit f266d1de83
2 files changed
+687

No files matched your search

@@ -0,0 +1,406 @@
import { Link, useLocation, useSearchParams } from 'react-router';
import {
useCommunity,
useCommunityInternalStatsMatches,
useCommunityLeaderboard,
useDdragonManifest,
useTeamInsights,
} from '../api/hooks';
import type {
ApiInternalMatchCard,
ApiLeaderHighlight,
ApiLeaderboardRow,
TeamSide,
} from '../api/types';
import { DEFAULT_COMMUNITY_ID } from '../config';
import { championDisplayName, championIconUrl, profileIconUrl } from '../lib/ddragon';
import { sideShortLabel, teamIndexColorClasses, teamIndexLabel } from '../lib/league';
function formatDateTime(value: string) {
return new Intl.DateTimeFormat('ko-KR', {
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
}).format(new Date(value));
}
function formatDuration(seconds: number) {
const minutes = Math.floor(seconds / 60);
const rest = seconds % 60;
return `${minutes}:${String(rest).padStart(2, '0')}`;
}
function sideLabel(side: TeamSide) {
return sideShortLabel(side);
}
function sideText(side: TeamSide) {
return side === 'blue' ? 'text-blue-400' : 'text-red-400';
}
function ratioClass(value: number) {
if (value >= 55) return 'text-blue-400';
if (value >= 50) return 'text-amber-400';
return 'text-base-content/45';
}
function ChampionChip({
championId,
manifest,
}: {
championId: number;
manifest: ReturnType<typeof useDdragonManifest>['data'];
}) {
const icon = championIconUrl(manifest, championId);
const name = championDisplayName(manifest, championId) ?? String(championId);
return (
<span className="inline-flex items-center gap-1.5">
<span className="w-5 h-5 bg-base-content/10 overflow-hidden">
{icon ? <img src={icon} alt="" className="w-full h-full object-cover" /> : null}
</span>
<span>{name}</span>
</span>
);
}
function MatchCard({
match,
manifest,
}: {
match: ApiInternalMatchCard;
manifest: ReturnType<typeof useDdragonManifest>['data'];
}) {
const primarySeries = match.seriesResults[0];
const teamName = (teamIndex: number) =>
teamIndexLabel(
teamIndex,
match.teams.find((team) => team.teamIndex === teamIndex)?.name,
);
return (
<Link
to={`/communities/${DEFAULT_COMMUNITY_ID}/internal-stats/match/${match.matchId}`}
className="block group"
>
<article className="border border-base-content/10 bg-base-100 hover:border-primary/25 transition-colors">
<div className="p-4 grid lg:grid-cols-[1fr_auto_1fr] gap-4 items-center">
<div className="min-w-0">
<div className="text-[11px] text-base-content/35">
{formatDateTime(match.scheduledAt)}
</div>
<h3 className="mt-1 text-base font-black text-base-content truncate group-hover:text-primary">
{match.title}
</h3>
<div className="mt-2 flex flex-wrap gap-1.5">
{match.games.map((game) => (
<span
key={game.gameNumber}
className="inline-flex items-center gap-1 border border-base-content/10 px-2 py-1 text-[10px] text-base-content/45"
>
{game.gameNumber}경기
{game.winnerSide && game.winnerTeamIndex ? (
<span className={sideText(game.winnerSide)}>
{teamName(game.winnerTeamIndex)} 승 ({sideLabel(game.winnerSide)})
</span>
) : (
'대기'
)}
<span>{formatDuration(game.gameDuration)}</span>
</span>
))}
</div>
</div>
<div className="text-center px-8">
{primarySeries && (
<div className="flex items-center justify-center gap-5">
<div className={primarySeries.winnerTeamIndex === primarySeries.teamAIndex ? teamIndexColorClasses(primarySeries.teamAIndex).text : 'text-base-content/30'}>
<div className="text-4xl font-black tabular-nums">{primarySeries.teamAWins}</div>
<div className="text-[10px] font-bold uppercase tracking-wider">
{teamName(primarySeries.teamAIndex)}
</div>
</div>
<div className="text-base-content/15 text-xs font-black">VS</div>
<div className={primarySeries.winnerTeamIndex === primarySeries.teamBIndex ? teamIndexColorClasses(primarySeries.teamBIndex).text : 'text-base-content/30'}>
<div className="text-4xl font-black tabular-nums">{primarySeries.teamBWins}</div>
<div className="text-[10px] font-bold uppercase tracking-wider">
{teamName(primarySeries.teamBIndex)}
</div>
</div>
</div>
)}
</div>
<div className="grid sm:grid-cols-2 gap-2">
{match.teams.map((team) => (
<div
key={team.teamIndex}
className={`border ${teamIndexColorClasses(team.teamIndex).border} p-2`}
>
<div className={`text-[10px] font-black uppercase tracking-wider ${teamIndexColorClasses(team.teamIndex).text}`}>
{team.name}
</div>
<div className="mt-2 space-y-1">
{team.members.slice(0, 5).map((member) => (
<div key={member.userId} className="flex items-center gap-1.5 min-w-0">
<span className="w-5 h-5 bg-base-content/10 overflow-hidden shrink-0">
<img
src={profileIconUrl(manifest, member.profileIconId)}
alt=""
className="w-full h-full object-cover"
loading="lazy"
/>
</span>
<span className="text-[11px] truncate text-base-content/65">
{member.summonerName}
</span>
</div>
))}
</div>
</div>
))}
</div>
</div>
</article>
</Link>
);
}
function HighlightCard({
title,
highlight,
suffix = '',
}: {
title: string;
highlight: ApiLeaderHighlight | null | undefined;
suffix?: string;
}) {
return (
<div className="border border-base-content/10 bg-base-100 p-4">
<div className="text-[10px] font-black uppercase tracking-wider text-base-content/30">
{title}
</div>
<div className="mt-2 text-xl font-black text-base-content tabular-nums">
{highlight ? `${highlight.metric}${suffix}` : '-'}
</div>
<div className="mt-1 text-[12px] text-base-content/45">
{highlight?.summonerName ?? '기록 없음'}
</div>
</div>
);
}
function LeaderboardTable({
rows,
manifest,
}: {
rows: ApiLeaderboardRow[];
manifest: ReturnType<typeof useDdragonManifest>['data'];
}) {
const location = useLocation();
const historyBackTo = `${location.pathname}${location.search}${location.hash}`;
return (
<div className="border border-base-content/10 bg-base-100 overflow-hidden">
<div className="overflow-x-auto">
<table className="w-full text-left min-w-[920px]">
<thead>
<tr className="border-b border-base-content/10 bg-base-200/50 text-[11px] uppercase tracking-wider text-base-content/35">
<th className="px-3 py-2.5">#</th>
<th className="px-3 py-2.5">소환사</th>
<th className="px-3 py-2.5 text-center">승률</th>
<th className="px-3 py-2.5 text-center">게임</th>
<th className="px-3 py-2.5 text-center">KDA</th>
<th className="px-3 py-2.5 text-center">GPM</th>
<th className="px-3 py-2.5 text-center">킬관여</th>
<th className="px-3 py-2.5 text-center">분당 CS</th>
<th className="px-3 py-2.5">주 챔피언</th>
</tr>
</thead>
<tbody>
{rows.map((row, index) => (
<tr key={row.userId} className="border-b border-base-content/5 last:border-b-0">
<td className="px-3 py-2.5 text-[11px] text-base-content/30">
{index + 1}
</td>
<td className="px-3 py-2.5">
<Link
to={`/users/${row.userId}/history?queueGroup=internal`}
state={{ backTo: historyBackTo }}
className="flex items-center gap-2 group"
>
<span className="w-7 h-7 bg-base-content/10 overflow-hidden">
<img
src={profileIconUrl(manifest, row.profileIconId)}
alt=""
className="w-full h-full object-cover"
loading="lazy"
/>
</span>
<span className="font-bold text-[13px] group-hover:text-primary">
{row.summonerName}
</span>
</Link>
</td>
<td className={`px-3 py-2.5 text-center text-[13px] font-black ${ratioClass(row.winRate)}`}>
{row.winRate}%
</td>
<td className="px-3 py-2.5 text-center text-[12px] tabular-nums">
{row.games}
</td>
<td className="px-3 py-2.5 text-center text-[12px] font-bold tabular-nums">
{row.kda}
</td>
<td className="px-3 py-2.5 text-center text-[12px] tabular-nums">
{row.avgGoldPerMin}
</td>
<td className="px-3 py-2.5 text-center text-[12px] tabular-nums">
{row.avgKillParticipation}%
</td>
<td className="px-3 py-2.5 text-center text-[12px] tabular-nums">
{row.avgCsPerMin}
</td>
<td className="px-3 py-2.5">
<div className="flex flex-wrap gap-2 text-[11px] text-base-content/55">
{row.topChampions.map((champion) => (
<ChampionChip
key={champion.championId}
championId={champion.championId}
manifest={manifest}
/>
))}
</div>
</td>
</tr>
))}
{rows.length === 0 && (
<tr>
<td colSpan={9} className="py-10 text-center text-sm text-base-content/35">
아직 집계할 내전 기록이 없습니다.
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
);
}
export default function CommunityInternalStatsPage() {
const [searchParams] = useSearchParams();
const championIdParam = Number(searchParams.get('championId'));
const championId =
Number.isFinite(championIdParam) && championIdParam > 0
? championIdParam
: undefined;
const filters = {
seasonId: searchParams.get('seasonId') ?? undefined,
championId,
limit: 12,
};
const communityQuery = useCommunity(DEFAULT_COMMUNITY_ID);
const matchesQuery = useCommunityInternalStatsMatches(filters);
const leaderboardQuery = useCommunityLeaderboard(filters);
const insightsQuery = useTeamInsights(filters);
const { data: ddragon } = useDdragonManifest();
const matches = matchesQuery.data?.pages.flatMap((page) => page.matches) ?? [];
const season =
leaderboardQuery.data?.season ?? matchesQuery.data?.pages[0]?.season ?? null;
if (!DEFAULT_COMMUNITY_ID) {
return (
<main className="max-w-6xl mx-auto px-4 py-20 text-center text-sm text-amber-600">
`VITE_DEFAULT_COMMUNITY_ID` 설정이 필요합니다.
</main>
);
}
return (
<main className="max-w-6xl mx-auto px-4 sm:px-6 py-6">
<Link to="/" className="text-xs text-base-content/40 hover:text-base-content">
← 홈으로
</Link>
<section className="mt-5 mb-6 border border-base-content/10 bg-base-100 p-5">
<div className="flex flex-col sm:flex-row sm:items-end sm:justify-between gap-4">
<div>
<p className="text-[11px] font-black uppercase tracking-[0.24em] text-primary">
Community Records
</p>
<h1 className="mt-2 text-3xl font-black text-base-content">
{communityQuery.data?.name ?? '커뮤니티'} 내전 전적
</h1>
<p className="mt-2 text-sm text-base-content/45">
토너먼트 코드로 수집된 결과를 기준으로 매치, 선수 순위, 팀 인사이트를 보여줍니다.
</p>
</div>
<div className="border border-base-content/10 px-4 py-3 text-sm text-base-content/55">
<div className="text-[10px] uppercase tracking-wider text-base-content/30">
시즌
</div>
<div className="mt-1 font-bold">{season?.label ?? '현재 시즌'}</div>
</div>
</div>
</section>
<section className="grid sm:grid-cols-2 lg:grid-cols-5 gap-3 mb-6">
<HighlightCard title="최고 KDA" highlight={insightsQuery.data?.topKda} />
<HighlightCard title="누적 킬 1위" highlight={insightsQuery.data?.mostKills} />
<HighlightCard title="단일 경기 GPM" highlight={insightsQuery.data?.topTeamGpm} />
<HighlightCard title="킬 관여율" highlight={insightsQuery.data?.topKillParticipation} suffix="%" />
<HighlightCard title="분당 CS" highlight={insightsQuery.data?.bestFarm} />
</section>
<section className="mb-8">
<div className="flex items-center justify-between gap-3 mb-3">
<h2 className="text-sm font-black text-base-content">최근 내전</h2>
<div className="text-[11px] text-base-content/35">
블루 승률 {insightsQuery.data?.sideWinRate.blue ?? 0}% · 레드 승률{' '}
{insightsQuery.data?.sideWinRate.red ?? 0}%
</div>
</div>
{matchesQuery.isLoading ? (
<div className="border border-base-content/10 bg-base-100 px-4 py-12 text-center text-sm text-base-content/35">
내전 기록을 불러오는 중...
</div>
) : (
<div className="space-y-3">
{matches.map((match) => (
<MatchCard key={match.matchId} match={match} manifest={ddragon} />
))}
{matches.length === 0 && (
<div className="border border-base-content/10 bg-base-100 px-4 py-12 text-center text-sm text-base-content/35">
아직 수집된 내전 결과가 없습니다.
</div>
)}
</div>
)}
{matchesQuery.hasNextPage && (
<button
type="button"
className="mt-3 w-full border border-base-content/15 bg-base-100 py-3 text-sm font-bold text-base-content/60 hover:text-primary hover:border-primary/30 disabled:opacity-40"
disabled={matchesQuery.isFetchingNextPage}
onClick={() => matchesQuery.fetchNextPage()}
>
{matchesQuery.isFetchingNextPage ? '불러오는 중...' : '+ 더 보기'}
</button>
)}
</section>
<section>
<div className="flex items-center justify-between gap-3 mb-3">
<h2 className="text-sm font-black text-base-content">선수 순위</h2>
<div className="text-[11px] text-base-content/35">
포지션은 Riot Match의 라인 정보를 기준으로 합니다.
</div>
</div>
<LeaderboardTable
rows={leaderboardQuery.data?.rows ?? []}
manifest={ddragon}
/>
</section>
</main>
);
}