Spaces:
Runtime error
Runtime error
ranga galla commited on
Upload static/app.js with huggingface_hub
Browse files- static/app.js +88 -0
static/app.js
CHANGED
|
@@ -165,3 +165,91 @@ function escapeHtml(text) {
|
|
| 165 |
div.appendChild(document.createTextNode(text));
|
| 166 |
return div.innerHTML;
|
| 167 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
div.appendChild(document.createTextNode(text));
|
| 166 |
return div.innerHTML;
|
| 167 |
}
|
| 168 |
+
|
| 169 |
+
/* ========== Community: Like & Comments ========== */
|
| 170 |
+
|
| 171 |
+
function getSessionId() {
|
| 172 |
+
let id = localStorage.getItem('dict_session_id');
|
| 173 |
+
if (!id) {
|
| 174 |
+
id = crypto.randomUUID();
|
| 175 |
+
localStorage.setItem('dict_session_id', id);
|
| 176 |
+
}
|
| 177 |
+
return id;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
async function loadCommunity() {
|
| 181 |
+
try {
|
| 182 |
+
const resp = await fetch('/api/community');
|
| 183 |
+
if (!resp.ok) return;
|
| 184 |
+
const data = await resp.json();
|
| 185 |
+
|
| 186 |
+
document.getElementById('likeCount').textContent = data.likes || 0;
|
| 187 |
+
document.getElementById('commentCount').textContent = `${data.comments.length} Comment${data.comments.length !== 1 ? 's' : ''}`;
|
| 188 |
+
|
| 189 |
+
const liked = data.liked_users.includes(getSessionId());
|
| 190 |
+
const btn = document.getElementById('likeBtn');
|
| 191 |
+
btn.classList.toggle('liked', liked);
|
| 192 |
+
document.getElementById('likeIcon').textContent = liked ? '❤️' : '🤍';
|
| 193 |
+
|
| 194 |
+
renderComments(data.comments);
|
| 195 |
+
} catch (_) {}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
async function toggleLike() {
|
| 199 |
+
const btn = document.getElementById('likeBtn');
|
| 200 |
+
const isLiked = btn.classList.contains('liked');
|
| 201 |
+
|
| 202 |
+
try {
|
| 203 |
+
const resp = await fetch('/api/like', {
|
| 204 |
+
method: isLiked ? 'DELETE' : 'POST',
|
| 205 |
+
headers: { 'Content-Type': 'application/json' },
|
| 206 |
+
body: JSON.stringify({ session_id: getSessionId() }),
|
| 207 |
+
});
|
| 208 |
+
if (resp.ok) loadCommunity();
|
| 209 |
+
} catch (_) {}
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
function toggleComments() {
|
| 213 |
+
const panel = document.getElementById('commentsPanel');
|
| 214 |
+
panel.style.display = panel.style.display === 'none' ? 'block' : 'none';
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
async function submitComment() {
|
| 218 |
+
const nameEl = document.getElementById('commentName');
|
| 219 |
+
const textEl = document.getElementById('commentText');
|
| 220 |
+
const name = nameEl.value.trim();
|
| 221 |
+
const text = textEl.value.trim();
|
| 222 |
+
if (!text) return;
|
| 223 |
+
|
| 224 |
+
try {
|
| 225 |
+
const resp = await fetch('/api/comment', {
|
| 226 |
+
method: 'POST',
|
| 227 |
+
headers: { 'Content-Type': 'application/json' },
|
| 228 |
+
body: JSON.stringify({ name: name || 'Anonymous', text }),
|
| 229 |
+
});
|
| 230 |
+
if (resp.ok) {
|
| 231 |
+
textEl.value = '';
|
| 232 |
+
loadCommunity();
|
| 233 |
+
}
|
| 234 |
+
} catch (_) {}
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
function renderComments(comments) {
|
| 238 |
+
const list = document.getElementById('commentsList');
|
| 239 |
+
if (!comments.length) {
|
| 240 |
+
list.innerHTML = '<div class="no-comments">No comments yet. Be the first!</div>';
|
| 241 |
+
return;
|
| 242 |
+
}
|
| 243 |
+
list.innerHTML = comments.map(c => `
|
| 244 |
+
<div class="comment-item">
|
| 245 |
+
<div class="comment-meta">
|
| 246 |
+
<span class="comment-name">${escapeHtml(c.name)}</span>
|
| 247 |
+
<span class="comment-time">${escapeHtml(c.time)}</span>
|
| 248 |
+
</div>
|
| 249 |
+
<div class="comment-text">${escapeHtml(c.text)}</div>
|
| 250 |
+
</div>
|
| 251 |
+
`).join('');
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
// Load community data on page load
|
| 255 |
+
loadCommunity();
|