Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
9.0 KB
Raw Blame
<% content_for :page_header do %>
  <div class="space-y-4 mb-6">
    <header class="flex justify-between items-center text-primary font-medium gap-4">
      <div class="space-y-1">
        <h1 class="text-xl lg:text-3xl font-medium text-primary">
          <%= t("reports.index.title") %>
        </h1>
        <p class="text-sm lg:text-base text-secondary">
          <%= t("reports.index.subtitle") %>
        </p>
      </div>

      <%# Flash messages %>
      <% if flash[:alert].present? %>
        <div class="p-3 rounded-lg bg-destructive-surface text-sm text-destructive">
          <%= flash[:alert] %>
        </div>
      <% end %>

      <%# Print Report Button %>
        <%= link_to print_reports_path(period_type: @period_type, start_date: @start_date, end_date: @end_date, **reports_filter_params),
                  target: "_blank",
                  rel: "noopener",
                  aria: { label: t("reports.index.print_report") },
                  class: "font-medium whitespace-nowrap inline-flex items-center gap-1 rounded-lg px-3 py-2 text-sm text-primary border border-secondary bg-transparent hover:bg-surface-hover" do %>
        <%= icon("printer", size: "sm") %>
        <span class="hidden sm:inline"><%= t("reports.index.print_report") %></span>
      <% end %>
    </header>

    <%# Period Navigation Tabs %>
    <div class="flex flex-wrap items-center justify-between gap-4">
      <%= render DS::SegmentedControl.new(aria_label: t("reports.index.period_aria", default: "Report period"), class: "max-w-full overflow-x-auto no-scrollbar") do |sc| %>
        <% %i[monthly quarterly ytd last_6_months last_12_months all_time custom].each do |ptype| %>
          <% sc.with_segment(t("reports.index.periods.#{ptype}"),
               active: @period_type == ptype,
               href: report_view_path(period_type: ptype, start_date: nil, end_date: nil),
               class: "whitespace-nowrap") %>
        <% end %>
      <% end %>

      <% if @tags.any? %>
        <%= render layout: "pages/dashboard/filter_popover", locals: {
              button_text: @filter_tags.empty? ? t("reports.index.tag_filter.all") : t("reports.index.tag_filter.selected", count: @filter_tags.size),
              search_label: t("reports.index.tag_filter.search"),
              apply_text: t("reports.index.tag_filter.apply"),
              hidden_fields: {
                period_type: @period_type,
                start_date: @start_date,
                end_date: @end_date,
                trend_category_ids: @category_selection.selected_values.presence,
                trend_granularity: trend_granularity_param
              }.compact,
              url: reports_path,
              form_data: { turbo_action: "advance" }
            } do %>
          <% @tags.each do |tag| %>
            <%= render layout: "pages/dashboard/filter_popover_option", locals: {
                  name: "filter_tag_ids[]",
                  value: tag.id,
                  checked: @filter_tags.include?(tag),
                  filter_name: tag.name
                } do %>
              <%= render "tags/badge", tag: tag %>
            <% end %>
          <% end %>
        <% end %>
      <% end %>
    </div>

    <%# Custom Date Range Picker (only shown when custom is selected) %>
    <% if @period_type == :custom %>
      <%= form_with url: reports_path, method: :get, data: { controller: "auto-submit-form" }, class: "flex items-center gap-3 bg-surface-inset p-3 rounded-lg" do |f| %>
        <%= f.hidden_field :period_type, value: :custom %>
        <% reports_filter_params.each do |key, values| %>
          <% Array(values).each do |value| %>
            <%= hidden_field_tag "#{key}[]", value, id: nil %>
          <% end %>
        <% end %>
        <% @category_selection.selected_values.each do |value| %>
          <%= hidden_field_tag "trend_category_ids[]", value, id: nil %>
        <% end %>
        <%# Only an explicit choice is forwarded. An automatic default must not
            become sticky when the next submit shrinks the range. %>
        <% if trend_granularity_param.present? %>
          <%= hidden_field_tag :trend_granularity, trend_granularity_param %>
        <% end %>

        <div class="flex items-center gap-2">
          <label class="text-sm font-medium text-secondary"><%= t("reports.index.date_range.from") %></label>
          <%= f.date_field :start_date,
                           value: @start_date.strftime("%Y-%m-%d"),
                           data: { auto_submit_form_target: "auto" },
                           autocomplete: "off",
                           class: "px-3 py-1.5 border border-primary rounded-lg text-sm bg-container-inset text-primary" %>
        </div>
        <span class="text-secondary">—</span>
        <div class="flex items-center gap-2">
          <label class="text-sm font-medium text-secondary"><%= t("reports.index.date_range.to") %></label>
          <%= f.date_field :end_date,
                           value: @end_date.strftime("%Y-%m-%d"),
                           data: { auto_submit_form_target: "auto" },
                           autocomplete: "off",
                           class: "px-3 py-1.5 border border-primary rounded-lg text-sm bg-container-inset text-primary" %>
        </div>
      <% end %>
    <% end %>

    <% if @period_type == :all_time %>
      <p class="text-primary font-medium text-lg lg:text-base mb-4">
        <%= t("reports.index.period_label.all_time",
              start: I18n.l(@start_date, format: :short_month_year),
              end: I18n.l(@end_date, format: :short_month_year)) %>
      </p>
    <% end %>

    <%# Period Display %>
    <% if @nav %>
      <div class="flex items-center gap-1 mb-4">
        <div class="flex items-center gap-2">
          <%= render DS::Tooltip.new(text: t("reports.index.shortcut_hint", action: t("reports.index.previous_period"), key: "←")) do |tooltip| %>
            <% tooltip.with_trigger do %>
              <%= render DS::Link.new(
                variant: "icon",
                icon: "chevron-left",
                href: report_view_path(period_type: @period_type, start_date: @nav[:prev_start], end_date: @nav[:prev_end]),
                aria: { label: t("reports.index.previous_period"), keyshortcuts: "ArrowLeft" },
                data: { controller: "hotkey", hotkey: "ArrowLeft" },
              ) %>
            <% end %>
          <% end %>

          <% if @nav[:at_latest] %>
            <button disabled aria-label="<%= t("reports.index.next_period") %>" class="text-subdued inline-flex items-center justify-center w-9 h-9 rounded-lg cursor-not-allowed">
              <%= icon("chevron-right", color: "current") %>
            </button>
          <% else %>
            <%= render DS::Tooltip.new(text: t("reports.index.shortcut_hint", action: t("reports.index.next_period"), key: "→")) do |tooltip| %>
              <% tooltip.with_trigger do %>
                <%= render DS::Link.new(
                  variant: "icon",
                  icon: "chevron-right",
                  href: report_view_path(period_type: @period_type, start_date: @nav[:next_start], end_date: @nav[:next_end]),
                  aria: { label: t("reports.index.next_period"), keyshortcuts: "ArrowRight" },
                  data: { controller: "hotkey", hotkey: "ArrowRight" },
                ) %>
              <% end %>
            <% end %>
          <% end %>
        </div>

        <% if [:monthly, :quarterly, :ytd].include?(@period_type) %>
          <%= render DS::Popover.new(variant: "button") do |popover| %>
            <% popover.with_button class: "flex items-center gap-1 hover:bg-surface-hover cursor-pointer rounded-md p-2" do %>
              <span class="text-primary font-medium text-lg lg:text-base"><%= @nav[:label] %></span>
              <%= icon("chevron-down") %>
            <% end %>

            <% popover.with_custom_content do %>
              <%= render "reports/period_picker", period_type: @period_type, start_date: @start_date %>
            <% end %>
          <% end %>
        <% else %>
          <span class="text-primary font-medium text-lg lg:text-base"><%= @nav[:label] %></span>
        <% end %>

        <div class="ml-auto">
          <%= render DS::Link.new(
            text: t("reports.index.today"),
            variant: "outline",
            href: report_view_path(period_type: @period_type, start_date: nil, end_date: nil),
          ) %>
        </div>
      </div>
    <% end %>

    <%# When the report was built. A stale one is being rebuilt in the
        background; Refresh rebuilds it now, while the member waits. %>
    <div class="flex items-center gap-2 text-xs text-secondary" data-testid="report-built-at">
      <span><%= t("reports.index.built_at", time: time_ago_in_words(@report_built_at)) %></span>
      <% if @report_stale %>
        <span>· <%= t("reports.index.updating") %></span>
      <% end %>
      <span aria-hidden="true">·</span>
      <%= render DS::Link.new(text: t("reports.index.refresh"), href: reports_path(request.query_parameters.merge(refresh: 1))) %>
    </div>
  </div>
<% end %>

<%= render "reports/body", report: @report, sections: @reports_sections %>