Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
4.7 KB
Raw Blame
<%# locals: (entry:, entryable:) %>
<%
  label = entryable.investment_activity_label
  has_label = label.present?

  # Build the correct URL based on entryable type
  update_url = entryable.is_a?(Transaction) ? transaction_path(entry) : trade_path(entry)

  # Color mapping for different investment activity labels using design system tokens
  color = if has_label
    case label
    when "Buy"
      "var(--color-blue-500)"
    when "Sell"
      "var(--color-red-500)"
    when "Dividend", "Interest"
      "var(--color-green-500)"
    when "Contribution"
      "var(--color-violet-500)"
    when "Withdrawal"
      "var(--color-orange-500)"
    when "Fee"
      "var(--color-gray-500)"
    when "Transfer", "Sweep In", "Sweep Out", "Exchange"
      "var(--color-gray-500)"
    when "Reinvestment"
      "var(--color-blue-500)"
    else
      "var(--color-gray-500)" # for "Other"
    end
  else
    "var(--color-gray-400)" # slightly lighter for empty state
  end

  activity_labels = entryable.is_a?(Trade) ? Trade::ACTIVITY_LABELS : Transaction::ACTIVITY_LABELS
  entryable_type = entryable.is_a?(Trade) ? "Trade" : "Transaction"
  convert_url = entryable.is_a?(Transaction) ? convert_to_trade_transaction_path(entryable) : nil
  income_trade = entryable.is_a?(Trade) && %w[Dividend Interest].include?(label)
%>

<div class="relative"
     data-controller="activity-label-quick-edit"
     data-activity-label-quick-edit-url-value="<%= update_url %>"
     data-activity-label-quick-edit-entryable-id-value="<%= entryable.id %>"
     data-activity-label-quick-edit-current-label-value="<%= label %>"
     data-activity-label-quick-edit-entryable-type-value="<%= entryable_type %>"
     <% if convert_url %>
     data-activity-label-quick-edit-convert-url-value="<%= convert_url %>"
     <% end %>>

  <%# Dividend/Interest trades have no dropdown to open (income_trade), so this
      renders as a plain <span> rather than a <button> — a real <button> with no
      click handler would still be excluded from the row's whole-row click
      delegation (clickable_row_controller.js excludes "a, button, input, select,
      textarea, label"), creating a dead spot identical to the bug issue #3366
      fixed for the rest of the row. %>
  <%= content_tag(income_trade ? :span : :button,
        type: income_trade ? nil : "button",
        class: "inline-flex items-center gap-1 text-xs font-medium rounded-full px-2.5 py-1 #{income_trade ? "cursor-pointer" : "cursor-pointer hover:opacity-80"} transition-opacity",
        style: (has_label ? "background-color: color-mix(in oklab, #{color} 15%, transparent); border: 1px solid color-mix(in oklab, #{color} 25%, transparent); color: #{color};" : "background-color: var(--color-surface-inset); border: 1px solid var(--color-border-secondary); color: var(--color-text-secondary);"),
        data: (income_trade ? {} : { action: "click->activity-label-quick-edit#toggle", activity_label_quick_edit_target: "badge" }),
        aria: (income_trade ? {} : { expanded: "false" }),
        title: (has_label ? t("transactions.transaction.activity_type_tooltip") : t("transactions.show.activity_type"))) do %>
    <% if has_label %>
      <%= t("transactions.activity_labels.#{label.parameterize(separator: "_")}", default: label) %>
    <% else %>
      <%= icon "tag", size: "xs", color: "current" %>
      <span><%= t("transactions.show.activity_type") %></span>
    <% end %>
    <% unless income_trade %>
      <%= icon "chevron-down", size: "xs", color: "current" %>
    <% end %>
  <% end %>

  <% unless income_trade %>
    <!-- Dropdown menu -->
    <div class="hidden absolute right-0 z-50 mt-1 w-44 rounded-lg border border-primary bg-container shadow-lg"
         data-activity-label-quick-edit-target="dropdown">
      <div class="py-1 max-h-64 overflow-y-auto">
        <% if has_label %>
          <button type="button"
                  class="w-full text-left px-3 py-1.5 text-sm text-secondary hover:bg-surface-inset transition-colors border-b border-primary"
                  data-action="click->activity-label-quick-edit#select"
                  data-label="">
            <span class="italic"><%= t("transactions.form.none") %></span>
          </button>
        <% end %>
        <% activity_labels.each do |activity_label| %>
          <button type="button"
                  class="w-full text-left px-3 py-1.5 text-sm text-primary hover:bg-surface-inset transition-colors <%= activity_label == label ? "font-semibold bg-surface-inset" : "" %>"
                  data-action="click->activity-label-quick-edit#select"
                  data-label="<%= activity_label %>">
            <%= t("transactions.activity_labels.#{activity_label.parameterize(separator: "_")}", default: activity_label) %>
          </button>
        <% end %>
      </div>
    </div>
  <% end %>
</div>