Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
8.6 KB
Raw Blame
<%# locals: (family:, active_tab:, mobile: false) %>

<%# skip_digest: this fragment renders DS::* view components, which Rails'
    ERB dependency tracker mis-parses as a bogus "Ds/D" template dependency
    and then logs "Couldn't find template for digesting: Ds/D" on every cache
    miss. The cache key is already manually versioned via
    account_sidebar_tabs_cache_key ("account_sidebar_tabs_v12",
    invalidate_on_data_updates: true), so we opt out of automatic template
    digesting. Bump that version suffix when changing this template or its
    sidebar dependencies. %>
<% cache account_sidebar_tabs_cache_key(family: family, active_tab: active_tab, mobile: mobile), expires_in: 12.hours, skip_digest: true do %>
<% sortable = Current.user&.account_order&.manual? %>
<%= tag.div id: "account-sidebar-tabs",
      data: sortable ? {
        controller: "account-sort-mode",
        action: "turbo:morph@window->account-sort-mode#render"
      } : {} do %>
  <% if family.missing_data_provider? %>
    <div class="mb-3">
      <%= render DS::Alert.new(title: t("accounts.sidebar.missing_data"), variant: :warning) do %>
        <p class="text-secondary"><%= t("accounts.sidebar.missing_data_description", product: product_name) %></p>
        <p><%= link_to t("accounts.sidebar.configure_providers"), settings_hosting_path, class: "underline font-medium" %></p>
      <% end %>
    </div>
  <% end %>

  <%= render DS::Tabs.new(active_tab: active_tab, session_key: "account_sidebar_tab", testid: "account-sidebar-tabs") do |tabs| %>
    <% tabs.with_nav do |nav| %>
      <% nav.with_btn(id: "all", label: t("accounts.sidebar.tabs.all")) %>
      <% nav.with_btn(id: "asset", label: t("accounts.sidebar.tabs.assets")) %>
      <% nav.with_btn(id: "liability", label: t("accounts.sidebar.tabs.debts")) %>
    <% end %>

    <% tabs.with_panel(tab_id: "asset") do %>
      <div class="space-y-2">
        <div class="flex items-center gap-2">
          <%= render DS::Link.new(
            text: t("accounts.sidebar.new_asset"),
            variant: "ghost",
            href: new_account_path(step: "method_select", classification: "asset"),
            icon: "plus",
            frame: :modal,
            class: "flex-1 justify-start"
          ) %>

          <%= render "accounts/sort_mode_toggle", sortable: sortable && family.balance_sheet.assets.account_groups.any? %>

          <%= render DS::ExpandToggle.new(
            expand_label: t("accounts.sidebar.expand_all"),
            collapse_label: t("accounts.sidebar.collapse_all"),
            data: { account_sidebar_expand_all: true },
            class_name: "shrink-0"
          ) %>
        </div>

        <%= render "accounts/sort_mode_bar", sortable: sortable && family.balance_sheet.assets.account_groups.any? %>

        <div>
          <% family.balance_sheet.assets.account_groups.each do |group| %>
            <%= render "accounts/accountable_group", account_group: group, mobile: mobile, sortable: sortable %>
          <% end %>
        </div>
      </div>
    <% end %>

    <% tabs.with_panel(tab_id: "liability") do %>
      <div class="space-y-2">
        <div class="flex items-center gap-2">
          <%= render DS::Link.new(
              text: t("accounts.sidebar.new_debt"),
              variant: "ghost",
              href: new_account_path(step: "method_select", classification: "liability"),
              icon: "plus",
              frame: :modal,
              class: "flex-1 justify-start"
            ) %>

          <%= render "accounts/sort_mode_toggle", sortable: sortable && family.balance_sheet.liabilities.account_groups.any? %>

          <%= render DS::ExpandToggle.new(
            expand_label: t("accounts.sidebar.expand_all"),
            collapse_label: t("accounts.sidebar.collapse_all"),
            data: { account_sidebar_expand_all: true },
            class_name: "shrink-0"
          ) %>
        </div>

        <%= render "accounts/sort_mode_bar", sortable: sortable && family.balance_sheet.liabilities.account_groups.any? %>

        <div>
          <% family.balance_sheet.liabilities.account_groups.each do |group| %>
            <%= render "accounts/accountable_group", account_group: group, mobile: mobile, sortable: sortable %>
          <% end %>
        </div>
      </div>
    <% end %>

    <% tabs.with_panel(tab_id: "all") do %>
      <div class="space-y-2">
        <div class="flex items-center gap-2">
          <%= render DS::Link.new(
              text: t("accounts.sidebar.new_account"),
              variant: "ghost",
              href: new_account_path(step: "method_select"),
              icon: "plus",
              frame: :modal,
              class: "flex-1 justify-start"
            ) %>

          <%= render "accounts/sort_mode_toggle", sortable: sortable && family.balance_sheet.account_groups.any? %>

          <%= render DS::ExpandToggle.new(
            expand_label: t("accounts.sidebar.expand_all"),
            collapse_label: t("accounts.sidebar.collapse_all"),
            data: { account_sidebar_expand_all: true },
            class_name: "shrink-0"
          ) %>
        </div>

        <%= render "accounts/sort_mode_bar", sortable: sortable && family.balance_sheet.account_groups.any? %>

        <div>
          <% family.balance_sheet.account_groups.each do |group| %>
            <%= render "accounts/accountable_group", account_group: group, mobile: mobile, all_tab: true, sortable: sortable %>
          <% end %>
        </div>
      </div>
    <% end %>
  <% end %>
<% end %>
<% end %>
<%# Outside the cache block: the nonce is per session, so a cached script
    would carry another session's nonce and the CSP would block it. %>
<script nonce="<%= content_security_policy_nonce %>">
  // Expand/Collapse all for the account sidebar. Event-delegated on document
  // so it works for both the desktop and mobile sidebar instances and survives
  // Turbo re-renders. Includes a nonce for CSP compliance
  // (matches the dashboard balance sheet).
  (function () {
    function setLabel(btn, expanded) {
      btn.setAttribute("aria-expanded", expanded ? "true" : "false");
      btn.setAttribute("aria-label", expanded ? btn.dataset.collapseLabel : btn.dataset.expandLabel);
      // Swap the visible icon via inline display (not a Tailwind
      // group-aria-expanded: variant, which is absent from the precompiled
      // bundle shipped in the Docker image).
      var expandIcon = btn.querySelector('[data-icon="expand"]');
      var collapseIcon = btn.querySelector('[data-icon="collapse"]');
      if (expandIcon) expandIcon.style.display = expanded ? "none" : "";
      if (collapseIcon) collapseIcon.style.display = expanded ? "" : "none";
    }

    // A mixed open/closed panel shows "Expand all" (a click expands the closed
    // ones); only an all-open panel shows "Collapse all".
    function syncPanel(panel) {
      var btn = panel.querySelector("[data-account-sidebar-expand-all]");
      if (!btn) return;
      var details = Array.prototype.slice.call(panel.querySelectorAll("details"));
      // Hide the control when the panel has no groups (e.g. assets but no
      // debts) so it never renders as a dead button. Matches the dashboard.
      btn.style.display = details.length ? "" : "none";
      if (!details.length) return;
      setLabel(btn, details.every(function (d) { return d.open; }));
    }

    // One-time event binding (document delegation survives Turbo re-renders
    // and covers both the desktop and mobile sidebar instances).
    if (!window.__sidebarExpandAllBound) {
      window.__sidebarExpandAllBound = true;

      document.addEventListener("click", function (e) {
        var btn = e.target.closest("[data-account-sidebar-expand-all]");
        if (!btn) return;
        var panel = btn.closest("[role=tabpanel]");
        if (!panel) return;
        var details = Array.prototype.slice.call(panel.querySelectorAll("details"));
        if (!details.length) return;
        var anyClosed = details.some(function (d) { return !d.open; });
        details.forEach(function (d) { d.open = anyClosed; });
        setLabel(btn, anyClosed);
      });

      // Keep the label in sync when a group is opened/closed manually.
      // `toggle` does not bubble, so listen in the capture phase.
      document.addEventListener("toggle", function (e) {
        var panel = e.target.closest("[role=tabpanel]");
        if (panel) syncPanel(panel);
      }, true);
    }

    // Per-render label sync (idempotent): runs on every render, including after
    // Turbo navigations, so a freshly rendered sidebar reflects its groups'
    // current open/closed state.
    Array.prototype.slice.call(document.querySelectorAll("[role=tabpanel]")).forEach(function (panel) {
      syncPanel(panel);
    });
  })();
</script>