1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-08 23:15:24 +02:00

Fix turbo stream broadcasts

This commit is contained in:
Zach Gollwitzer 2025-06-10 18:09:13 -04:00
parent 4a81c41ebf
commit 77e425f0e0
5 changed files with 41 additions and 18 deletions

View file

@ -16,13 +16,13 @@ class Account::SyncCompleteEvent
locals: { account: account }
)
# Replace the groups this account belongs to in the sidebar
account_group_ids.each do |id|
# Replace the groups this account belongs to in both desktop and mobile sidebars
sidebar_targets.each do |(tab, mobile_flag)|
account.broadcast_replace_to(
account.family,
target: id,
target: account_group.dom_id(tab: tab, mobile: mobile_flag),
partial: "accounts/accountable_group",
locals: { account_group: account_group, open: true }
locals: { account_group: account_group, open: true, all_tab: tab == :all, mobile: mobile_flag }
)
end
@ -37,18 +37,18 @@ class Account::SyncCompleteEvent
end
private
# The sidebar will show the account in both its classification tab and the "all" tab,
# so we need to broadcast to both.
def account_group_ids
unless account_group.present?
error = Error.new("Account #{account.id} is not part of an account group")
Rails.logger.warn(error.message)
Sentry.capture_exception(error, level: :warning)
return []
end
# Returns an array of [tab, mobile?] tuples that should receive an update.
# We broadcast to both the classification-specific tab and the "all" tab,
# for desktop (mobile: false) and mobile (mobile: true) variants.
def sidebar_targets
return [] unless account_group.present?
id = account_group.id
[ id, "#{account_group.classification}_#{id}" ]
[
[ account_group.classification.to_sym, false ],
[ :all, false ],
[ account_group.classification.to_sym, true ],
[ :all, true ]
]
end
def account_group

View file

@ -13,6 +13,22 @@ class BalanceSheet::AccountGroup
@classification_group = classification_group
end
# A stable DOM id for this group.
# Example outputs:
# dom_id(tab: :asset) # => "asset_depository"
# dom_id(tab: :all, mobile: true) # => "mobile_all_depository"
#
# Keeping all of the logic here means the view layer and broadcaster only
# need to ask the object for its DOM id instead of rebuilding string
# fragments in multiple places.
def dom_id(tab: nil, mobile: false)
parts = []
parts << "mobile" if mobile
parts << (tab ? tab.to_s : classification.to_s)
parts << key
parts.compact.join("_")
end
def key
accountable_type.to_s.underscore
end
@ -31,6 +47,11 @@ class BalanceSheet::AccountGroup
accounts.any?(&:syncing?)
end
# "asset" or "liability"
def classification
classification_group.classification
end
def currency
classification_group.currency
end

View file

@ -65,6 +65,8 @@ class Sync < ApplicationRecord
start!
sleep 10
begin
syncable.perform_sync(self)
rescue => e

View file

@ -82,7 +82,7 @@
<div>
<% family.balance_sheet.account_groups.each do |group| %>
<%= render "accounts/accountable_group", account_group: group, mobile: mobile %>
<%= render "accounts/accountable_group", account_group: group, mobile: mobile, all_tab: true %>
<% end %>
</div>
</div>

View file

@ -1,6 +1,6 @@
<%# locals: (account_group:, mobile: false, open: nil, **args) %>
<%# locals: (account_group:, mobile: false, all_tab: false, open: nil, **args) %>
<div id="<%= mobile ? "mobile_#{account_group.key}" : account_group.key %>">
<div id="<%= account_group.dom_id(tab: all_tab ? :all : nil, mobile: mobile) %>">
<% is_open = open.nil? ? account_group.accounts.any? { |account| page_active?(account_path(account)) } : open %>
<%= render DisclosureComponent.new(title: account_group.name, align: :left, open: is_open) do |disclosure| %>
<% disclosure.with_summary_content do %>