feat: Replace sort buttons with dropdown, add comment sort option

This commit is contained in:
Kerby Martino (aider) committed 2025-05-04 15:00:19 +08:00
1 parent 5423ded939
commit 5233797bb4
1 file changed
+23 -10
+23 -10
View File
@@ -23,11 +23,15 @@
</button> </button>
</div> </div>
<!-- Sort By --> <!-- Sort By Dropdown -->
<div class="sort-controls flex items-center space-x-2"> <div class="sort-controls flex items-center space-x-2">
<span class="text-sm font-medium text-gray-700 mr-2">Sort by:</span> <label for="sort-select" class="text-sm font-medium text-gray-700">Sort by:</label>
<button data-sort-by="new" class="sort-btn active px-3 py-1 text-sm font-action bg-primary text-white rounded-md hover:bg-primary-dark transition-colors duration-150 ease-in-out">New</button> <select id="sort-select" name="sort_by" class="sort-select text-sm font-action bg-white border border-gray-300 rounded-md px-3 py-1.5 focus:outline-none focus:ring-1 focus:ring-primary focus:border-primary cursor-pointer">
<button data-sort-by="top" class="sort-btn px-3 py-1 text-sm font-action bg-primary text-white rounded-md hover:bg-primary-dark transition-colors duration-150 ease-in-out">Top</button> <option value="new" selected>Newest</option>
<option value="upvotes">Most Upvotes</option>
<option value="comments">Most Comments</option>
<!-- Add other options like 'today', 'nearby' here if data/logic becomes available -->
</select>
</div> </div>
</div> </div>
</div> </div>
@@ -36,7 +40,7 @@
<div id="post-list-container" class="post-list space-y-4"> <div id="post-list-container" class="post-list space-y-4">
{% assign sorted_posts = site.posts | sort: 'date' | reverse %} {% assign sorted_posts = site.posts | sort: 'date' | reverse %}
{% for post in sorted_posts %} {% for post in sorted_posts %}
<div class="post-item mb-4" data-type="{{ post.type | default: 'unknown' }}" data-date="{{ post.date | date_to_xmlschema }}" data-upvotes="{{ post.upvotes | default: 0 }}"> <div class="post-item mb-4" data-type="{{ post.type | default: 'unknown' }}" data-date="{{ post.date | date_to_xmlschema }}" data-upvotes="{{ post.upvotes | default: 0 }}" data-comments="{{ post.comments_count | default: 0 }}">
<article class="flex flex-col md:flex-row items-start"> <article class="flex flex-col md:flex-row items-start">
<div class="flex-1 text-left"> <div class="flex-1 text-left">
<div> <div>
@@ -93,12 +97,12 @@
<script> <script>
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const filterButtons = document.querySelectorAll('.filter-btn'); const filterButtons = document.querySelectorAll('.filter-btn');
const sortButtons = document.querySelectorAll('.sort-btn'); const sortSelect = document.getElementById('sort-select'); // Get the select dropdown
const postListContainer = document.getElementById('post-list-container'); const postListContainer = document.getElementById('post-list-container');
const postItems = Array.from(postListContainer.querySelectorAll('.post-item')); // Convert NodeList to Array const postItems = Array.from(postListContainer.querySelectorAll('.post-item')); // Convert NodeList to Array
let currentFilter = 'all'; let currentFilter = 'all';
let currentSort = 'new'; // Default sort let currentSort = sortSelect.value; // Default sort from selected option ('new')
function applyFiltersAndSorting() { function applyFiltersAndSorting() {
// 1. Filter // 1. Filter
@@ -114,15 +118,24 @@ document.addEventListener('DOMContentLoaded', () => {
if (currentSort === 'new') { if (currentSort === 'new') {
// Sort by date descending (newest first) // Sort by date descending (newest first)
return new Date(b.dataset.date) - new Date(a.dataset.date); return new Date(b.dataset.date) - new Date(a.dataset.date);
} else if (currentSort === 'top') { } else if (currentSort === 'upvotes') { // Changed from 'top'
// Sort by upvotes descending (highest first) // Sort by upvotes descending (highest first)
// Add secondary sort by date for ties
const scoreDiff = parseInt(b.dataset.upvotes) - parseInt(a.dataset.upvotes); const scoreDiff = parseInt(b.dataset.upvotes) - parseInt(a.dataset.upvotes);
if (scoreDiff !== 0) { if (scoreDiff !== 0) {
return scoreDiff; return scoreDiff;
} }
return new Date(b.dataset.date) - new Date(a.dataset.date); // Newest first if votes are equal // Secondary sort by date for ties
return new Date(b.dataset.date) - new Date(a.dataset.date);
} else if (currentSort === 'comments') {
// Sort by comments descending (most first)
const commentDiff = parseInt(b.dataset.comments) - parseInt(a.dataset.comments);
if (commentDiff !== 0) {
return commentDiff;
}
// Secondary sort by date for ties
return new Date(b.dataset.date) - new Date(a.dataset.date);
} }
// Add logic for other sort options like 'today', 'nearby' here if implemented
return 0; // Default no sort change return 0; // Default no sort change
}); });