<%# locals: (simplefin_item:) %> <%= tag.div id: dom_id(simplefin_item) do %>
<%= icon "chevron-right", class: "group-open:transform group-open:rotate-90" %>
<% if simplefin_item.logo.attached? %> <%= image_tag simplefin_item.logo, class: "rounded-full h-full w-full", loading: "lazy" %> <% else %>
<%= tag.p simplefin_item.name.first.upcase, class: "text-success text-xs font-medium" %>
<% end %>
<%= tag.p simplefin_item.name, class: "font-medium text-primary" %> <% if simplefin_item.scheduled_for_deletion? %>

<%= t(".deletion_in_progress") %>

<% end %>
<% if simplefin_item.accounts.any? %>

<%= simplefin_item.institution_summary %>

<%# Extra inline badges from latest sync stats %> <% stats = (@simplefin_sync_stats_map || {})[simplefin_item.id] || {} %> <% if stats.present? %>
<% if stats["unlinked_accounts"].to_i > 0 %> <%= render DS::Tooltip.new(text: "Accounts need setup", icon: "link-2", size: "sm") %> Unlinked: <%= stats["unlinked_accounts"].to_i %> <% end %> <% if stats["accounts_skipped"].to_i > 0 %> <%= render DS::Tooltip.new(text: "Some accounts were skipped due to errors during sync", icon: "alert-triangle", size: "sm", color: "warning") %> Skipped: <%= stats["accounts_skipped"].to_i %> <% end %> <% if stats["rate_limited"].present? || stats["rate_limited_at"].present? %> <% ts = stats["rate_limited_at"] %> <% ago = (ts.present? ? (begin; time_ago_in_words(Time.parse(ts)); rescue StandardError; nil; end) : nil) %> <%= render DS::Tooltip.new( text: (ago ? "Rate limited (" + ago + " ago)" : "Rate limited recently"), icon: "clock", size: "sm", color: "warning" ) %> <% end %> <% if stats["total_errors"].to_i > 0 || (stats["errors"].is_a?(Array) && stats["errors"].any?) %> <% tooltip_text = simplefin_error_tooltip(stats) %> <% if tooltip_text.present? %> <%= render DS::Tooltip.new(text: tooltip_text, icon: "alert-octagon", size: "sm", color: "warning") %> <% end %> <% end %> <% if stats["total_accounts"].to_i > 0 %> Total: <%= stats["total_accounts"].to_i %> <% end %>
<% end %> <% end %> <%# Determine if all reported errors are benign duplicate-skips (suppress scary banner). Computed in controller for testability. %> <% duplicate_only_errors = (@simplefin_duplicate_only_map || {})[simplefin_item.id] || false %> <% if simplefin_item.syncing? %>
<%= icon "loader", size: "sm", class: "animate-spin" %> <%= tag.span t(".syncing") %>
<% elsif simplefin_item.requires_update? %>
<%= icon "alert-triangle", size: "sm", color: "warning" %> <%= tag.span t(".requires_update") %>
<% elsif simplefin_item.rate_limited_message.present? %>
<%= icon "clock", size: "sm", color: "warning" %> <%= tag.span simplefin_item.rate_limited_message %>
<% elsif simplefin_item.sync_error.present? && !duplicate_only_errors %>
<%= render DS::Tooltip.new(text: simplefin_item.sync_error, icon: "alert-circle", size: "sm", color: "destructive") %> <%= tag.span t(".error"), class: "text-destructive" %>
<% elsif duplicate_only_errors %>
<%= icon "info", size: "sm" %> <%= tag.span "Some accounts were skipped as duplicates — use ‘Link existing accounts’ to merge.", class: "text-secondary" %>
<% else %>

<% if simplefin_item.last_synced_at %> <% if simplefin_item.sync_status_summary %> <%= t(".status_with_summary", timestamp: time_ago_in_words(simplefin_item.last_synced_at), summary: simplefin_item.sync_status_summary) %> <% else %> <%= t(".status", timestamp: time_ago_in_words(simplefin_item.last_synced_at)) %> <% end %> <% else %> <%= t(".status_never") %> <% end %>

<% end %>
<% if simplefin_item.requires_update? %> <%= render DS::Link.new( text: t(".update"), icon: "refresh-cw", variant: "secondary", href: edit_simplefin_item_path(simplefin_item), frame: "modal" ) %> <% elsif Rails.env.development? %> <%= icon( "refresh-cw", as_button: true, href: sync_simplefin_item_path(simplefin_item) ) %> <% end %> <%= render DS::Menu.new do |menu| %> <% menu.with_item( variant: "button", text: t(".delete"), icon: "trash-2", href: simplefin_item_path(simplefin_item), method: :delete, confirm: CustomConfirm.for_resource_deletion(simplefin_item.name, high_severity: true) ) %> <% end %>
<% unless simplefin_item.scheduled_for_deletion? %>
<% if simplefin_item.accounts.any? %> <%= render "accounts/index/account_groups", accounts: simplefin_item.accounts %> <% end %> <%# Sync summary (collapsible) %> <% stats = (@simplefin_sync_stats_map || {})[simplefin_item.id] || {} %> <% if stats.present? %>
<%= icon "chevron-right", class: "group-open:transform group-open:rotate-90" %> Sync summary
<% if simplefin_item.last_synced_at %> Last sync: <%= time_ago_in_words(simplefin_item.last_synced_at) %> ago <% end %>

Accounts

Total: <%= stats["total_accounts"].to_i %> Linked: <%= stats["linked_accounts"].to_i %> Unlinked: <%= stats["unlinked_accounts"].to_i %> <% institutions = simplefin_item.connected_institutions %> Institutions: <%= institutions.size %>

Transactions

Seen: <%= stats["tx_seen"].to_i %> Imported: <%= stats["tx_imported"].to_i %> Updated: <%= stats["tx_updated"].to_i %> Skipped: <%= stats["tx_skipped"].to_i %>

Holdings

Processed: <%= stats["holdings_processed"].to_i %>

Health

<% if stats["rate_limited"].present? || stats["rate_limited_at"].present? %> <% ts = stats["rate_limited_at"] %> <% ago = (ts.present? ? (begin; time_ago_in_words(Time.parse(ts)); rescue StandardError; nil; end) : nil) %> Rate limited <%= ago ? "(#{ago} ago)" : "recently" %> <% end %> <% total_errors = stats["total_errors"].to_i %> <% if total_errors > 0 %> Errors: <%= total_errors %> <% else %> Errors: 0 <% end %>
<% end %> <%# Compute unlinked SimpleFin accounts (no legacy account and no AccountProvider link) # Prefer controller-provided map; fallback to a local query so the card stays accurate after Turbo broadcasts %> <% unlinked_count = if defined?(@simplefin_unlinked_count_map) && @simplefin_unlinked_count_map @simplefin_unlinked_count_map[simplefin_item.id] || 0 else begin simplefin_item.simplefin_accounts .left_joins(:account, :account_provider) .where(accounts: { id: nil }, account_providers: { id: nil }) .count rescue => e Rails.logger.warn("SimpleFin card: unlinked_count fallback failed: #{e.class} - #{e.message}") 0 end end %> <% if unlinked_count.to_i > 0 %>

<%= t(".setup_needed") %>

<%= t(".setup_description") %>

<%= render DS::Link.new( text: t(".setup_action"), icon: "settings", variant: "primary", href: setup_accounts_simplefin_item_path(simplefin_item), frame: :modal ) %>
<% elsif simplefin_item.accounts.empty? %>

<%= t(".no_accounts_title") %>

<%= t(".no_accounts_description") %>

<% end %>
<% end %>
<% end %>