The personal finance app for everyone (by everyone)
Ruby
<%# locals: (cashflow_data:, accounts:, accessible_account_ids:, view_params:) %>
<%
pill_button_class = "inline-flex items-center gap-1.5 bg-container border border-secondary font-medium rounded-lg pl-3 pr-2 py-2 text-sm cursor-pointer text-primary hover:bg-container-inset-hover focus:outline-hidden focus:ring-0 whitespace-nowrap"
selected_account_ids = cashflow_data[:account_ids] || accounts.map { |a| a.id.to_s }
all_accounts_selected = selected_account_ids.size == accounts.size
# TransactionsController falls back to the user's full accessible-accounts
# set (broader than this widget's eligible accounts) when account_ids is
# absent, so the param can only be dropped when that default matches this
# widget's selection exactly.
drilldown_account_ids = selected_account_ids.sort == accessible_account_ids.sort ? nil : selected_account_ids
period = cashflow_data[:period]
# The ring amounts exclude pending entries (IncomeStatement scopes to
# excluding_pending), so both drill-down paths filter to confirmed too.
drilldown_statuses = [ "confirmed" ]
# The other widgets' months and filters, carried through this widget's
# links and form so changing it doesn't reset them.
other_view_params = view_params.except(:cashflow_month, :cashflow_account_ids)
groups = [
{ key: "inflows", label: t(".inflows"), segments: cashflow_data[:inflows], total: cashflow_data[:total_income] },
{ key: "outflows", label: t(".outflows"), segments: cashflow_data[:outflows], total: cashflow_data[:total_expense] }
]
all_segments = cashflow_data[:inflows] + cashflow_data[:outflows]
%>
<div id="cashflow-donut-section" class="@container px-4 space-y-4">
<div class="flex flex-col lg:flex-row lg:items-start lg:justify-between gap-3">
<p class="text-sm text-secondary">
<%= t(".date_range", start_date: l(period.date_range.begin, format: :long), end_date: l(period.date_range.end, format: :long)) %>
</p>
<div class="flex items-center gap-2">
<%= render DS::Tooltip.new(text: t(".period_scope_hint"), placement: "top-start") %>
<%= render DS::Menu.new(variant: :button, placement: "bottom-end", max_height: "18rem") do |menu| %>
<% menu.with_button(type: "button", class: pill_button_class, aria: { label: t(".month_picker_aria_label") }) do %>
<span class="capitalize"><%= I18n.l(cashflow_data[:month], format: :month_year) %></span>
<%= icon("chevron-down", size: "sm") %>
<% end %>
<% (0..11).each do |i| %>
<% month = Date.current.beginning_of_month - i.months %>
<% menu.with_item(
variant: :link,
text: I18n.l(month, format: :month_year).capitalize,
href: root_path(other_view_params.merge(cashflow_month: month.iso8601, cashflow_account_ids: cashflow_data[:account_ids]).compact),
frame: "dashboard_sections",
selected: month == cashflow_data[:month],
data: { turbo_action: "advance" }
) %>
<% end %>
<% end %>
<%= render layout: "pages/dashboard/filter_popover", locals: {
button_text: all_accounts_selected ? t(".filter_accounts_all") : t(".filter_accounts", count: selected_account_ids.size),
search_label: t(".search_accounts"),
apply_text: t(".apply"),
hidden_fields: other_view_params.merge(cashflow_month: cashflow_data[:month].iso8601)
} do %>
<% accounts.each do |account| %>
<%= render layout: "pages/dashboard/filter_popover_option", locals: {
name: "cashflow_account_ids[]",
value: account.id,
checked: selected_account_ids.include?(account.id.to_s),
filter_name: account.name
} do %>
<span class="text-sm text-primary truncate"><%= account.name %></span>
<% end %>
<% end %>
<% end %>
</div>
</div>
<% if cashflow_data[:has_categories] %>
<%# The donut controller wraps the lists as well as the chart: the rows'
hover actions highlight ring segments, and Stimulus only binds
actions inside the controller's own element. %>
<div class="flex flex-col @2xl:flex-row gap-8 items-center"
data-controller="donut-chart"
data-donut-chart-rings-value="<%= [ cashflow_data[:inflows], cashflow_data[:outflows] ].to_json %>"
data-donut-chart-segment-opacity-value="0.9"
data-donut-chart-extended-hover-value="true"
data-donut-chart-hover-extension-value="3"
data-donut-chart-enable-click-value="true"
data-donut-chart-start-date-value="<%= period.date_range.first %>"
data-donut-chart-end-date-value="<%= period.date_range.last %>"
data-donut-chart-account-ids-value="<%= (drilldown_account_ids || []).to_json %>"
data-donut-chart-statuses-value="<%= drilldown_statuses.to_json %>">
<!-- Donut Chart: outer ring inflows, inner ring outflows -->
<div class="w-full @2xl:w-1/3 max-w-75 px-4">
<div class="h-75 relative">
<div data-donut-chart-target="chartContainer" class="absolute inset-0 pointer-events-none"></div>
<div data-donut-chart-target="contentContainer" class="flex justify-center items-center h-full">
<div data-donut-chart-target="defaultContent" class="flex flex-col items-center">
<div class="text-secondary text-sm mb-2">
<span><%= t(".spent") %></span>
</div>
<div data-donut-chart-target="amount" class="text-3xl font-medium text-primary privacy-sensitive whitespace-nowrap">
<%= format_money Money.new(cashflow_data[:total_expense], cashflow_data[:currency]) %>
</div>
<p class="text-sm text-secondary privacy-sensitive whitespace-nowrap mt-1">
<%= t(".of_income", amount: format_money(Money.new(cashflow_data[:total_income], cashflow_data[:currency]))) %>
</p>
<% if cashflow_data[:net].negative? %>
<p class="text-sm font-medium text-destructive privacy-sensitive whitespace-nowrap mt-2">
<%= t(".over_amount", amount: format_money(Money.new(cashflow_data[:net].abs, cashflow_data[:currency]))) %>
</p>
<% else %>
<p class="text-sm font-medium text-success privacy-sensitive whitespace-nowrap mt-2">
<%= t(".left_amount", amount: format_money(Money.new(cashflow_data[:net], cashflow_data[:currency]))) %>
</p>
<% end %>
</div>
<% all_segments.each do |segment| %>
<div id="segment_<%= segment[:id] %>" class="hidden">
<div class="flex flex-col gap-2 items-center">
<p class="text-sm text-secondary"><%= segment[:name] %></p>
<p data-donut-chart-target="amount" class="text-3xl font-medium text-primary privacy-sensitive whitespace-nowrap">
<%= format_money Money.new(segment[:amount], segment[:currency]) %>
</p>
<p class="text-sm text-secondary privacy-sensitive"><%= segment[:percentage] %>%</p>
</div>
</div>
<% end %>
</div>
</div>
</div>
<!-- Inflow and outflow lists -->
<div class="w-full @2xl:w-2/3 bg-container-inset rounded-xl p-1 space-y-1 overflow-x-auto">
<% groups.each do |group| %>
<div class="px-4 py-2 flex items-center uppercase text-xs font-medium text-secondary">
<div class="w-16 @2xl:w-40">
<p><%= group[:label] %><span class="text-subdued mx-1">·</span><%= group[:segments].size %></p>
</div>
<div class="ml-auto text-right flex items-center gap-6">
<div class="w-16 @2xl:w-40">
<p class="privacy-sensitive normal-case text-primary"><%= format_money Money.new(group[:total], cashflow_data[:currency]) %></p>
</div>
<div class="w-16">
<p><%= t(".weight") %></p>
</div>
</div>
</div>
<% if group[:segments].any? %>
<div class="py-3 shadow-border-xs rounded-lg bg-container font-medium text-sm max-w-full">
<% group[:segments].each_with_index do |segment, idx| %>
<%
segment_content = capture do
%>
<div class="flex items-center gap-3 flex-1 min-w-0">
<div class="h-7 w-7 flex-shrink-0 group-hover:scale-105 transition-all duration-300 rounded-full flex justify-center items-center"
style="
background-color: color-mix(in oklab, <%= segment[:color] %> 10%, transparent);
border-color: color-mix(in oklab, <%= segment[:color] %> 10%, transparent);
color: <%= segment[:color] %>;">
<% if segment[:icon] %>
<%= icon(segment[:icon], color: "current", size: "sm") %>
<% else %>
<%= render DS::FilledIcon.new(
variant: :text,
hex_color: segment[:color],
text: segment[:name],
size: "sm",
rounded: true
) %>
<% end %>
</div>
<span class="text-sm font-medium text-primary truncate"><%= segment[:name] %></span>
</div>
<div class="flex items-center gap-4 flex-shrink-0 text-right">
<span class="text-sm font-medium text-primary whitespace-nowrap privacy-sensitive"><%= format_money Money.new(segment[:amount], segment[:currency]) %></span>
<span class="text-sm text-secondary whitespace-nowrap w-10 @2xl:w-15 privacy-sensitive"><%= segment[:percentage] %>%</span>
</div>
<% end %>
<% if segment[:clickable] %>
<%= link_to transactions_path(q: { categories: [ segment[:filter_value] ], status: drilldown_statuses, start_date: period.date_range.first, end_date: period.date_range.last, account_ids: drilldown_account_ids }.compact),
class: "flex items-center justify-between mx-3 p-3 rounded-lg cursor-pointer group gap-3",
data: {
turbo_frame: "_top",
turbo_prefetch: false,
category_id: segment[:id],
action: "mouseenter->donut-chart#highlightSegment mouseleave->donut-chart#unhighlightSegment"
} do %>
<%= segment_content %>
<% end %>
<% else %>
<div class="flex items-center justify-between mx-3 p-3 rounded-lg group gap-3"
data-category-id="<%= segment[:id] %>"
data-action="mouseenter->donut-chart#highlightSegment mouseleave->donut-chart#unhighlightSegment">
<%= segment_content %>
</div>
<% end %>
<% if idx < group[:segments].size - 1 %>
<%= render "shared/ruler", classes: "mx-3 @2xl:mx-4" %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
</div>
</div>
<% else %>
<p class="text-sm text-secondary text-center py-8"><%= t(".no_data") %></p>
<% end %>
</div>