From ac38fd11e74beb9bde7b0ef0d0067c18fea1203d Mon Sep 17 00:00:00 2001 From: Cinder Date: Tue, 11 Feb 2025 22:25:46 -0800 Subject: [PATCH] [Posts] Add an approximate result counter (#903) --- app/helpers/pagination_helper.rb | 23 +++++++++++++++++++ .../src/styles/views/posts/_posts.scss | 1 + .../views/posts/index/partials/_stats.scss | 13 +++++++++++ app/views/posts/index.html.erb | 1 + .../posts/partials/index/_stats.html.erb | 3 +++ 5 files changed, 41 insertions(+) create mode 100644 app/javascript/src/styles/views/posts/index/partials/_stats.scss create mode 100644 app/views/posts/partials/index/_stats.html.erb diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index d293d422a..919014c6c 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -1,6 +1,29 @@ # frozen_string_literal: true module PaginationHelper + def approximate_count(records) + return "" if records.pagination_mode != :numbered + + if records.total_pages > records.max_numbered_pages + pages = records.max_numbered_pages + schar = "over " + count = pages * records.records_per_page + title = "Over #{count} results found.\nActual result count may be much larger." + else + pages = records.total_pages + schar = "~" + count = pages * records.records_per_page + title = "Approximately #{count} results found.\nActual result count may differ." + end + + tag.span(class: "approximate-count", title: title, data: { count: count, pages: pages, per: records.max_numbered_pages }) do + concat schar + concat number_to_human(count, precision: 2, format: "%n%u", units: { thousand: "k" }) + concat " " + concat "result".pluralize(count) + end + end + def sequential_paginator(records) tag.nav(class: "pagination sequential", aria: { label: "Pagination" }) do return "" if records.try(:none?) diff --git a/app/javascript/src/styles/views/posts/_posts.scss b/app/javascript/src/styles/views/posts/_posts.scss index 833ab2586..0508e64b5 100644 --- a/app/javascript/src/styles/views/posts/_posts.scss +++ b/app/javascript/src/styles/views/posts/_posts.scss @@ -7,6 +7,7 @@ // Features @import "index/partials/mode_menu"; @import "index/partials/wiki_excerpt"; +@import "index/partials/stats"; @include window-larger-than(50rem) { @include with-setting("fullscreen", "true") { diff --git a/app/javascript/src/styles/views/posts/index/partials/_stats.scss b/app/javascript/src/styles/views/posts/index/partials/_stats.scss new file mode 100644 index 000000000..5f7b55196 --- /dev/null +++ b/app/javascript/src/styles/views/posts/index/partials/_stats.scss @@ -0,0 +1,13 @@ +.posts-index-stats { + display: flex; + justify-content: end; + + margin: -0.5rem 0rem 0.25rem; + + font-size: 0.75rem; + line-height: 0.75rem; + color: #fffa; + font-family: monospace; + + & > span { cursor: help; } +} diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 1d452ab0d..f85cc74a5 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -24,6 +24,7 @@
<%= render "ads/leaderboard", tag_string: @post_set.ad_tag_string %> <%= render "posts/partials/index/edit" %> + <%= render "posts/partials/index/stats", :post_set => @post_set %> <%= render "posts/partials/index/posts", :post_set => @post_set %>
diff --git a/app/views/posts/partials/index/_stats.html.erb b/app/views/posts/partials/index/_stats.html.erb new file mode 100644 index 000000000..d788206da --- /dev/null +++ b/app/views/posts/partials/index/_stats.html.erb @@ -0,0 +1,3 @@ +
+ <%= approximate_count(post_set.posts) %> +