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 = '예정된 매치가 없습니다.';
function SkeletonBlock({ className }: { className: string }) {
return (
);
}
function HomePageSkeleton() {
const upcomingSkeletons = Array.from({ length: UPCOMING_GRID_MATCH_LIMIT });
const historySkeletons = Array.from({ length: 5 });
return (
매치 목록을 불러오는 중...
{upcomingSkeletons.map((_, index) => (
))}
{['날짜', '내전', '상태', '맵', '참가', '결과'].map((label) => (
|
|
))}
{historySkeletons.map((_, index) => (
|
|
|
|
|
|
|
))}
{historySkeletons.map((_, index) => (
))}
);
}
function StatusBadge({ status }: { status: string }) {
const styles: Record = {
모집중: '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 (
{status}
);
}
function MapBadge({ map }: { map: string }) {
return (
{map}
);
}
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,
result,
}: {
map: string;
participants: number;
result?: string;
}) {
return (
{map}
·
{participants}명
{result && (
<>
·
{result}
>
)}
);
}
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 (
총 {total}개 · {page}/{pageCount}페이지
);
}
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 (
`apps/web/.env.development`의 `VITE_DEFAULT_COMMUNITY_ID`를 먼저 설정해주세요.
);
}
if (upcomingMatchesQuery.isLoading || historyMatchesQuery.isLoading) {
return ;
}
if (upcomingMatchesQuery.error || historyMatchesQuery.error) {
return (
매치 목록을 불러오지 못했습니다.
);
}
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 historyPageFromApi = historyMatchesQuery.data?.page ?? historyPage;
const historyPageCount = Math.max(
1,
Math.ceil(historyTotal / historyLimit),
);
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 (
다음 내전
{canManage && (
내전 관리
)}
내전 전적 보기
{nextMatch ? (
{nextMatch.title}
{nextMatch.description}
{nextMatch.date} {nextMatch.time}
|
{nextMatch.participants.length}/{nextMatch.maxParticipants}명
|
상세 보기
) : (
)}
예정된 내전
{upcomingCards.length > 0 ? (
upcomingCards.map((match) => (
{match.title}
{match.date} {match.time}
{match.participants.length}/{match.maxParticipants}명
))
) : (
{EMPTY_SCHEDULE_TEXT}
)}
지난 내전 기록
{/* 데스크톱: 테이블 */}
| 날짜 |
내전 |
상태 |
맵 |
참가 |
결과 |
{historyMatches.length > 0 ? (
historyMatches.map((match) => (
|
{match.date}
|
{match.title}
|
|
|
{match.participants.length}명
|
{match.result?.winner ? `${match.result.winner} 승리` : '-'}
|
))
) : (
|
아직 내전 기록이 없습니다.
|
)}
{/* 모바일: 카드 리스트 */}
{historyMatches.length > 0 ? (
historyMatches.map((match) => {
const resultLabel = match.result?.winner
? `${match.result.winner} 승리`
: undefined;
return (
{match.compactDateTime || `${match.date} ${match.time}`}
{match.title}
);
})
) : (
아직 내전 기록이 없습니다.
)}
{historyTotal > 0 && (
)}
);
}