428 lines
15 KiB
TypeScript
428 lines
15 KiB
TypeScript
import { useMemo, useState } from 'react';
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
import { Link, useParams } from 'react-router';
|
|
import {
|
|
useCancelLegacyImport,
|
|
useLegacyImportJobs,
|
|
useLegacyImportMembers,
|
|
useMe,
|
|
useStartLegacyImport,
|
|
} from '../../api/hooks';
|
|
import type {
|
|
LegacyImportJob,
|
|
LegacyImportMemberWindow,
|
|
StartLegacyImportRequest,
|
|
} from '../../api/types';
|
|
import { DEFAULT_COMMUNITY_ID } from '../../config';
|
|
|
|
const DEFAULT_START = '2024-05-01T00:00:00.000Z';
|
|
|
|
type MemberWindowOverride = { startTime: string; endTime: string };
|
|
|
|
function toInputValue(value: string) {
|
|
const date = new Date(value);
|
|
const local = new Date(date.getTime() - date.getTimezoneOffset() * 60_000);
|
|
return local.toISOString().slice(0, 16);
|
|
}
|
|
|
|
function fromInputValue(value: string) {
|
|
return value ? new Date(value).toISOString() : undefined;
|
|
}
|
|
|
|
function formatDate(value: string | undefined) {
|
|
if (!value) return '-';
|
|
return new Intl.DateTimeFormat('ko-KR', {
|
|
month: 'long',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
}).format(new Date(value));
|
|
}
|
|
|
|
function formatEta(seconds: number) {
|
|
if (seconds < 60) return `${Math.max(1, Math.ceil(seconds))}초`;
|
|
if (seconds < 3600) return `${Math.ceil(seconds / 60)}분`;
|
|
return `${Math.ceil(seconds / 3600)}시간`;
|
|
}
|
|
|
|
function progress(job: LegacyImportJob) {
|
|
if (job.totalMembers === 0) return 100;
|
|
return Math.round((job.processedMembers / job.totalMembers) * 100);
|
|
}
|
|
|
|
function defaultMemberOverride(
|
|
member: LegacyImportMemberWindow,
|
|
): MemberWindowOverride {
|
|
return {
|
|
startTime: toInputValue(member.startTime),
|
|
endTime: toInputValue(member.endTime),
|
|
};
|
|
}
|
|
|
|
function JobCard({
|
|
job,
|
|
onCancel,
|
|
cancelling,
|
|
}: {
|
|
job: LegacyImportJob;
|
|
onCancel: (jobId: string) => void;
|
|
cancelling: boolean;
|
|
}) {
|
|
const running = job.state === 'pending' || job.state === 'running';
|
|
const remaining = Math.max(0, job.totalMembers - job.processedMembers);
|
|
const eta = remaining * (job.averageMemberSeconds ?? 60);
|
|
|
|
return (
|
|
<div className="border border-base-content/10 bg-base-100 p-4">
|
|
<div className="flex flex-col sm:flex-row sm:items-start sm:justify-between gap-3">
|
|
<div>
|
|
<div className="text-xs font-mono text-base-content/35">
|
|
{job.jobId}
|
|
</div>
|
|
<div className="mt-1 flex flex-wrap items-center gap-2">
|
|
<span className="text-sm font-black text-base-content">
|
|
{job.state}
|
|
</span>
|
|
<span className="text-xs text-base-content/40">
|
|
{job.processedMembers}/{job.totalMembers}명 · 발견{' '}
|
|
{job.totalDiscoveredMatchIds} · import {job.totalImportedMatches} ·
|
|
중복 {job.totalSkippedDuplicate} · 에러 {job.totalErrors}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
{running && (
|
|
<button
|
|
type="button"
|
|
className="btn btn-ghost btn-xs px-3 text-red-400"
|
|
disabled={cancelling}
|
|
onClick={() => onCancel(job.jobId)}
|
|
>
|
|
취소
|
|
</button>
|
|
)}
|
|
</div>
|
|
<div className="mt-3 h-2 bg-base-content/8">
|
|
<div
|
|
className="h-full bg-primary"
|
|
style={{ width: `${progress(job)}%` }}
|
|
/>
|
|
</div>
|
|
<div className="mt-2 flex flex-wrap justify-between gap-2 text-xs text-base-content/35">
|
|
<span>
|
|
{formatDate(job.windowStart)} ~ {formatDate(job.windowEnd)}
|
|
</span>
|
|
<span>{running ? `예상 남은 시간 ${formatEta(eta)}` : formatDate(job.completedAt)}</span>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function LegacyImportPage() {
|
|
const { cid } = useParams();
|
|
const communityId = cid ?? DEFAULT_COMMUNITY_ID;
|
|
const queryClient = useQueryClient();
|
|
const meQuery = useMe();
|
|
const [windowStart, setWindowStart] = useState(toInputValue(DEFAULT_START));
|
|
const [windowEnd, setWindowEnd] = useState('');
|
|
const [message, setMessage] = useState<string | null>(null);
|
|
const [overrides, setOverrides] = useState<
|
|
Record<string, MemberWindowOverride>
|
|
>({});
|
|
const currentUser =
|
|
meQuery.data?.memberships.find(
|
|
(membership) => membership.community?._id === communityId,
|
|
)?.user ?? null;
|
|
const canManage = currentUser?.role === 'owner' || currentUser?.role === 'admin';
|
|
const membersQuery = useLegacyImportMembers(
|
|
{
|
|
windowStart: fromInputValue(windowStart),
|
|
windowEnd: fromInputValue(windowEnd),
|
|
},
|
|
communityId,
|
|
canManage,
|
|
);
|
|
const jobsQuery = useLegacyImportJobs(communityId, canManage);
|
|
const startMutation = useStartLegacyImport(communityId);
|
|
const cancelMutation = useCancelLegacyImport(communityId);
|
|
const members = membersQuery.data?.members ?? [];
|
|
|
|
const manageableMemberships =
|
|
meQuery.data?.memberships.filter((membership) => {
|
|
const role = membership.user.role;
|
|
return Boolean(
|
|
membership.community && (role === 'owner' || role === 'admin'),
|
|
);
|
|
}) ?? [];
|
|
const runningJob = useMemo(
|
|
() =>
|
|
jobsQuery.data?.jobs.find(
|
|
(job) => job.state === 'pending' || job.state === 'running',
|
|
),
|
|
[jobsQuery.data?.jobs],
|
|
);
|
|
|
|
async function invalidate() {
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: ['legacy-import-jobs', communityId] }),
|
|
queryClient.invalidateQueries({ queryKey: ['legacy-matches', communityId] }),
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['community-internal-stats-matches', communityId],
|
|
}),
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['community-internal-stats-leaderboard', communityId],
|
|
}),
|
|
]);
|
|
}
|
|
|
|
function handleStart() {
|
|
if (!window.confirm('과거 type=tourney 매치 백필을 시작할까요? 야간/유휴 시간대 실행을 권장합니다.')) {
|
|
return;
|
|
}
|
|
const payload: StartLegacyImportRequest = {
|
|
windowStart: fromInputValue(windowStart),
|
|
windowEnd: fromInputValue(windowEnd),
|
|
memberOverrides: members.map((member) => {
|
|
const override =
|
|
overrides[member.userId] ?? defaultMemberOverride(member);
|
|
return {
|
|
userId: member.userId,
|
|
startTime: fromInputValue(override.startTime),
|
|
endTime: fromInputValue(override.endTime),
|
|
};
|
|
}),
|
|
};
|
|
startMutation.mutate(payload, {
|
|
onSuccess: async (result) => {
|
|
await invalidate();
|
|
setMessage(
|
|
`백필 작업을 시작했습니다. ${result.totalMembers}명, 예상 ${formatEta(result.etaSeconds)}.`,
|
|
);
|
|
},
|
|
onError: (error) => {
|
|
setMessage(error instanceof Error ? error.message : '백필 시작 실패');
|
|
},
|
|
});
|
|
}
|
|
|
|
if (!communityId) {
|
|
return (
|
|
<main className="max-w-5xl mx-auto px-4 py-20 text-center">
|
|
<h1 className="text-lg font-bold">커뮤니티 설정이 필요합니다</h1>
|
|
</main>
|
|
);
|
|
}
|
|
|
|
if (meQuery.isPending) {
|
|
return (
|
|
<main className="max-w-5xl mx-auto px-4 py-20 text-center">
|
|
<h1 className="text-lg font-bold">권한 확인 중...</h1>
|
|
</main>
|
|
);
|
|
}
|
|
|
|
if (!canManage) {
|
|
return (
|
|
<main className="max-w-5xl mx-auto px-4 py-20 text-center">
|
|
<h1 className="text-lg font-bold">권한이 필요합니다</h1>
|
|
<p className="mt-2 text-sm text-base-content/40">
|
|
Legacy 백필은 owner/admin만 실행할 수 있습니다.
|
|
</p>
|
|
{manageableMemberships.length > 0 && (
|
|
<div className="mt-5 flex flex-wrap justify-center gap-2">
|
|
{manageableMemberships.map((membership) => (
|
|
<Link
|
|
key={membership.community?._id ?? membership.user._id}
|
|
to={`/admin/communities/${membership.community?._id}/legacy-import`}
|
|
className="btn btn-primary btn-sm px-4 text-xs"
|
|
>
|
|
{membership.community?.name ?? '관리 가능한 커뮤니티'}로 이동
|
|
</Link>
|
|
))}
|
|
</div>
|
|
)}
|
|
</main>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<main className="max-w-6xl mx-auto px-4 sm:px-6 py-6">
|
|
<Link to="/" className="inline-flex text-xs text-base-content/40 hover:text-base-content mb-5">
|
|
← 목록으로
|
|
</Link>
|
|
<div className="mb-6 flex flex-col sm:flex-row sm:items-end sm:justify-between gap-3">
|
|
<div>
|
|
<h1 className="text-2xl font-black text-base-content">
|
|
Legacy 내전 백필
|
|
</h1>
|
|
<p className="mt-1 text-sm text-base-content/45">
|
|
커뮤니티 멤버 PUUID로 과거 `type=tourney` 매치를 찾아 내전 기록으로 import합니다.
|
|
</p>
|
|
</div>
|
|
<Link
|
|
to={`/admin/communities/${communityId}/legacy-matches`}
|
|
className="btn btn-ghost btn-sm px-4 text-xs"
|
|
>
|
|
Legacy 매치 정리
|
|
</Link>
|
|
</div>
|
|
|
|
<section className="border border-base-content/10 bg-base-100 p-4 mb-6">
|
|
<h2 className="text-sm font-bold mb-3">검색 윈도우</h2>
|
|
<div className="grid sm:grid-cols-[1fr_1fr_auto] gap-3 items-end">
|
|
<label className="text-xs text-base-content/45">
|
|
시작일
|
|
<input
|
|
type="datetime-local"
|
|
value={windowStart}
|
|
onChange={(event) => setWindowStart(event.target.value)}
|
|
className="mt-1 w-full border border-base-content/10 bg-transparent px-3 py-2 text-xs"
|
|
/>
|
|
</label>
|
|
<label className="text-xs text-base-content/45">
|
|
종료일
|
|
<input
|
|
type="datetime-local"
|
|
value={windowEnd}
|
|
onChange={(event) => setWindowEnd(event.target.value)}
|
|
placeholder="비우면 지금"
|
|
className="mt-1 w-full border border-base-content/10 bg-transparent px-3 py-2 text-xs"
|
|
/>
|
|
</label>
|
|
<button
|
|
type="button"
|
|
className="btn btn-primary btn-sm px-5 text-xs"
|
|
disabled={startMutation.isPending || Boolean(runningJob)}
|
|
onClick={handleStart}
|
|
>
|
|
{runningJob
|
|
? '작업 진행 중'
|
|
: startMutation.isPending
|
|
? '시작 중...'
|
|
: '백필 시작'}
|
|
</button>
|
|
</div>
|
|
{message && (
|
|
<div className="mt-3 border border-primary/15 bg-primary/5 px-3 py-2 text-xs text-base-content/55">
|
|
{message}
|
|
</div>
|
|
)}
|
|
</section>
|
|
|
|
<section className="border border-base-content/10 bg-base-100 overflow-hidden mb-6">
|
|
<div className="px-4 py-3 border-b border-base-content/10">
|
|
<h2 className="text-sm font-bold">멤버별 윈도우</h2>
|
|
<p className="mt-1 text-xs text-base-content/35">
|
|
기본 시작일은 2024-05-01이며, 필요하면 멤버별 시작/종료 시간을 조정할 수 있습니다.
|
|
비활성 멤버는 종료일이 마지막 업데이트 시각으로 잡힙니다.
|
|
</p>
|
|
</div>
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full min-w-[900px] text-left">
|
|
<thead>
|
|
<tr className="bg-base-200/40 border-b border-base-content/10 text-xs uppercase tracking-wider text-base-content/35">
|
|
<th className="px-3 py-2">멤버</th>
|
|
<th className="px-3 py-2">Riot ID</th>
|
|
<th className="px-3 py-2">상태</th>
|
|
<th className="px-3 py-2">시작</th>
|
|
<th className="px-3 py-2">종료</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{members.map((member) => {
|
|
const override =
|
|
overrides[member.userId] ?? defaultMemberOverride(member);
|
|
return (
|
|
<tr
|
|
key={member.userId}
|
|
className="border-b border-base-content/5"
|
|
>
|
|
<td className="px-3 py-2 text-sm font-bold">
|
|
{member.displayName}
|
|
</td>
|
|
<td className="px-3 py-2 text-xs text-base-content/45">
|
|
{member.gameName}#{member.tagLine}
|
|
</td>
|
|
<td className="px-3 py-2 text-xs">
|
|
{member.isActivated ? '활성' : '비활성'}
|
|
</td>
|
|
<td className="px-3 py-2">
|
|
<input
|
|
type="datetime-local"
|
|
value={override.startTime}
|
|
onChange={(event) =>
|
|
setOverrides((current) => {
|
|
const currentOverride =
|
|
current[member.userId] ??
|
|
defaultMemberOverride(member);
|
|
return {
|
|
...current,
|
|
[member.userId]: {
|
|
...currentOverride,
|
|
startTime: event.target.value,
|
|
},
|
|
};
|
|
})
|
|
}
|
|
className="border border-base-content/10 bg-transparent px-2 py-1 text-xs"
|
|
/>
|
|
</td>
|
|
<td className="px-3 py-2">
|
|
<input
|
|
type="datetime-local"
|
|
value={override.endTime}
|
|
onChange={(event) =>
|
|
setOverrides((current) => {
|
|
const currentOverride =
|
|
current[member.userId] ??
|
|
defaultMemberOverride(member);
|
|
return {
|
|
...current,
|
|
[member.userId]: {
|
|
...currentOverride,
|
|
endTime: event.target.value,
|
|
},
|
|
};
|
|
})
|
|
}
|
|
className="border border-base-content/10 bg-transparent px-2 py-1 text-xs"
|
|
/>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
{members.length === 0 && (
|
|
<tr>
|
|
<td colSpan={5} className="px-4 py-8 text-center text-sm text-base-content/35">
|
|
Riot 계정이 있는 멤버가 없습니다.
|
|
</td>
|
|
</tr>
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
|
|
<section className="space-y-3">
|
|
<h2 className="text-sm font-bold">작업 이력</h2>
|
|
{(jobsQuery.data?.jobs ?? []).map((job) => (
|
|
<JobCard
|
|
key={job.jobId}
|
|
job={job}
|
|
cancelling={cancelMutation.isPending}
|
|
onCancel={(jobId) =>
|
|
cancelMutation.mutate(jobId, {
|
|
onSuccess: invalidate,
|
|
})
|
|
}
|
|
/>
|
|
))}
|
|
{(jobsQuery.data?.jobs ?? []).length === 0 && (
|
|
<div className="border border-base-content/10 bg-base-100 px-4 py-8 text-center text-sm text-base-content/35">
|
|
아직 실행한 백필 작업이 없습니다.
|
|
</div>
|
|
)}
|
|
</section>
|
|
</main>
|
|
);
|
|
}
|