|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect, useCallback } from 'react'; |
| 4 | +import { ReportReason } from '@private-board/shared'; |
| 5 | +import { REPORT_REASON_LABELS } from '@/lib/constants'; |
| 6 | +import { AdminReport, getAdminReports, adminDeletePost, adminDismissReport } from '@/lib/api'; |
| 7 | + |
| 8 | +export default function AdminPage() { |
| 9 | + const [secret, setSecret] = useState(''); |
| 10 | + const [input, setInput] = useState(''); |
| 11 | + const [reports, setReports] = useState<AdminReport[]>([]); |
| 12 | + const [loading, setLoading] = useState(false); |
| 13 | + const [error, setError] = useState(''); |
| 14 | + |
| 15 | + const fetchReports = useCallback(async (s: string) => { |
| 16 | + setLoading(true); |
| 17 | + setError(''); |
| 18 | + try { |
| 19 | + const data = await getAdminReports(s); |
| 20 | + setReports(data); |
| 21 | + } catch (err) { |
| 22 | + if (err instanceof Error && err.message === 'UNAUTHORIZED') { |
| 23 | + setSecret(''); |
| 24 | + sessionStorage.removeItem('adminSecret'); |
| 25 | + setError('인증에 실패했습니다.'); |
| 26 | + } else { |
| 27 | + setError(err instanceof Error ? err.message : '오류가 발생했습니다.'); |
| 28 | + } |
| 29 | + } finally { |
| 30 | + setLoading(false); |
| 31 | + } |
| 32 | + }, []); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + const saved = sessionStorage.getItem('adminSecret'); |
| 36 | + if (saved) { |
| 37 | + setSecret(saved); |
| 38 | + fetchReports(saved); |
| 39 | + } |
| 40 | + }, [fetchReports]); |
| 41 | + |
| 42 | + async function handleLogin(e: React.FormEvent) { |
| 43 | + e.preventDefault(); |
| 44 | + setError(''); |
| 45 | + sessionStorage.setItem('adminSecret', input); |
| 46 | + setSecret(input); |
| 47 | + await fetchReports(input); |
| 48 | + } |
| 49 | + |
| 50 | + function handleLogout() { |
| 51 | + sessionStorage.removeItem('adminSecret'); |
| 52 | + setSecret(''); |
| 53 | + setReports([]); |
| 54 | + setInput(''); |
| 55 | + } |
| 56 | + |
| 57 | + async function handleDelete(slug: string) { |
| 58 | + if (!confirm(`"${slug}" 게시글을 삭제하시겠습니까?`)) return; |
| 59 | + try { |
| 60 | + await adminDeletePost(secret, slug); |
| 61 | + setReports((prev) => prev.filter((r) => r.post.slug !== slug)); |
| 62 | + } catch (err) { |
| 63 | + alert(err instanceof Error ? err.message : '오류가 발생했습니다.'); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + async function handleDismiss(id: string) { |
| 68 | + try { |
| 69 | + await adminDismissReport(secret, id); |
| 70 | + setReports((prev) => prev.filter((r) => r.id !== id)); |
| 71 | + } catch (err) { |
| 72 | + alert(err instanceof Error ? err.message : '오류가 발생했습니다.'); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (!secret) { |
| 77 | + return ( |
| 78 | + <section className="mx-auto max-w-sm px-4 py-16"> |
| 79 | + <div className="rounded-xl border border-border bg-surface-card p-8 shadow-md"> |
| 80 | + <h1 className="mb-6 text-lg font-semibold text-text-primary">관리자</h1> |
| 81 | + <form onSubmit={handleLogin} className="flex flex-col gap-4"> |
| 82 | + <input |
| 83 | + type="password" |
| 84 | + placeholder="관리자 비밀키" |
| 85 | + value={input} |
| 86 | + onChange={(e) => setInput(e.target.value)} |
| 87 | + autoFocus |
| 88 | + className="rounded-lg border border-border bg-surface px-3 py-2.5 text-sm shadow-sm focus:border-border-focus focus:outline-none" |
| 89 | + /> |
| 90 | + {error && <p className="text-xs text-error">{error}</p>} |
| 91 | + <button |
| 92 | + type="submit" |
| 93 | + disabled={!input} |
| 94 | + className="rounded-lg bg-brand-600 px-4 py-2.5 text-sm font-medium text-white hover:bg-brand-700 disabled:opacity-50" |
| 95 | + > |
| 96 | + 로그인 |
| 97 | + </button> |
| 98 | + </form> |
| 99 | + </div> |
| 100 | + </section> |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return ( |
| 105 | + <section className="mx-auto max-w-3xl px-4 py-10"> |
| 106 | + <div className="mb-6 flex items-center justify-between"> |
| 107 | + <h1 className="text-xl font-semibold text-text-primary">신고 관리</h1> |
| 108 | + <div className="flex items-center gap-3"> |
| 109 | + <button |
| 110 | + onClick={() => fetchReports(secret)} |
| 111 | + className="text-sm text-text-secondary hover:text-text-primary" |
| 112 | + > |
| 113 | + 새로고침 |
| 114 | + </button> |
| 115 | + <button |
| 116 | + onClick={handleLogout} |
| 117 | + className="text-sm text-text-secondary hover:text-text-primary" |
| 118 | + > |
| 119 | + 로그아웃 |
| 120 | + </button> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + |
| 124 | + {loading && ( |
| 125 | + <p className="text-sm text-text-secondary">불러오는 중...</p> |
| 126 | + )} |
| 127 | + |
| 128 | + {error && ( |
| 129 | + <p className="text-sm text-error">{error}</p> |
| 130 | + )} |
| 131 | + |
| 132 | + {!loading && reports.length === 0 && ( |
| 133 | + <div className="rounded-xl border border-border bg-surface-card px-6 py-12 text-center"> |
| 134 | + <p className="text-sm text-text-secondary">처리 대기 중인 신고가 없습니다.</p> |
| 135 | + </div> |
| 136 | + )} |
| 137 | + |
| 138 | + <ul className="flex flex-col gap-4"> |
| 139 | + {reports.map((report) => ( |
| 140 | + <li key={report.id} className="rounded-xl border border-border bg-surface-card p-5"> |
| 141 | + <div className="mb-3 flex items-start justify-between gap-4"> |
| 142 | + <div> |
| 143 | + <p className="font-medium text-text-primary">{report.post.title}</p> |
| 144 | + <p className="mt-0.5 text-xs text-text-secondary">/{report.post.slug}</p> |
| 145 | + </div> |
| 146 | + <span className="shrink-0 rounded-full bg-brand-100 px-2.5 py-0.5 text-xs font-medium text-brand-600"> |
| 147 | + {REPORT_REASON_LABELS[report.reason]} |
| 148 | + </span> |
| 149 | + </div> |
| 150 | + |
| 151 | + {report.description && ( |
| 152 | + <p className="mb-3 text-sm text-text-secondary">{report.description}</p> |
| 153 | + )} |
| 154 | + |
| 155 | + <div className="flex items-center justify-between"> |
| 156 | + <p className="text-xs text-text-muted"> |
| 157 | + {report.reporterIp} · {new Date(report.createdAt).toLocaleString('ko-KR')} |
| 158 | + </p> |
| 159 | + <div className="flex gap-2"> |
| 160 | + <button |
| 161 | + onClick={() => handleDismiss(report.id)} |
| 162 | + className="rounded-lg border border-border px-3 py-1.5 text-xs text-text-secondary hover:bg-surface transition-colors" |
| 163 | + > |
| 164 | + 신고 기각 |
| 165 | + </button> |
| 166 | + <button |
| 167 | + onClick={() => handleDelete(report.post.slug)} |
| 168 | + className="rounded-lg bg-error px-3 py-1.5 text-xs font-medium text-white hover:opacity-90 transition-opacity" |
| 169 | + > |
| 170 | + 글 삭제 |
| 171 | + </button> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + </li> |
| 175 | + ))} |
| 176 | + </ul> |
| 177 | + </section> |
| 178 | + ); |
| 179 | +} |
0 commit comments