feat: implement mobile comment view with flat list and nested replies navigation

This commit is contained in:
Kerby Martino (aider) committed 2025-05-02 12:33:53 +08:00
1 parent 64fbff589a
commit d303b4cd96
1 file changed
+141 -2
+141 -2
View File
@@ -1,8 +1,147 @@
{% comment %} {% comment %}
Mobile Comment View: Renders a flat list of top-level comments for the current post. Mobile Comment View: Renders a flat list of top-level comments for the current post.
Assumes JavaScript will handle the navigation to nested replies using fragment identifiers.
{% endcomment %} {% endcomment %}
{% assign top_comments = site.comments | where: "parent", nil | sort: "date" %} {% assign post_comments = site.comments | where: "post", page.slug %}
{% assign top_level_comments = post_comments | where_exp: "item", "item.parent == nil or item.parent == ''" | sort: "date" %}
{% for comment in top_comments %} <div id="mobile-comments-list" class="space-y-4">
{% if top_level_comments.size > 0 %}
{% for comment in top_level_comments %}
{% assign direct_replies = site.comments | where: "parent", comment.slug %}
<div class="border-b border-gray-200 pb-3 mb-3" id="mobile-comment-{{ comment.slug }}">
<div class="mt-2 mb-1 text-sm flex flex-col md:flex-row items-start md:items-center md:space-x-2">
<div class="flex items-start space-x-2 mb-2 md:mb-0">
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<i class="fa-solid fa-arrow-up mr-1"></i> {{ comment.upvotes }}
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<i class="fas fa-reply mr-1"></i> REPLY
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<i class="fas fa-comment mr-1"></i> {{ direct_replies.size }}
</span>
</div>
<small class="text-sm font-action">
<a href="#" class="text-sm hover:underline">{{ comment.author }}</a>
<span class="text-text-muted text-sm opacity-75">{{ comment.date | date: "%d %b %Y" }}</span>
</small>
</div>
<p class="text-sm mb-2">{{ comment.content | default: "Missing content" }}</p>
<div class="text-right">
{% if direct_replies.size > 0 %}
<a href="#mobile-comment-thread-{{ comment.slug }}" class="view-replies text-xs text-primary hover:underline" data-comment-slug="{{ comment.slug }}">
View Replies ({{ direct_replies.size }}) <i class="fas fa-chevron-right text-xs"></i>
</a>
{% else %}
<span class="text-xs text-text-muted opacity-75">No Replies</span>
{% endif %}
</div>
</div>
{% endfor %} {% endfor %}
{% else %}
<p class="text-sm text-text-muted">No comments yet.</p>
{% endif %}
</div>
{% comment %}
Placeholder for where JavaScript would load the replies for a selected comment.
This container could be populated by JS when a 'View Replies' link is clicked.
{% endcomment %}
<div id="mobile-comment-thread-view" class="hidden mt-4">
<button class="back-to-list text-primary mb-2 text-sm">
<i class="fas fa-chevron-left text-xs"></i> Back to Comments
</button>
<div id="mobile-comment-thread-content">
<!-- Parent comment and its replies will be loaded here by JavaScript -->
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const commentsList = document.getElementById('mobile-comments-list');
const threadView = document.getElementById('mobile-comment-thread-view');
const threadContent = document.getElementById('mobile-comment-thread-content');
const backToListButton = threadView.querySelector('.back-to-list');
commentsList.addEventListener('click', function(event) {
if (event.target.classList.contains('view-replies')) {
event.preventDefault();
const commentSlug = event.target.getAttribute('data-comment-slug');
loadCommentThread(commentSlug);
}
});
backToListButton.addEventListener('click', function() {
threadView.classList.add('hidden');
commentsList.classList.remove('hidden');
});
function loadCommentThread(commentSlug) {
// Fetch the parent comment and its replies using AJAX or preloaded data
// For demonstration, we'll use a static example
const parentComment = {
slug: commentSlug,
author: 'Author Name',
date: '10 Oct 2023',
content: 'This is the parent comment content.',
upvotes: 5,
replies: [
{
author: 'Reply Author 1',
date: '11 Oct 2023',
content: 'This is a reply to the parent comment.'
},
{
author: 'Reply Author 2',
date: '12 Oct 2023',
content: 'This is another reply to the parent comment.'
}
]
};
// Render the parent comment and its replies
threadContent.innerHTML = `
<div class="border-b border-gray-200 pb-3 mb-3">
<div class="mt-2 mb-1 text-sm flex flex-col md:flex-row items-start md:items-center md:space-x-2">
<div class="flex items-start space-x-2 mb-2 md:mb-0">
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<i class="fa-solid fa-arrow-up mr-1"></i> ${parentComment.upvotes}
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<i class="fas fa-reply mr-1"></i> REPLY
</span>
<span class="bg-gray-200 text-gray-700 text-xs font-action px-4 py-2 rounded-full flex items-center">
<i class="fas fa-comment mr-1"></i> ${parentComment.replies.length}
</span>
</div>
<small class="text-sm font-action">
<a href="#" class="text-sm hover:underline">${parentComment.author}</a>
<span class="text-text-muted text-sm opacity-75">${parentComment.date}</span>
</small>
</div>
<p class="text-sm mb-2">${parentComment.content}</p>
</div>
<div class="ml-4 mt-3 border-l-2 border-gray-200 pl-2">
${parentComment.replies.map(reply => `
<div class="mb-3">
<div class="mt-2 mb-1 text-sm flex flex-col md:flex-row items-start md:items-center md:space-x-2">
<small class="text-sm font-action">
<a href="#" class="text-sm hover:underline">${reply.author}</a>
<span class="text-text-muted text-sm opacity-75">${reply.date}</span>
</small>
</div>
<p class="text-sm mb-2">${reply.content}</p>
</div>
`).join('')}
</div>
`;
// Show the thread view and hide the comments list
commentsList.classList.add('hidden');
threadView.classList.remove('hidden');
}
});
</script>