The personal finance app for everyone (by everyone)
Ruby
<%# locals: (spending_trend_data:, view_params:) %>
<%
data = spending_trend_data
period = data[:current_period]
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"
has_data = [ data[:current], *data[:comparisons].map { |c| c[:series] } ].any? { |series| series.any? { |p| p[:value].positive? } }
chart_data = {
days: data[:days],
current_days: data[:current_days],
axis_labels: data[:axis_labels],
current: data[:current],
comparisons: data[:comparisons]
}
previous_label = data[:previous_label]
previous_comparison_day = data[:previous_comparison_day]
date_range_short = data[:date_range_short]
category_options = data[:category_options]
# 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(:spending_month, :spending_category_ids)
%>
<div id="spending-trend-section" class="px-4 space-y-4">
<%# Both rows wrap, so on narrow widths the controls drop below the date
and then onto a second line instead of overflowing the card. %>
<div class="flex flex-wrap items-center justify-between gap-3">
<p class="text-sm text-secondary hidden sm:block">
<%= t(".date_range", start_date: l(period.date_range.begin, format: :long), end_date: l(period.date_range.end, format: :long)) %>
</p>
<p class="text-sm text-secondary sm:hidden">
<%= date_range_short %>
</p>
<div class="flex flex-wrap 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(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(spending_month: month.iso8601, spending_category_ids: data[:category_ids]).compact),
frame: "dashboard_sections",
selected: month == data[:month],
data: { turbo_action: "advance" }
) %>
<% end %>
<% end %>
<%= render DS::Menu.new(variant: :button, placement: "bottom-end") do |menu| %>
<% menu.with_button(type: "button", class: pill_button_class, aria: { label: t(".compare_picker_aria_label") }) do %>
<span><%= t(".compare_months", count: data[:compare_months]) %></span>
<%= icon("chevron-down", size: "sm") %>
<% end %>
<% PagesController::SPENDING_COMPARE_MONTHS.each do |months| %>
<% menu.with_item(
variant: :link,
text: t(".compare_months", count: months),
href: root_path(other_view_params.merge(spending_month: data[:month].iso8601, spending_compare_months: (months if months > PagesController::SPENDING_COMPARE_MONTHS.min), spending_category_ids: data[:category_ids]).compact),
frame: "dashboard_sections",
selected: months == data[:compare_months],
data: { turbo_action: "advance" }
) %>
<% end %>
<% end %>
<%= render layout: "pages/dashboard/filter_popover", locals: {
button_text: data[:category_ids] ? t(".filter_categories", count: data[:selected_category_count]) : t(".filter_categories_all"),
search_label: t(".search_categories"),
apply_text: t(".apply"),
hidden_fields: other_view_params.merge(spending_month: data[:month].iso8601),
list_controllers: "list-filter category-filter"
} do %>
<%# Same cascade as the transactions category filter: a parent is
submitted only while all its subcategories are checked, and
picking a parent covers its subcategories. %>
<% category_options.each do |option| %>
<% category = option.category %>
<%= render layout: "pages/dashboard/filter_popover_option", locals: {
name: "spending_category_ids[]",
value: option.value,
checked: option.checked,
filter_name: category.display_name,
checkbox_data: {
category_filter_target: "checkbox",
action: "change->category-filter#toggle",
category_id: category.id,
parent_id: category.parent_id
}
} do %>
<% if category.parent_id.present? %>
<%= icon("corner-down-right", size: "sm") %>
<% end %>
<%= render partial: "categories/badge", locals: { category: category } %>
<% end %>
<% end %>
<% end %>
</div>
</div>
<div class="flex items-center justify-between gap-4">
<div class="flex items-center gap-3 min-w-0">
<span class="w-1 h-10 rounded-full bg-success shrink-0"></span>
<div class="min-w-0">
<p class="text-sm text-primary capitalize truncate"><%= I18n.l(data[:month], format: :month_year) %></p>
<p class="flex flex-wrap items-baseline gap-x-2">
<span class="font-medium text-lg tabular-nums text-primary privacy-sensitive whitespace-nowrap"><%= format_money data[:current_total] %></span>
<span class="text-sm tabular-nums privacy-sensitive whitespace-nowrap <%= data[:delta].negative? ? "text-success" : (data[:delta].zero? ? "text-secondary" : "text-destructive") %>">
<%= format_money data[:delta] %>
</span>
</p>
</div>
</div>
<div class="flex items-center gap-3 min-w-0">
<span class="w-1 h-10 rounded-full bg-subdued shrink-0"></span>
<div class="min-w-0">
<p class="text-sm text-secondary truncate">
<span class="capitalize"><%= previous_label %></span>
<%# While the selected month is still running, the figure below covers %>
<%# the same elapsed days rather than the whole previous month. %>
<% if previous_comparison_day %>
<span class="text-xs"><%= t(".previous_comparison_days", end_day: previous_comparison_day) %></span>
<% end %>
</p>
<p class="font-medium text-lg tabular-nums text-secondary privacy-sensitive whitespace-nowrap"><%= format_money data[:previous_total] %></p>
</div>
</div>
</div>
<% if has_data %>
<div class="w-full min-h-52 privacy-sensitive"
style="height: var(--dash-widget-h, 13rem);"
data-controller="spending-chart"
data-spending-chart-data-value="<%= chart_data.to_json %>"
data-spending-chart-currency-value="<%= Current.family.currency %>"
data-spending-chart-current-label-value="<%= I18n.l(data[:month], format: :month_year).capitalize %>">
</div>
<% else %>
<div class="w-full h-52 flex items-center justify-center">
<p class="text-secondary text-sm"><%= t(".no_data") %></p>
</div>
<% end %>
</div>