From f9aad52dc52c76f492ae7a2eff64557ca1fb185f Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Thu, 24 Apr 2025 20:31:43 -0400 Subject: [PATCH] Implement client sorting and pinning --- web/main.go | 1 + web/static/index.js | 21 ++++++++++++++++++++- web/views/board.templ | 20 +++++++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/web/main.go b/web/main.go index 3f4eba9..f8fdc8d 100644 --- a/web/main.go +++ b/web/main.go @@ -430,6 +430,7 @@ func main() { IpCount: len(uniqueIpHashes), Pinned: thread.Pinned, Locked: thread.Locked, + BumpedAt: thread.BumpedAt, }) } diff --git a/web/static/index.js b/web/static/index.js index d9f2584..7b8900b 100644 --- a/web/static/index.js +++ b/web/static/index.js @@ -71,7 +71,7 @@ function highlightPost(postId, e, status = true) { } function resizeCatalogPreviewImgs() { - const value = $("selectSortBy").value; + const value = $("selectResize").value; const postsImages = Array.from(document.querySelectorAll(".catalog-preview img")); for (const postImage of postsImages) { switch (value) { @@ -105,6 +105,25 @@ function applyCatalogSearch() { } } +function applyCatalogSort(desc = true) { + const sortBy = $("selectSortBy").value; + + const catalog = document.getElementById('catalog'); + const previews = Array.from(catalog.children); + + previews.sort((a, b) => { + const pinDiff = (+b.dataset.pinned) - (+a.dataset.pinned); + if (pinDiff !== 0) return pinDiff; + + const bumpDiff = (+a.dataset[sortBy]) - (+b.dataset[sortBy]); + return desc ? -bumpDiff : bumpDiff; + }); + + const frag = document.createDocumentFragment(); + previews.forEach(el => frag.appendChild(el)); + catalog.appendChild(frag); +} + function currentBoardSlug() { return window.location.pathname.split("/")[1] || ""; } diff --git a/web/views/board.templ b/web/views/board.templ index 2adbd82..852d223 100644 --- a/web/views/board.templ +++ b/web/views/board.templ @@ -6,6 +6,7 @@ import ( "github.com/dominicf2001/comfychan/internal/util" "github.com/dominicf2001/comfychan/web/views/shared" "strconv" + "time" ) templ BoardHeader(board database.Board) { @@ -32,10 +33,15 @@ templ Board(board database.Board, isAdmin bool) {
Image size: - + Sort by: + [Refresh]
@@ -45,7 +51,8 @@ templ Board(board database.Board, isAdmin bool) { hx-trigger="load, refreshPosts from:body" _="on htmx:afterRequest call resizeCatalogPreviewImgs() - then call applyCatalogSearch()" + then call applyCatalogSearch() + then call applyCatalogSort()" > } } @@ -60,6 +67,7 @@ type CatalogThreadPreview struct { IpCount int Pinned bool Locked bool + BumpedAt time.Time } type CatalogContext struct { @@ -73,7 +81,13 @@ templ ThreadsCatalog(previews []CatalogThreadPreview, catalogContext CatalogCont {{ replyCount := strconv.FormatInt(int64(preview.ReplyCount), 10) }} {{ ipCount := strconv.FormatInt(int64(preview.IpCount), 10) }} {{ elThreadId := fmt.Sprintf("thread-%d", preview.ThreadId) }} -
+ {{ + pinnedInt := 0 + if preview.Pinned { + pinnedInt = 1 + } + }} +