Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
15.0 KB
Raw Blame
<%= render DS::Dialog.new(frame: "drawer", responsive: true) do |dialog| %>
  <% dialog.with_header(custom_header: true) do %>
    <div class="flex items-center justify-between">
      <div>
        <%= tag.h3 @holding.name, class: "text-2xl font-medium text-primary" %>
        <%= tag.p @holding.ticker, class: "text-sm text-secondary" %>
      </div>
      <div class="flex items-center gap-3">
        <% if (logo = @holding.security.display_logo_url).present? %>
          <%= image_tag logo, loading: "lazy", class: "w-9 h-9 rounded-full" %>
        <% else %>
          <%= render DS::FilledIcon.new(variant: :text, text: @holding.name, size: "md", rounded: true) %>
        <% end %>
        <%= dialog.close_button %>
      </div>
    </div>
  <% end %>
  <% dialog.with_body do %>
    <% dialog.with_section(title: t(".overview"), open: true) do %>
      <div class="pb-4">
        <% if @holding.security.provider_status == :provider_unavailable %>
          <div class="px-3 pt-2">
            <%= render DS::Alert.new(
              message: t(".provider_disabled_warning", provider: @holding.security.price_provider&.humanize || "Unknown"),
              variant: :warning
            ) %>
          </div>
        <% end %>
        <% if (first_on = @holding.security.first_provider_price_on).present? &&
              (earliest_trade_date = @holding.trades.minimum(:date)) &&
              earliest_trade_date < first_on %>
          <div class="px-3 pt-2">
            <%= render DS::Alert.new(
              message: t(".truncated_history_warning", date: l(first_on, format: :long)),
              variant: :warning
            ) %>
          </div>
        <% end %>
        <dl class="space-y-3 px-3 py-2">
          <div data-controller="holding-security-remap">
            <div class="flex items-center justify-between text-sm">
              <dt class="text-secondary"><%= t(".security_label") %></dt>
              <dd class="text-primary flex items-center gap-2">
                <%= @holding.ticker %>
                <% if @holding.security_remapped? && @holding.account.linked? %>
                  <span class="text-xs text-secondary">
                    (<%= t(".originally", ticker: @holding.provider_security.ticker) %>)
                  </span>
                  <%= icon "lock", size: "xs", class: "text-secondary" %>
                <% end %>
                <button type="button"
                        data-action="click->holding-security-remap#toggle"
                  class="text-secondary hover:text-primary"
                  aria-label="<%= t(".edit_security") %>">
                  <%= icon "pencil", size: "xs" %>
                </button>
              </dd>
            </div>
            <div data-holding-security-remap-target="form" class="hidden mt-3 space-y-3">
              <% if Security.providers.any? %>
                <%= styled_form_with url: remap_security_holding_path(@holding), method: :patch, class: "space-y-3" do |f| %>
                  <div class="form-field combobox">
                    <%= f.combobox :security_id,
                                  securities_path(country_code: Current.family.country),
                                  label: t(".search_security"),
                                  placeholder: t(".search_security_placeholder"),
                                  required: true %>
                  </div>
                  <div class="flex justify-end gap-2">
                    <button type="button"
                            class="inline-flex items-center gap-1 px-3 py-2 rounded-lg text-sm font-medium text-primary button-bg-secondary-strong hover:button-bg-secondary-strong-hover"
                            data-action="click->holding-security-remap#toggle">
                      <%= t(".cancel") %>
                    </button>
                    <%= f.submit t(".remap_security") %>
                  </div>
                <% end %>
              <% else %>
                <p class="text-xs text-secondary"><%= t(".no_security_provider") %></p>
              <% end %>
            </div>
          </div>
          <div class="flex items-center justify-between text-sm">
            <dt class="text-secondary"><%= t(".current_market_price_label") %></dt>
            <dd id="<%= dom_id(@holding, :current_market_price) %>" class="text-primary">
              <% begin %>
                <%= @holding.security.current_price ? current_price_with_quote(@holding.security, @holding.currency) : t(".unknown") %>
                <% rescue ActiveRecord::RecordInvalid %>
                <%= t(".unknown") %>
                <% rescue StandardError => e %>
                <% logger.error "Error fetching current price for security #{@holding.security.id}: #{e.message}" %>
                <% logger.error e.backtrace.first(5).join("\n") %>
                <%= t(".unknown") %>
              <% end %>
            </dd>
          </div>
          <div class="flex items-center justify-between text-sm">
            <dt class="text-secondary"><%= t(".shares_label") %></dt>
            <dd id="<%= dom_id(@holding, :shares) %>" class="text-primary"><%= format_quantity(@holding.qty, exact: true) %></dd>
          </div>
          <div class="flex items-center justify-between text-sm">
            <dt class="text-secondary"><%= t(".portfolio_weight_label") %></dt>
            <dd class="text-primary"><%= @holding.weight ? number_to_percentage(@holding.weight, precision: 2) : t(".unknown") %></dd>
          </div>
          <%# Average Cost with inline editor %>
          <div data-controller="drawer-cost-basis">
            <div class="flex items-center justify-between text-sm">
              <dt class="text-secondary"><%= t(".avg_cost_label") %></dt>
              <dd class="text-primary flex items-center gap-1">
                <%= @holding.avg_cost ? format_money(@holding.avg_cost) : t(".unknown") %>
                <% if @holding.cost_basis_locked? %>
                  <%= icon "lock", size: "xs", class: "text-secondary" %>
                <% end %>
                <% if @holding.cost_basis_source.present? %>
                  <span class="text-xs text-secondary">(<%= @holding.cost_basis_source_label %>)</span>
                <% end %>
                <button type="button" class="ml-1" data-action="click->drawer-cost-basis#toggle" data-drawer-cost-basis-target="opener">
                  <%= icon "pencil", size: "xs", class: "text-secondary hover:text-primary" %>
                </button>
              </dd>
            </div>
            <%# Inline cost basis editor (hidden by default) %>
            <div class="hidden mt-3 space-y-3" data-drawer-cost-basis-target="form">
              <p class="text-xs text-secondary">
                <%= t("holdings.cost_basis_cell.set_cost_basis_header", ticker: @holding.ticker) %>
              </p>
              <%= render "holdings/cost_basis_form", holding: @holding, context: :drawer, cancel_action: "drawer-cost-basis#toggle" %>
            </div>
          </div>
          <% if @lots.any? %>
            <%= render "holdings/lots", holding: @holding, lots: @lots, can_edit: @can_write_lots, provider_cost: @lots_provider_cost %>
          <% end %>
          <div class="flex items-center justify-between text-sm">
            <dt class="text-secondary"><%= t(".book_value_label") %></dt>
            <dd class="text-primary">
              <% book_value = @holding.avg_cost ? @holding.avg_cost * @holding.qty : nil %>
              <%= book_value ? format_money(book_value) : t(".unknown") %>
            </dd>
          </div>
          <div id="<%= dom_id(@holding, :market_value) %>" class="flex items-center justify-between text-sm">
            <dt class="text-secondary"><%= t(".market_value_label") %></dt>
            <dd class="text-primary"><%= format_money(@holding.amount_money) %></dd>
          </div>
          <div id="<%= dom_id(@holding, :total_return) %>" class="flex items-center justify-between text-sm">
            <dt class="text-secondary"><%= t(".total_return_label") %></dt>
            <% if @holding.trend %>
              <dd style="color: <%= @holding.trend.color %>;">
                <%= render("shared/trend_change", trend: @holding.trend) %>
              </dd>
            <% else %>
              <dd class="text-secondary"><%= t(".unknown") %></dd>
            <% end %>
          </div>
        </dl>
      </div>
    <% end %>
    <% dialog.with_section(title: t(".history"), open: true) do %>
      <div class="space-y-2">
        <div class="px-3 py-4">
          <% if @holding.trades.any? %>
            <ul class="space-y-2">
              <% @holding.trades.each_with_index do |trade_entry, index| %>
                <li class="flex gap-4 text-sm space-y-1">
                  <div class="flex flex-col items-center gap-1.5 pt-2">
                    <div class="rounded-full h-1.5 w-1.5 bg-gray-300"></div>
                    <% unless index == @holding.trades.length - 1 %>
                      <div class="h-12 w-px bg-alpha-black-200"></div>
                    <% end %>
                  </div>
                  <div>
                    <p class="text-secondary text-xs uppercase"><%= l(trade_entry.date, format: :long) %></p>
                    <p class="text-primary"><%= t(
                      ".trade_history_entry",
                      qty: trade_entry.trade.qty,
                      security: trade_entry.trade.security.ticker,
                      price: trade_entry.trade.price_money.format
                    ) %></p>
                  </div>
                </li>
              <% end %>
            </ul>
          <% else %>
            <p class="text-secondary"><%= t(".no_trade_history") %></p>
          <% end %>
        </div>
      </div>
    <% end %>
    <% if @holding.cost_basis_locked? || (@holding.security_remapped? && @holding.account.linked?) || @holding.account.can_delete_holdings? || !@holding.security.offline? || @holding.security.provider_status == :provider_unavailable %>
      <% dialog.with_section(title: t(".settings"), open: true) do %>
        <div class="pb-4">
          <% if @holding.security.provider_status == :provider_unavailable %>
            <div class="flex items-center justify-between gap-2 p-3 border-b border-tertiary">
              <div class="text-sm space-y-1 flex-1">
                <h4 class="text-primary"><%= t(".switch_provider_label") %></h4>
                <p class="text-secondary"><%= t(".switch_provider_description", provider: @holding.security.price_provider&.humanize || "Unknown") %></p>
                <% if Security.providers.any? %>
                  <%= form_with url: remap_security_holding_path(@holding), method: :patch, data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value": "change" } do |f| %>
                    <div class="form-field combobox mt-2">
                      <%= f.combobox :security_id,
                                    securities_path(country_code: Current.family.country),
                                    id: "switch_provider_security_id",
                                    placeholder: t(".search_security_placeholder"),
                                    data: { "auto-submit-form-target": "auto" } %>
                    </div>
                  <% end %>
                <% else %>
                  <p class="text-xs text-secondary mt-2"><%= t(".no_security_provider") %></p>
                <% end %>
              </div>
            </div>
          <% end %>
          <% if @holding.security_remapped? && @holding.account.linked? %>
            <div class="flex items-center justify-between gap-2 p-3 border-b border-tertiary">
              <div class="text-sm space-y-1">
                <h4 class="text-primary"><%= t(".security_remapped_label") %></h4>
                <p class="text-secondary"><%= t(".provider_sent", ticker: @holding.provider_security.ticker) %></p>
              </div>
              <%= render DS::Button.new(
                    text: t(".reset_to_provider"),
                    variant: :secondary,
                    href: reset_security_holding_path(@holding),
                    method: :post,
                    form: { data: { turbo_frame: "_top" } },
                    confirm: CustomConfirm.new(
                      title: t(".reset_confirm_title"),
                      body: t(".reset_confirm_body", current: @holding.security.ticker, original: @holding.provider_security.ticker)
                    )
                  ) %>
            </div>
          <% end %>
          <% unless @holding.security.offline? %>
            <div id="<%= dom_id(@holding, :market_data_section) %>" class="flex items-center justify-between gap-2 p-3 border-b border-tertiary">
              <div class="text-sm space-y-1">
                <h4 class="text-primary"><%= t(".market_data_label") %></h4>
                <p class="text-secondary">
                  <%= t(".last_price_update") %>: <%= @last_price_updated ? l(@last_price_updated, format: :long) : t(".never") %>
                </p>
              </div>
              <%= render DS::Button.new(
                    text: t(".market_data_sync_button"),
                    variant: :secondary,
                    href: sync_prices_holding_path(@holding),
                    method: :post,
                    data: { loading_button_target: "button" },
                    form: { data: {
                      controller: "loading-button",
                      action: "submit->loading-button#showLoading",
                      loading_button_loading_text_value: t(".syncing")
                    } }
                  ) %>
            </div>
          <% end %>
          <% if @holding.cost_basis_locked? %>
            <div class="flex items-center justify-between gap-2 p-3 border-b border-tertiary">
              <div class="text-sm space-y-1">
                <h4 class="text-primary"><%= t(".cost_basis_locked_label") %></h4>
                <p class="text-secondary"><%= t(".cost_basis_locked_description") %></p>
              </div>
              <%= render DS::Button.new(
                    text: t(".unlock_cost_basis"),
                    variant: :secondary,
                    href: unlock_cost_basis_holding_path(@holding),
                    method: :post,
                    form: { data: { turbo_frame: "_top" } },
                    confirm: CustomConfirm.new(title: t(".unlock_confirm_title"), body: t(".unlock_confirm_body"))
                  ) %>
            </div>
          <% end %>
          <% if @holding.account.can_delete_holdings? %>
            <div class="flex items-center justify-between gap-2 p-3">
              <div class="text-sm space-y-1">
                <h4 class="text-primary"><%= t(".delete_title") %></h4>
                <p class="text-secondary"><%= t(".delete_subtitle") %></p>
              </div>
              <%= button_to t(".delete"),
                  holding_path(@holding),
                  method: :delete,
                  class: "rounded-lg px-3 py-2 text-red-500 text-sm font-medium border border-secondary",
                  data: { turbo_confirm: true } %>
            </div>
          <% end %>
        </div>
      <% end %>
    <% end %>
  <% end %>
<% end %>