Show each tag's total unread count and remember collapsed sections

Closes gitea#24: the sidebar's per-feed unread counts were already
computed by sidebar() (apps.core.context_processors); each tag group's
total is now the sum of its own feeds' counts (a feed can carry more
than one tag, so this reuses the same per-feed data rather than a
second query) and renders next to the tag name like a feed's own
count.

Closes gitea#25: each sidebar <details> section (one per tag, plus
"Untagged") now carries a stable data-sidebar-key, and
static/js/sidebar-state.js remembers which ones are collapsed in
localStorage — this isn't a SPA, so every click re-renders the sidebar
server-side, and previously every section reset back to expanded on
the next page load. Per-browser, same pattern as theme.js/theme-init.js,
not synced across devices/accounts.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-31 12:34:57 -06:00
1 parent 9439f9cc59
commit 816cc2c5a7
7 files changed
+184 -4

No files matched your search

+13 -1
View File
@@ -44,7 +44,19 @@ def sidebar(request):
return { return {
"sidebar_tag_groups": [ "sidebar_tag_groups": [
{"tag": tag, "subscriptions": feeds_by_tag.get(tag.pk, [])} for tag in tags {
"tag": tag,
"subscriptions": feeds_by_tag.get(tag.pk, []),
# A feed can carry more than one tag, so this is a sum
# over whichever of the user's feeds this tag happens to
# cover right now — not a separately-tracked counter — and
# reuses the per-feed counts already computed above rather
# than a second query.
"unread_count": sum(
subscription.unread_count for subscription in feeds_by_tag.get(tag.pk, [])
),
}
for tag in tags
], ],
"sidebar_untagged": untagged, "sidebar_untagged": untagged,
"sidebar_starred_count": ArticleTag.objects.filter( "sidebar_starred_count": ArticleTag.objects.filter(
+82
View File
@@ -0,0 +1,82 @@
import pytest
from django.contrib.auth import get_user_model
from django.test import RequestFactory
from django.urls import reverse
from apps.articles.models import Article, SubscriptionTag, Tag
from apps.feeds.models import Feed, Subscription
from .context_processors import sidebar
User = get_user_model()
@pytest.fixture
def alice(db):
return User.objects.create_user("alice", password="pw-alice-123")
def _sidebar_context(user):
request = RequestFactory().get("/")
request.user = user
return sidebar(request)
@pytest.mark.django_db
def test_tag_unread_count_sums_its_feeds(alice):
feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml")
sub_a = Subscription.objects.create(user=alice, feed=feed_a)
sub_b = Subscription.objects.create(user=alice, feed=feed_b)
tag = Tag.objects.create(user=alice, name="work")
SubscriptionTag.objects.create(user=alice, subscription=sub_a, tag=tag)
SubscriptionTag.objects.create(user=alice, subscription=sub_b, tag=tag)
for i in range(3):
Article.objects.create(feed=feed_a, guid=f"a-{i}", title=f"A{i}")
for i in range(4):
Article.objects.create(feed=feed_b, guid=f"b-{i}", title=f"B{i}")
context = _sidebar_context(alice)
[group] = context["sidebar_tag_groups"]
assert group["tag"] == tag
assert group["unread_count"] == 7
@pytest.mark.django_db
def test_tag_unread_count_is_zero_with_no_unread_articles(alice):
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
sub = Subscription.objects.create(user=alice, feed=feed)
tag = Tag.objects.create(user=alice, name="empty")
SubscriptionTag.objects.create(user=alice, subscription=sub, tag=tag)
context = _sidebar_context(alice)
[group] = context["sidebar_tag_groups"]
assert group["unread_count"] == 0
@pytest.mark.django_db
def test_sidebar_sections_carry_a_stable_collapse_state_key(client, alice):
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
sub = Subscription.objects.create(user=alice, feed=feed)
tag = Tag.objects.create(user=alice, name="work")
SubscriptionTag.objects.create(user=alice, subscription=sub, tag=tag)
other_feed = Feed.objects.create(feed_url="https://other.example.com/feed.xml")
Subscription.objects.create(user=alice, feed=other_feed) # untagged
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
content = response.content.decode()
# static/js/sidebar-state.js reads these to restore/persist each
# section's collapsed state across page loads.
assert f'data-sidebar-key="tag-{tag.pk}"' in content
assert 'data-sidebar-key="untagged"' in content
@pytest.mark.django_db
def test_base_template_loads_the_sidebar_state_script(client, alice):
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
assert "sidebar-state.js" in response.content.decode()
+24
View File
@@ -164,3 +164,27 @@ def test_saved_filter_only_shows_saved_articles(client, alice, alices_article):
response = client.get(reverse("reader:stream"), {"filter": "starred"}) response = client.get(reverse("reader:stream"), {"filter": "starred"})
assert alices_article.title not in response.content.decode() assert alices_article.title not in response.content.decode()
@pytest.mark.django_db
def test_sidebar_shows_a_tags_total_unread_count(client, alice):
from apps.articles.models import SubscriptionTag, Tag
feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml")
sub_a = Subscription.objects.create(user=alice, feed=feed_a)
sub_b = Subscription.objects.create(user=alice, feed=feed_b)
tag = Tag.objects.create(user=alice, name="work")
SubscriptionTag.objects.create(user=alice, subscription=sub_a, tag=tag)
SubscriptionTag.objects.create(user=alice, subscription=sub_b, tag=tag)
for i in range(3):
Article.objects.create(feed=feed_a, guid=f"a-{i}", title=f"A{i}")
for i in range(4):
Article.objects.create(feed=feed_b, guid=f"b-{i}", title=f"B{i}")
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
# The only "7" on the page — the individual feeds show 3 and 4.
content = response.content.decode()
assert '<span class="unread-count">7</span>' in content
+10
View File
@@ -299,6 +299,16 @@ form p {
letter-spacing: 0.04em; letter-spacing: 0.04em;
} }
/* An inner wrapper, not `summary` itself — `summary`'s native disclosure
triangle only renders with its default `display: list-item`, so the
flex layout for the tag name + unread count lives one level in. */
.sidebar-tag-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.4rem;
}
.sidebar-tag-link { .sidebar-tag-link {
display: inline-block; display: inline-block;
padding: 0.15rem 0.4rem; padding: 0.15rem 0.4rem;
+46
View File
@@ -0,0 +1,46 @@
(function () {
// Remembers which sidebar <details> sections (one per tag, plus
// "Untagged" — see templates/core/_sidebar.html's data-sidebar-key)
// the user has collapsed, so a full page navigation (this isn't an
// SPA — every click re-renders the sidebar server-side) doesn't keep
// snapping them back open. Per-browser via localStorage, same pattern
// as theme.js/theme-init.js — not synced across devices/accounts.
var STORAGE_KEY = "evection-sidebar-collapsed";
function readCollapsed() {
try {
var raw = localStorage.getItem(STORAGE_KEY);
return raw ? JSON.parse(raw) : [];
} catch (e) {
return [];
}
}
function writeCollapsed(keys) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(keys));
} catch (e) {
/* localStorage unavailable (private mode, etc.) — state just won't persist */
}
}
var collapsed = readCollapsed();
document.querySelectorAll(".sidebar-section[data-sidebar-key]").forEach(function (details) {
var key = details.dataset.sidebarKey;
if (collapsed.indexOf(key) !== -1) {
details.open = false;
}
details.addEventListener("toggle", function () {
var current = readCollapsed();
var index = current.indexOf(key);
if (details.open) {
if (index !== -1) current.splice(index, 1);
} else if (index === -1) {
current.push(key);
}
writeCollapsed(current);
});
});
})();
+4 -1
View File
@@ -53,7 +53,10 @@
{% endif %} {% endif %}
<script src="{% static 'js/stream.js' %}" defer></script> <script src="{% static 'js/stream.js' %}" defer></script>
{% if user.is_authenticated %}<script src="{% static 'js/theme.js' %}" defer></script>{% endif %} {% if user.is_authenticated %}
<script src="{% static 'js/theme.js' %}" defer></script>
<script src="{% static 'js/sidebar-state.js' %}" defer></script>
{% endif %}
<script src="{% static 'js/register-sw.js' %}" defer></script> <script src="{% static 'js/register-sw.js' %}" defer></script>
</body> </body>
</html> </html>
+5 -2
View File
@@ -31,10 +31,13 @@
</ul> </ul>
{% for entry in sidebar_tag_groups %} {% for entry in sidebar_tag_groups %}
<details class="sidebar-section" open> <details class="sidebar-section" data-sidebar-key="tag-{{ entry.tag.pk }}" open>
<summary> <summary>
<span class="sidebar-tag-row">
<a href="{% url 'reader:stream' %}?tag={{ entry.tag.pk }}" <a href="{% url 'reader:stream' %}?tag={{ entry.tag.pk }}"
class="sidebar-tag-link{% if request.GET.tag == entry.tag.pk|stringformat:'s' %} active{% endif %}">{{ entry.tag.name }}</a> class="sidebar-tag-link{% if request.GET.tag == entry.tag.pk|stringformat:'s' %} active{% endif %}">{{ entry.tag.name }}</a>
{% if entry.unread_count %}<span class="unread-count">{{ entry.unread_count }}</span>{% endif %}
</span>
</summary> </summary>
{% if entry.subscriptions %} {% if entry.subscriptions %}
<ul class="sidebar-item-list"> <ul class="sidebar-item-list">
@@ -54,7 +57,7 @@
{% endfor %} {% endfor %}
{% if sidebar_untagged %} {% if sidebar_untagged %}
<details class="sidebar-section" open> <details class="sidebar-section" data-sidebar-key="untagged" open>
<summary>Untagged</summary> <summary>Untagged</summary>
<ul class="sidebar-item-list"> <ul class="sidebar-item-list">
{% for subscription in sidebar_untagged %} {% for subscription in sidebar_untagged %}