Files
ss-front-react/src/features/bbs/components/AdminPage.tsx
T
headporterandClaude Opus 5.5 2cc3b74b71
SpecialSource Frontend (React) CD / deploy (push) Successful in 58s
feat : TUI 주크박스 (RAR을 브라우저에서 풀어 재생: node-unrar-js), 학습노트 18
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 17:07:03 +09:00

140 lines
5.1 KiB
TypeScript

import { useEffect, useState } from "react";
import { fetchAdminStats, fetchVisitors } from "../api";
import type { AdminStats, VisitorRow } from "../types";
import { bar } from "./MediaList";
import styles from "./bbs.module.css";
const PAGE = 50; // 백엔드가 한 번에 주는 접속 기록 수
interface Props {
token: string;
rev: number; // "L"을 누르면 +1 → 다시 불러오기
onError: (e: unknown) => void;
}
/** 브라우저 정보(User-Agent) → "Chrome · Windows" 처럼 짧게. 전체 값은 마우스를 올리면 보임 */
const agent = (ua: string | null) => {
if (!ua) return "-";
if (/bot|crawl|spider|curl|python|java\//i.test(ua)) return "봇·프로그램";
const browser = /Edg\//.test(ua) ? "Edge" : /Chrome\//.test(ua) ? "Chrome" : /Firefox\//.test(ua) ? "Firefox" : /Safari\//.test(ua) ? "Safari" : "기타";
const os = /Android/.test(ua) ? "Android" : /iPhone|iPad/.test(ua) ? "iOS" : /Windows/.test(ua) ? "Windows" : /Mac OS/.test(ua) ? "Mac" : /Linux/.test(ua) ? "Linux" : "?";
return `${browser} · ${os}`;
};
/** [9] 관리자 (SYSOP): 방문 통계 + 접속 기록 (상세 기록은 30일 보관) */
export default function AdminPage({ token, rev, onError }: Props) {
const [stats, setStats] = useState<AdminStats | null>(null);
const [rows, setRows] = useState<VisitorRow[] | null>(null);
const [last, setLast] = useState(true);
useEffect(() => {
let stale = false;
Promise.all([fetchAdminStats(token), fetchVisitors(token)])
.then(([s, r]) => {
if (stale) return;
setStats(s);
setRows(r);
setLast(r.length < PAGE);
})
.catch((e) => !stale && onError(e));
return () => {
stale = true;
};
}, [token, rev, onError]);
const more = async () => {
if (!rows?.length) return;
try {
const r = await fetchVisitors(token, rows.at(-1)!.id);
setRows([...rows, ...r]);
setLast(r.length < PAGE);
} catch (e) {
onError(e);
}
};
const peak = Math.max(1, ...(stats?.days.map((d) => d.visitors) ?? [])); // 막대 100% 기준
return (
<>
<section className={`${styles.win} ${styles.full}`}>
<span className={styles.cap}>═ [9] 관리자 · 방문 통계 ═</span>
{!stats ? (
<div className={styles.dim}>불러오는 중...</div>
) : (
<>
<div>
<span className={styles.accent}>지금 접속</span> {stats.online}명 · <span className={styles.accent}>오늘 방문</span>{" "}
{stats.today}명 ({stats.todayViews}회) · <span className={styles.accent}>전체 방문</span> {stats.total}명
</div>
<hr className={styles.rule} />
<table className={styles.tbl}>
<thead>
<tr>
<th>날짜</th>
<th>방문자</th>
<th>방문 수</th>
</tr>
</thead>
<tbody>
{stats.days.map((d) => (
<tr key={d.date}>
<td>{d.date.slice(5).replace("-", ".")}</td>
<td className={styles.accent}>
{bar(Math.round((d.visitors / peak) * 100), 10).replace(/ \d+%$/, "")} {d.visitors}
</td>
<td>{d.views}</td>
</tr>
))}
</tbody>
</table>
</>
)}
<div className={styles.footer}>방문자 = 하루 동안의 서로 다른 IP 수 · 방문 수 = 같은 IP·사용자는 1시간에 한 번만 셈</div>
</section>
<section className={`${styles.win} ${styles.full}`}>
<span className={styles.cap}>═ 접속 기록 (최근 30일) ═</span>
{!rows ? (
<div className={styles.dim}>불러오는 중...</div>
) : rows.length === 0 ? (
<div className={styles.dim}>기록이 없습니다.</div>
) : (
<div className={styles.scrollX}>
<table className={styles.tbl}>
<thead>
<tr>
<th>시각</th>
<th>사용자</th>
<th>IP</th>
<th>브라우저</th>
</tr>
</thead>
<tbody>
{rows.map((v) => (
<tr key={v.id}>
<td>{v.at.slice(5, 16).replace("T", " ").replace("-", ".")}</td>
<td className={v.username === "anonymous" ? styles.dim : styles.accent}>
{v.username === "anonymous" ? "손님" : v.username}
</td>
<td>{v.ip}</td>
<td title={v.userAgent ?? ""}>{agent(v.userAgent)}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
{rows && rows.length > 0 && !last && (
<button type="button" className={styles.linkBtn} onClick={more}>
[더 보기]
</button>
)}
<div className={styles.footer}>
<b>L</b>=새로고침 · <b>M</b>=메인
</div>
</section>
</>
);
}