The personal finance app for everyone (by everyone)
Ruby
Update the insights lightbulb badge live on dismiss and refresh (#3880)
Guillem Arias Fauste98e825d0378c
diff --git a/app/jobs/generate_insights_job.rb b/app/jobs/generate_insights_job.rbindex 82e8f6a5f1..31c7dd20b9 100644--- a/app/jobs/generate_insights_job.rb+++ b/app/jobs/generate_insights_job.rb@@ -69,6 +69,14 @@ class GenerateInsightsJob < ApplicationJobpartial: "insights/refresh_button",locals: { pending: false })+ # The lightbulb lives in the layout, so it goes on the family stream every+ # page subscribes to, not just the /insights one.+ Turbo::StreamsChannel.broadcast_replace_to(+ family,+ targets: "[data-insights-badge]",+ partial: "layouts/shared/insights_badge",+ locals: { count: unread_ids.size }+ )end# A visible insight whose generator ran successfully but did not re-emitdiff --git a/app/views/insights/acknowledge.turbo_stream.erb b/app/views/insights/acknowledge.turbo_stream.erbindex 2de2142f62..1355fa8a43 100644--- a/app/views/insights/acknowledge.turbo_stream.erb+++ b/app/views/insights/acknowledge.turbo_stream.erb@@ -32,3 +32,6 @@element is emitted by the shared dashboard loop and carries no id. %><%= turbo_stream.remove_all "[data-section-key='insights_feed']" %><% end %>++<%# Lightbulb badge in both layout copies (desktop header, mobile top nav). %>+<%= turbo_stream.replace_all "[data-insights-badge]", partial: "layouts/shared/insights_badge", locals: { count: @unread_ids.size } %>diff --git a/app/views/insights/unacknowledge.turbo_stream.erb b/app/views/insights/unacknowledge.turbo_stream.erbindex d3d4914e94..10f2d5e9ce 100644--- a/app/views/insights/unacknowledge.turbo_stream.erb+++ b/app/views/insights/unacknowledge.turbo_stream.erb@@ -10,3 +10,6 @@<%= turbo_stream.replace "insights-feed" do %><%= render "pages/dashboard/insights_feed", insights: @feed_insights %><% end %>++<%# Lightbulb badge in both layout copies (desktop header, mobile top nav). %>+<%= turbo_stream.replace_all "[data-insights-badge]", partial: "layouts/shared/insights_badge", locals: { count: @unread_ids.size } %>diff --git a/app/views/layouts/shared/_insights_badge.html.erb b/app/views/layouts/shared/_insights_badge.html.erbnew file mode 100644index 0000000000..2d8d375b3a--- /dev/null+++ b/app/views/layouts/shared/_insights_badge.html.erb@@ -0,0 +1,8 @@+<%# locals: (count:) %>+<%# The wrapper stays at 0 so acknowledge/unacknowledge and GenerateInsightsJob+ streams always have a target; `contents` keeps it out of layout so the chip+ still positions against the lightbulb link. Targeted by attribute because+ the layout renders the lightbulb twice (desktop header, mobile top nav).+ Unread count stays a hand-rolled inverse chip — DS::Pill's filled neutral+ tone is not theme-swapped and disappears on dark nav surfaces. %>+<span data-insights-badge class="contents"><% if count.positive? %><span class="absolute top-1 right-1 min-w-4 h-4 px-1 rounded-full bg-inverse text-inverse text-[10px] font-medium flex items-center justify-center pointer-events-none"><%= count > 9 ? "9+" : count %></span><% end %></span>diff --git a/app/views/layouts/shared/_insights_button.html.erb b/app/views/layouts/shared/_insights_button.html.erbindex e7402c0bd7..8ff37bbeff 100644--- a/app/views/layouts/shared/_insights_button.html.erb+++ b/app/views/layouts/shared/_insights_button.html.erb@@ -3,7 +3,6 @@users who haven't opted in. No preview dot on the icon: theunread badge already occupies that corner. %><% if Current.family && preview_features_enabled? %>- <% unread_insights_count = Current.family.insights.active.count %><%# Prefetch disabled: /insights marks insights read on real visitsand deliberately skips prefetch requests, so a prefetchedresponse served on click would never clear the badge. %>@@ -13,10 +12,7 @@data: { turbo_prefetch: false } do %><%# `as: :span` + `html_class:` so the tooltip trigger fills thisalready-focusable link (full hover target). DS::Tooltip- binds keyboard focus on the closest <a>/<summary> ancestor.- Unread count stays a hand-rolled inverse chip — DS::Pill's- filled neutral tone is not theme-swapped and disappears on- dark nav surfaces. %>+ binds keyboard focus on the closest <a>/<summary> ancestor. %><%= render DS::Tooltip.new(text: t("layouts.application.insights"),icon: "lightbulb",@@ -24,8 +20,6 @@as: :span,html_class: "size-full items-center justify-center") %>- <% if unread_insights_count.positive? %>- <span class="absolute top-1 right-1 min-w-4 h-4 px-1 rounded-full bg-inverse text-inverse text-[10px] font-medium flex items-center justify-center pointer-events-none"><%= unread_insights_count > 9 ? "9+" : unread_insights_count %></span>- <% end %>+ <%= render "layouts/shared/insights_badge", count: Current.family.insights.active.count %><% end %><% end %>diff --git a/test/controllers/insights_controller_test.rb b/test/controllers/insights_controller_test.rbindex 40bc6c82b7..fd9c571f28 100644--- a/test/controllers/insights_controller_test.rb+++ b/test/controllers/insights_controller_test.rb@@ -108,6 +108,66 @@ class InsightsControllerTest < ActionDispatch::IntegrationTestassert_select "turbo-stream[action=replace][target=?]", "insights-feed", count: 0end+ # The layout renders the lightbulb twice (desktop header, mobile top nav), so+ # the badge is replaced by attribute, not id, and its wrapper stays even at 0+ # so a later stream still has something to target.+ test "layout renders a badge target in both lightbulb copies" do+ get root_url++ assert_response :success+ assert_select "[data-insights-badge]", count: 2+ end++ test "acknowledge refreshes the unread badge in both lightbulb copies" do+ unread = @user.family.insights.active.count+ assert_operator unread, :>, 1, "fixtures need more than one unread insight"++ patch acknowledge_insight_url(@insight), as: :turbo_stream++ assert_response :success+ assert_select "turbo-stream[action=replace][targets=?]", "[data-insights-badge]" do+ assert_select "template [data-insights-badge]", text: (unread - 1).to_s+ end+ end++ test "acknowledging the last unread insight empties the badge but keeps its target" do+ @user.family.insights.active.where.not(id: @insight.id).update_all(status: "read")++ patch acknowledge_insight_url(@insight), as: :turbo_stream++ assert_response :success+ assert_select "turbo-stream[action=replace][targets=?]", "[data-insights-badge]" do+ assert_select "template [data-insights-badge]", text: ""+ end+ end++ test "the badge stream still caps the count at 9+" do+ 11.times do |i|+ @user.family.insights.create!(insight_type: "idle_cash", priority: "low", status: "active",+ title: "Extra #{i}", body: "body", dedup_key: "idle_cash:cap:#{i}", generated_at: Time.current)+ end++ patch acknowledge_insight_url(@insight), as: :turbo_stream++ assert_select "turbo-stream[action=replace][targets=?]", "[data-insights-badge]" do+ assert_select "template [data-insights-badge]", text: "9+"+ end+ end++ test "unacknowledge refreshes the unread badge" do+ @insight.acknowledge!++ unread = @user.family.insights.active.count++ patch unacknowledge_insight_url(@insight), as: :turbo_stream++ # Undo restores the insight as read, so the unread count holds.+ assert_response :success+ assert_select "turbo-stream[action=replace][targets=?]", "[data-insights-badge]" do+ assert_select "template [data-insights-badge]", text: unread.to_s+ end+ end+test "unacknowledge restores the insight as read and re-renders the list" do@insight.acknowledge!diff --git a/test/jobs/generate_insights_job_test.rb b/test/jobs/generate_insights_job_test.rbindex acbfca20fd..328070e14a 100644--- a/test/jobs/generate_insights_job_test.rb+++ b/test/jobs/generate_insights_job_test.rb@@ -89,6 +89,21 @@ class GenerateInsightsJobTest < ActiveJob::TestCaseassert_equal 5000.0, insight.metadata["balance"]end+ # The badge rides the family stream every layout subscribes to, so new unread+ # insights bump the lightbulb count on whatever page is open.+ test "broadcasts the unread badge alongside the refreshed list" do+ stub_generated([ generated_insight ])+ Turbo::StreamsChannel.stubs(:broadcast_replace_to)+ Turbo::StreamsChannel.expects(:broadcast_replace_to).with(+ @family,+ targets: "[data-insights-badge]",+ partial: "layouts/shared/insights_badge",+ locals: { count: @family.insights.active.count + 1 }+ )++ GenerateInsightsJob.perform_now(family_id: @family.id)+ end+test "enqueues notifications for newly created high priority insights" doRails.application.config.stubs(:app_mode).returns("managed".inquiry)opted_in_user, opted_out_user = @family.users.to_a