-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.html
More file actions
278 lines (259 loc) · 8.77 KB
/
Copy pathadmin.html
File metadata and controls
278 lines (259 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Admin — Moderation</title>
<meta name="robots" content="noindex, nofollow"/>
<link rel="stylesheet" href="style.css"/>
<style>
body {
background: var(--navy-deep);
min-height: 100vh;
font-family: var(--font-body);
color: var(--white-pure);
}
.admin-wrap { max-width: 760px; margin: 0 auto; padding: 3rem 1.5rem; }
.admin-gate { max-width: 420px; margin: 4rem auto; text-align: center; }
.admin-gate p { color: var(--white-dim); }
.admin-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 2rem;
flex-wrap: wrap;
gap: 1rem;
}
.admin-header h2 {
font-family: var(--font-display);
font-size: 1.6rem;
color: var(--white-pure);
}
.admin-section-title {
font-family: var(--font-mono);
font-size: 0.78rem;
letter-spacing: 0.1em;
text-transform: uppercase;
color: var(--cyan-soft);
margin: 2rem 0 1rem;
}
.admin-item { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; }
.admin-item > div:first-child { flex: 1; min-width: 0; }
.delete-btn {
background: rgba(239, 68, 68, 0.15);
border: 1px solid rgba(239, 68, 68, 0.4);
color: #ef4444;
font-family: var(--font-mono);
font-size: 0.7rem;
padding: 0.45rem 0.9rem;
border-radius: 6px;
cursor: pointer;
white-space: nowrap;
transition: background 0.2s;
}
.delete-btn:hover { background: rgba(239, 68, 68, 0.3); }
.logout-link {
font-family: var(--font-mono);
font-size: 0.72rem;
color: var(--cyan-soft);
cursor: pointer;
text-decoration: underline;
}
.logout-link:hover { color: var(--cyan-glow); }
.empty-state { color: var(--white-ghost); font-family: var(--font-mono); font-size: 0.85rem; }
.admin-comment-context {
display: block;
margin-top: 0.4rem;
font-family: var(--font-mono);
font-size: 0.7rem;
color: var(--white-ghost);
}
</style>
</head>
<body>
<div class="admin-wrap">
<!-- GATE -->
<div id="admin-gate" class="admin-gate comments-panel">
<h3>🔒 Admin Access</h3>
<p style="margin-bottom: 1.2rem;">Enter your admin key to manage posts and comments.</p>
<div class="form-row">
<input type="password" id="gate-key-input" placeholder="Admin key"/>
</div>
<button class="btn-submit" onclick="unlock()">Unlock</button>
</div>
<!-- PANEL (hidden until unlocked) -->
<div id="admin-panel" style="display:none;">
<div class="admin-header">
<h2>Moderation</h2>
<span class="logout-link" onclick="logout()">Log out</span>
</div>
<p class="admin-section-title">// posts (<span id="post-count">0</span>)</p>
<div id="admin-posts-list"></div>
<p class="admin-section-title">// recent comments (<span id="comment-count">0</span>)</p>
<div id="admin-comments-list"></div>
</div>
</div>
<script>
// ⚠️ Keep this in sync with the COMMENTS_API_URL in index.html
const COMMENTS_API_URL = 'https://comments-api2.onrender.com';
function getKey() { return sessionStorage.getItem('admin_key') || ''; }
function escapeHtml(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
function formatDate(iso) {
return new Date(iso).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' });
}
async function unlock() {
const key = document.getElementById('gate-key-input').value;
if (!key) return;
try {
// Validation trick: id 0 never exists (ids start at 1), so this call either
// confirms the key (200) or rejects it (401) without deleting anything real.
const res = await fetch(`${COMMENTS_API_URL}/api/comments/0`, {
method: 'DELETE',
headers: { 'x-admin-key': key },
});
if (res.status === 401) {
alert('Wrong admin key.');
return;
}
sessionStorage.setItem('admin_key', key);
showPanel();
} catch (err) {
console.error(err);
alert('Could not reach the API. Check your connection and try again.');
}
}
function logout() {
sessionStorage.removeItem('admin_key');
document.getElementById('admin-panel').style.display = 'none';
document.getElementById('admin-gate').style.display = 'block';
document.getElementById('gate-key-input').value = '';
}
function showPanel() {
document.getElementById('admin-gate').style.display = 'none';
document.getElementById('admin-panel').style.display = 'block';
loadAdminPosts();
loadAdminComments();
}
async function loadAdminPosts() {
const list = document.getElementById('admin-posts-list');
try {
const res = await fetch(`${COMMENTS_API_URL}/api/posts`);
const posts = await res.json();
document.getElementById('post-count').textContent = posts.length;
list.innerHTML = '';
if (posts.length === 0) {
list.innerHTML = '<p class="empty-state">No posts yet.</p>';
return;
}
posts.forEach(post => {
const card = document.createElement('div');
card.className = 'blog-post-card';
card.innerHTML = `
<div class="admin-item">
<div>
<div class="blog-post-header">
<div class="blog-category">${escapeHtml(post.category)}</div>
<h3>${escapeHtml(post.title)}</h3>
</div>
<p>${escapeHtml(post.summary)}</p>
<span class="blog-meta">${formatDate(post.created_at)} · ${post.read_time_minutes} min read · 💬 ${post.comment_count} comments</span>
</div>
<button class="delete-btn">Delete</button>
</div>
`;
card.querySelector('.delete-btn').addEventListener('click', () => deletePost(post.id, post.title));
list.appendChild(card);
});
} catch (err) {
console.error(err);
list.innerHTML = '<p class="empty-state">Failed to load posts.</p>';
}
}
async function deletePost(id, title) {
if (!confirm(`Delete the post "${title}"? This also deletes its comments. This can't be undone.`)) return;
try {
const res = await fetch(`${COMMENTS_API_URL}/api/posts/${id}`, {
method: 'DELETE',
headers: { 'x-admin-key': getKey() },
});
if (!res.ok) throw new Error('Failed');
loadAdminPosts();
loadAdminComments();
} catch (err) {
console.error(err);
alert('Failed to delete post.');
}
}
async function loadAdminComments() {
const list = document.getElementById('admin-comments-list');
try {
const [commentsRes, postsRes] = await Promise.all([
fetch(`${COMMENTS_API_URL}/api/comments`),
fetch(`${COMMENTS_API_URL}/api/posts`),
]);
const comments = await commentsRes.json();
const posts = await postsRes.json();
const postTitleById = {};
posts.forEach(p => { postTitleById[p.id] = p.title; });
document.getElementById('comment-count').textContent = comments.length;
list.innerHTML = '';
if (comments.length === 0) {
list.innerHTML = '<p class="empty-state">No comments yet.</p>';
return;
}
comments.forEach(c => {
const initials = c.name.split(' ').map(w => w[0]).join('').slice(0, 2).toUpperCase();
const context = c.post_id
? `on "${escapeHtml(postTitleById[c.post_id] || 'a deleted post')}"`
: '(general comment)';
const item = document.createElement('div');
item.className = 'comment-item';
item.innerHTML = `
<div class="admin-item">
<div>
<div class="comment-author">
<div class="comment-avatar">${escapeHtml(initials)}</div>
<span class="comment-name">${escapeHtml(c.name)}</span>
<span class="comment-date">${formatDate(c.created_at)}</span>
</div>
<p class="comment-text">${escapeHtml(c.text)}</p>
<span class="admin-comment-context">${context}</span>
</div>
<button class="delete-btn">Delete</button>
</div>
`;
item.querySelector('.delete-btn').addEventListener('click', () => deleteComment(c.id));
list.appendChild(item);
});
} catch (err) {
console.error(err);
list.innerHTML = '<p class="empty-state">Failed to load comments.</p>';
}
}
async function deleteComment(id) {
if (!confirm("Delete this comment? This can't be undone.")) return;
try {
const res = await fetch(`${COMMENTS_API_URL}/api/comments/${id}`, {
method: 'DELETE',
headers: { 'x-admin-key': getKey() },
});
if (!res.ok) throw new Error('Failed');
loadAdminComments();
loadAdminPosts(); // refresh comment counts shown on post cards
} catch (err) {
console.error(err);
alert('Failed to delete comment.');
}
}
// Skip the gate if this tab still has a key (cleared automatically when the tab closes)
if (getKey()) {
showPanel();
}
</script>
<script src="cursor.js"></script>
</body>
</html>