The personal finance app for everyone (by everyone)
Ruby
Initial implementation of collapsible sections and re-order feature (#355)
soky srmdb8353e89596
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rbindex eaeb0a8b69..210dff919d 100644--- a/app/controllers/pages_controller.rb+++ b/app/controllers/pages_controller.rb@@ -16,9 +16,19 @@ class PagesController < ApplicationController@cashflow_sankey_data = build_cashflow_sankey_data(income_totals, expense_totals, family_currency)@outflows_data = build_outflows_donut_data(expense_totals)+ @dashboard_sections = build_dashboard_sections+@breadcrumbs = [ [ "Home", root_path ], [ "Dashboard", nil ] ]end+ def update_preferences+ if Current.user.update_dashboard_preferences(preferences_params)+ head :ok+ else+ head :unprocessable_entity+ end+ end+def changelog@release_notes = github_provider.fetch_latest_release_notes@@ -45,6 +55,64 @@ class PagesController < ApplicationControllerendprivate+ def preferences_params+ prefs = params.require(:preferences)+ {}.tap do |permitted|+ permitted["collapsed_sections"] = prefs[:collapsed_sections].to_unsafe_h if prefs[:collapsed_sections]+ permitted["section_order"] = prefs[:section_order] if prefs[:section_order]+ end+ end++ def build_dashboard_sections+ all_sections = [+ {+ key: "cashflow_sankey",+ title: "pages.dashboard.cashflow_sankey.title",+ partial: "pages/dashboard/cashflow_sankey",+ locals: { sankey_data: @cashflow_sankey_data, period: @period },+ visible: Current.family.accounts.any?,+ collapsible: true+ },+ {+ key: "outflows_donut",+ title: "pages.dashboard.outflows_donut.title",+ partial: "pages/dashboard/outflows_donut",+ locals: { outflows_data: @outflows_data, period: @period },+ visible: Current.family.accounts.any? && @outflows_data[:categories].present?,+ collapsible: true+ },+ {+ key: "net_worth_chart",+ title: "pages.dashboard.net_worth_chart.title",+ partial: "pages/dashboard/net_worth_chart",+ locals: { balance_sheet: @balance_sheet, period: @period },+ visible: Current.family.accounts.any?,+ collapsible: true+ },+ {+ key: "balance_sheet",+ title: "pages.dashboard.balance_sheet.title",+ partial: "pages/dashboard/balance_sheet",+ locals: { balance_sheet: @balance_sheet },+ visible: Current.family.accounts.any?,+ collapsible: true+ }+ ]++ # Order sections according to user preference+ section_order = Current.user.dashboard_section_order+ ordered_sections = section_order.map do |key|+ all_sections.find { |s| s[:key] == key }+ end.compact++ # Add any new sections that aren't in the saved order (future-proofing)+ all_sections.each do |section|+ ordered_sections << section unless ordered_sections.include?(section)+ end++ ordered_sections+ end+def github_providerProvider::Registry.get_provider(:github)enddiff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rbindex 2365dd8ebe..4addfb06eb 100644--- a/app/controllers/reports_controller.rb+++ b/app/controllers/reports_controller.rb@@ -37,9 +37,20 @@ class ReportsController < ApplicationController# Transactions breakdown@transactions = build_transactions_breakdown+ # Build reports sections for collapsible/reorderable UI+ @reports_sections = build_reports_sections+@breadcrumbs = [ [ "Home", root_path ], [ "Reports", nil ] ]end+ def update_preferences+ if Current.user.update_reports_preferences(preferences_params)+ head :ok+ else+ head :unprocessable_entity+ end+ end+def export_transactions@period_type = params[:period_type]&.to_sym || :monthly@start_date = parse_date_param(:start_date) || default_start_date@@ -100,6 +111,52 @@ class ReportsController < ApplicationControllerendprivate+ def preferences_params+ prefs = params.require(:preferences)+ {}.tap do |permitted|+ permitted["reports_collapsed_sections"] = prefs[:reports_collapsed_sections].to_unsafe_h if prefs[:reports_collapsed_sections]+ permitted["reports_section_order"] = prefs[:reports_section_order] if prefs[:reports_section_order]+ end+ end++ def build_reports_sections+ all_sections = [+ {+ key: "trends_insights",+ title: "reports.trends.title",+ partial: "reports/trends_insights",+ locals: { trends_data: @trends_data, spending_patterns: @spending_patterns },+ visible: Current.family.transactions.any?,+ collapsible: true+ },+ {+ key: "transactions_breakdown",+ title: "reports.transactions_breakdown.title",+ partial: "reports/transactions_breakdown",+ locals: {+ transactions: @transactions,+ period_type: @period_type,+ start_date: @start_date,+ end_date: @end_date+ },+ visible: Current.family.transactions.any?,+ collapsible: true+ }+ ]++ # Order sections according to user preference+ section_order = Current.user.reports_section_order+ ordered_sections = section_order.map do |key|+ all_sections.find { |s| s[:key] == key }+ end.compact++ # Add any new sections that aren't in the saved order (future-proofing)+ all_sections.each do |section|+ ordered_sections << section unless ordered_sections.include?(section)+ end++ ordered_sections+ enddef validate_and_fix_date_range(show_flash: false)return unless @start_date > @end_datediff --git a/app/javascript/controllers/dashboard_section_controller.js b/app/javascript/controllers/dashboard_section_controller.jsnew file mode 100644index 0000000000..6c7067ebe6--- /dev/null+++ b/app/javascript/controllers/dashboard_section_controller.js@@ -0,0 +1,97 @@+import { Controller } from "@hotwired/stimulus";++export default class extends Controller {+ static targets = ["content", "chevron", "container", "button"];+ static values = {+ sectionKey: String,+ collapsed: Boolean,+ };++ connect() {+ if (this.collapsedValue) {+ this.collapse(false);+ }+ }++ toggle(event) {+ event.preventDefault();+ if (this.collapsedValue) {+ this.expand();+ } else {+ this.collapse();+ }+ }++ handleToggleKeydown(event) {+ // Handle Enter and Space keys for keyboard accessibility+ if (event.key === "Enter" || event.key === " ") {+ event.preventDefault();+ event.stopPropagation(); // Prevent section's keyboard handler from firing+ this.toggle(event);+ }+ }++ collapse(persist = true) {+ this.contentTarget.classList.add("hidden");+ this.chevronTarget.classList.add("rotate-180");+ this.collapsedValue = true;+ if (this.hasButtonTarget) {+ this.buttonTarget.setAttribute("aria-expanded", "false");+ }+ if (persist) {+ this.savePreference(true);+ }+ }++ expand() {+ this.contentTarget.classList.remove("hidden");+ this.chevronTarget.classList.remove("rotate-180");+ this.collapsedValue = false;+ if (this.hasButtonTarget) {+ this.buttonTarget.setAttribute("aria-expanded", "true");+ }+ this.savePreference(false);+ }++ async savePreference(collapsed) {+ const preferences = {+ collapsed_sections: {+ [this.sectionKeyValue]: collapsed,+ },+ };++ // Safely obtain CSRF token+ const csrfToken = document.querySelector('meta[name="csrf-token"]');+ if (!csrfToken) {+ console.error(+ "[Dashboard Section] CSRF token not found. Cannot save preferences.",+ );+ return;+ }++ try {+ const response = await fetch("/dashboard/preferences", {+ method: "PATCH",+ headers: {+ "Content-Type": "application/json",+ "X-CSRF-Token": csrfToken.content,+ },+ body: JSON.stringify({ preferences }),+ });++ if (!response.ok) {+ const errorData = await response.json().catch(() => ({}));+ console.error(+ "[Dashboard Section] Failed to save preferences:",+ response.status,+ errorData,+ );+ }+ } catch (error) {+ console.error(+ "[Dashboard Section] Network error saving preferences:",+ error,+ );+ }+ }+}diff --git a/app/javascript/controllers/dashboard_sortable_controller.js b/app/javascript/controllers/dashboard_sortable_controller.jsnew file mode 100644index 0000000000..d44d47c921--- /dev/null+++ b/app/javascript/controllers/dashboard_sortable_controller.js@@ -0,0 +1,267 @@+import { Controller } from "@hotwired/stimulus";++export default class extends Controller {+ static targets = ["section"];++ connect() {+ this.draggedElement = null;+ this.placeholder = null;+ this.touchStartY = 0;+ this.currentTouchY = 0;+ this.isTouching = false;+ this.keyboardGrabbedElement = null;+ }++ // ===== Mouse Drag Events =====+ dragStart(event) {+ this.draggedElement = event.currentTarget;+ this.draggedElement.classList.add("opacity-50");+ this.draggedElement.setAttribute("aria-grabbed", "true");+ event.dataTransfer.effectAllowed = "move";+ }++ dragEnd(event) {+ event.currentTarget.classList.remove("opacity-50");+ event.currentTarget.setAttribute("aria-grabbed", "false");+ this.clearPlaceholders();+ }++ dragOver(event) {+ event.preventDefault();+ event.dataTransfer.dropEffect = "move";++ const afterElement = this.getDragAfterElement(event.clientY);+ const container = this.element;++ this.clearPlaceholders();++ if (afterElement == null) {+ this.showPlaceholder(container.lastElementChild, "after");+ } else {+ this.showPlaceholder(afterElement, "before");+ }+ }++ drop(event) {+ event.preventDefault();+ event.stopPropagation();++ const afterElement = this.getDragAfterElement(event.clientY);+ const container = this.element;++ if (afterElement == null) {+ container.appendChild(this.draggedElement);+ } else {+ container.insertBefore(this.draggedElement, afterElement);+ }++ this.clearPlaceholders();+ this.saveOrder();+ }++ // ===== Touch Events =====+ touchStart(event) {+ this.draggedElement = event.currentTarget;+ this.touchStartY = event.touches[0].clientY;+ this.isTouching = true;+ this.draggedElement.classList.add("opacity-50", "scale-105");+ this.draggedElement.setAttribute("aria-grabbed", "true");+ }++ touchMove(event) {+ if (!this.isTouching || !this.draggedElement) return;++ event.preventDefault();+ this.currentTouchY = event.touches[0].clientY;++ const afterElement = this.getDragAfterElement(this.currentTouchY);+ this.clearPlaceholders();++ if (afterElement == null) {+ this.showPlaceholder(this.element.lastElementChild, "after");+ } else {+ this.showPlaceholder(afterElement, "before");+ }+ }++ touchEnd(event) {+ if (!this.isTouching || !this.draggedElement) return;++ const afterElement = this.getDragAfterElement(this.currentTouchY);+ const container = this.element;++ if (afterElement == null) {+ container.appendChild(this.draggedElement);+ } else {+ container.insertBefore(this.draggedElement, afterElement);+ }++ this.draggedElement.classList.remove("opacity-50", "scale-105");+ this.draggedElement.setAttribute("aria-grabbed", "false");+ this.clearPlaceholders();+ this.saveOrder();++ this.isTouching = false;+ this.draggedElement = null;+ }++ // ===== Keyboard Navigation =====+ handleKeyDown(event) {+ const currentSection = event.currentTarget;++ switch (event.key) {+ case "ArrowUp":+ event.preventDefault();+ if (this.keyboardGrabbedElement === currentSection) {+ this.moveUp(currentSection);+ }+ break;+ case "ArrowDown":+ event.preventDefault();+ if (this.keyboardGrabbedElement === currentSection) {+ this.moveDown(currentSection);+ }+ break;+ case "Enter":+ case " ":+ event.preventDefault();+ this.toggleGrabMode(currentSection);+ break;+ case "Escape":+ if (this.keyboardGrabbedElement) {+ event.preventDefault();+ this.releaseKeyboardGrab();+ }+ break;+ }+ }++ toggleGrabMode(section) {+ if (this.keyboardGrabbedElement === section) {+ this.releaseKeyboardGrab();+ } else {+ this.grabWithKeyboard(section);+ }+ }++ grabWithKeyboard(section) {+ // Release any previously grabbed element+ if (this.keyboardGrabbedElement) {+ this.releaseKeyboardGrab();+ }++ this.keyboardGrabbedElement = section;+ section.setAttribute("aria-grabbed", "true");+ section.classList.add("ring-2", "ring-primary", "ring-offset-2");+ }++ releaseKeyboardGrab() {+ if (this.keyboardGrabbedElement) {+ this.keyboardGrabbedElement.setAttribute("aria-grabbed", "false");+ this.keyboardGrabbedElement.classList.remove(+ "ring-2",+ "ring-primary",+ "ring-offset-2",+ );+ this.keyboardGrabbedElement = null;+ this.saveOrder();+ }+ }++ moveUp(section) {+ const previousSibling = section.previousElementSibling;+ if (previousSibling?.hasAttribute("data-section-key")) {+ this.element.insertBefore(section, previousSibling);+ section.focus();+ }+ }++ moveDown(section) {+ const nextSibling = section.nextElementSibling;+ if (nextSibling?.hasAttribute("data-section-key")) {+ this.element.insertBefore(nextSibling, section);+ section.focus();+ }+ }++ getDragAfterElement(y) {+ const draggableElements = [+ ...this.sectionTargets.filter((section) => section !== this.draggedElement),+ ];++ return draggableElements.reduce(+ (closest, child) => {+ const box = child.getBoundingClientRect();+ const offset = y - box.top - box.height / 2;++ if (offset < 0 && offset > closest.offset) {+ return { offset: offset, element: child };+ }+ return closest;+ },+ { offset: Number.NEGATIVE_INFINITY },+ ).element;+ }++ showPlaceholder(element, position) {+ if (!element) return;++ if (position === "before") {+ element.classList.add("border-t-4", "border-primary");+ } else {+ element.classList.add("border-b-4", "border-primary");+ }+ }++ clearPlaceholders() {+ this.sectionTargets.forEach((section) => {+ section.classList.remove(+ "border-t-4",+ "border-b-4",+ "border-primary",+ "border-t-2",+ "border-b-2",+ );+ });+ }++ async saveOrder() {+ const order = this.sectionTargets.map(+ (section) => section.dataset.sectionKey,+ );++ // Safely obtain CSRF token+ const csrfToken = document.querySelector('meta[name="csrf-token"]');+ if (!csrfToken) {+ console.error(+ "[Dashboard Sortable] CSRF token not found. Cannot save section order.",+ );+ return;+ }++ try {+ const response = await fetch("/dashboard/preferences", {+ method: "PATCH",+ headers: {+ "Content-Type": "application/json",+ "X-CSRF-Token": csrfToken.content,+ },+ body: JSON.stringify({ preferences: { section_order: order } }),+ });++ if (!response.ok) {+ const errorData = await response.json().catch(() => ({}));+ console.error(+ "[Dashboard Sortable] Failed to save section order:",+ response.status,+ errorData,+ );+ }+ } catch (error) {+ console.error(+ "[Dashboard Sortable] Network error saving section order:",+ error,+ );+ }+ }+}diff --git a/app/javascript/controllers/reports_section_controller.js b/app/javascript/controllers/reports_section_controller.jsnew file mode 100644index 0000000000..6ed08a8b7f--- /dev/null+++ b/app/javascript/controllers/reports_section_controller.js@@ -0,0 +1,97 @@+import { Controller } from "@hotwired/stimulus";++export default class extends Controller {+ static targets = ["content", "chevron", "button"];+ static values = {+ sectionKey: String,+ collapsed: Boolean,+ };++ connect() {+ if (this.collapsedValue) {+ this.collapse(false);+ }+ }++ toggle(event) {+ event.preventDefault();+ if (this.collapsedValue) {+ this.expand();+ } else {+ this.collapse();+ }+ }++ handleToggleKeydown(event) {+ // Handle Enter and Space keys for keyboard accessibility+ if (event.key === "Enter" || event.key === " ") {+ event.preventDefault();+ event.stopPropagation(); // Prevent section's keyboard handler from firing+ this.toggle(event);+ }+ }++ collapse(persist = true) {+ this.contentTarget.classList.add("hidden");+ this.chevronTarget.classList.add("rotate-180");+ this.collapsedValue = true;+ if (this.hasButtonTarget) {+ this.buttonTarget.setAttribute("aria-expanded", "false");+ }+ if (persist) {+ this.savePreference(true);+ }+ }++ expand() {+ this.contentTarget.classList.remove("hidden");+ this.chevronTarget.classList.remove("rotate-180");+ this.collapsedValue = false;+ if (this.hasButtonTarget) {+ this.buttonTarget.setAttribute("aria-expanded", "true");+ }+ this.savePreference(false);+ }++ async savePreference(collapsed) {+ const preferences = {+ reports_collapsed_sections: {+ [this.sectionKeyValue]: collapsed,+ },+ };++ // Safely obtain CSRF token+ const csrfToken = document.querySelector('meta[name="csrf-token"]');+ if (!csrfToken) {+ console.error(+ "[Reports Section] CSRF token not found. Cannot save preferences.",+ );+ return;+ }++ try {+ const response = await fetch("/reports/update_preferences", {+ method: "PATCH",+ headers: {+ "Content-Type": "application/json",+ "X-CSRF-Token": csrfToken.content,+ },+ body: JSON.stringify({ preferences }),+ });++ if (!response.ok) {+ const errorData = await response.json().catch(() => ({}));+ console.error(+ "[Reports Section] Failed to save preferences:",+ response.status,+ errorData,+ );+ }+ } catch (error) {+ console.error(+ "[Reports Section] Network error saving preferences:",+ error,+ );+ }+ }+}diff --git a/app/javascript/controllers/reports_sortable_controller.js b/app/javascript/controllers/reports_sortable_controller.jsnew file mode 100644index 0000000000..82ddcc2f10--- /dev/null+++ b/app/javascript/controllers/reports_sortable_controller.js@@ -0,0 +1,267 @@+import { Controller } from "@hotwired/stimulus";++export default class extends Controller {+ static targets = ["section"];++ connect() {+ this.draggedElement = null;+ this.placeholder = null;+ this.touchStartY = 0;+ this.currentTouchY = 0;+ this.isTouching = false;+ this.keyboardGrabbedElement = null;+ }++ // ===== Mouse Drag Events =====+ dragStart(event) {+ this.draggedElement = event.currentTarget;+ this.draggedElement.classList.add("opacity-50");+ this.draggedElement.setAttribute("aria-grabbed", "true");+ event.dataTransfer.effectAllowed = "move";+ }++ dragEnd(event) {+ event.currentTarget.classList.remove("opacity-50");+ event.currentTarget.setAttribute("aria-grabbed", "false");+ this.clearPlaceholders();+ }++ dragOver(event) {+ event.preventDefault();+ event.dataTransfer.dropEffect = "move";++ const afterElement = this.getDragAfterElement(event.clientY);+ const container = this.element;++ this.clearPlaceholders();++ if (afterElement == null) {+ this.showPlaceholder(container.lastElementChild, "after");+ } else {+ this.showPlaceholder(afterElement, "before");+ }+ }++ drop(event) {+ event.preventDefault();+ event.stopPropagation();++ const afterElement = this.getDragAfterElement(event.clientY);+ const container = this.element;++ if (afterElement == null) {+ container.appendChild(this.draggedElement);+ } else {+ container.insertBefore(this.draggedElement, afterElement);+ }++ this.clearPlaceholders();+ this.saveOrder();+ }++ // ===== Touch Events =====+ touchStart(event) {+ this.draggedElement = event.currentTarget;+ this.touchStartY = event.touches[0].clientY;+ this.isTouching = true;+ this.draggedElement.classList.add("opacity-50", "scale-105");+ this.draggedElement.setAttribute("aria-grabbed", "true");+ }++ touchMove(event) {+ if (!this.isTouching || !this.draggedElement) return;++ event.preventDefault();+ this.currentTouchY = event.touches[0].clientY;++ const afterElement = this.getDragAfterElement(this.currentTouchY);+ this.clearPlaceholders();++ if (afterElement == null) {+ this.showPlaceholder(this.element.lastElementChild, "after");+ } else {+ this.showPlaceholder(afterElement, "before");+ }+ }++ touchEnd(event) {+ if (!this.isTouching || !this.draggedElement) return;++ const afterElement = this.getDragAfterElement(this.currentTouchY);+ const container = this.element;++ if (afterElement == null) {+ container.appendChild(this.draggedElement);+ } else {+ container.insertBefore(this.draggedElement, afterElement);+ }++ this.draggedElement.classList.remove("opacity-50", "scale-105");+ this.draggedElement.setAttribute("aria-grabbed", "false");+ this.clearPlaceholders();+ this.saveOrder();++ this.isTouching = false;+ this.draggedElement = null;+ }++ // ===== Keyboard Navigation =====+ handleKeyDown(event) {+ const currentSection = event.currentTarget;++ switch (event.key) {+ case "ArrowUp":+ event.preventDefault();+ if (this.keyboardGrabbedElement === currentSection) {+ this.moveUp(currentSection);+ }+ break;+ case "ArrowDown":+ event.preventDefault();+ if (this.keyboardGrabbedElement === currentSection) {+ this.moveDown(currentSection);+ }+ break;+ case "Enter":+ case " ":+ event.preventDefault();+ this.toggleGrabMode(currentSection);+ break;+ case "Escape":+ if (this.keyboardGrabbedElement) {+ event.preventDefault();+ this.releaseKeyboardGrab();+ }+ break;+ }+ }++ toggleGrabMode(section) {+ if (this.keyboardGrabbedElement === section) {+ this.releaseKeyboardGrab();+ } else {+ this.grabWithKeyboard(section);+ }+ }++ grabWithKeyboard(section) {+ // Release any previously grabbed element+ if (this.keyboardGrabbedElement) {+ this.releaseKeyboardGrab();+ }++ this.keyboardGrabbedElement = section;+ section.setAttribute("aria-grabbed", "true");+ section.classList.add("ring-2", "ring-primary", "ring-offset-2");+ }++ releaseKeyboardGrab() {+ if (this.keyboardGrabbedElement) {+ this.keyboardGrabbedElement.setAttribute("aria-grabbed", "false");+ this.keyboardGrabbedElement.classList.remove(+ "ring-2",+ "ring-primary",+ "ring-offset-2",+ );+ this.keyboardGrabbedElement = null;+ this.saveOrder();+ }+ }++ moveUp(section) {+ const previousSibling = section.previousElementSibling;+ if (previousSibling?.hasAttribute("data-section-key")) {+ this.element.insertBefore(section, previousSibling);+ section.focus();+ }+ }++ moveDown(section) {+ const nextSibling = section.nextElementSibling;+ if (nextSibling?.hasAttribute("data-section-key")) {+ this.element.insertBefore(nextSibling, section);+ section.focus();+ }+ }++ getDragAfterElement(y) {+ const draggableElements = [+ ...this.sectionTargets.filter((section) => section !== this.draggedElement),+ ];++ return draggableElements.reduce(+ (closest, child) => {+ const box = child.getBoundingClientRect();+ const offset = y - box.top - box.height / 2;++ if (offset < 0 && offset > closest.offset) {+ return { offset: offset, element: child };+ }+ return closest;+ },+ { offset: Number.NEGATIVE_INFINITY },+ ).element;+ }++ showPlaceholder(element, position) {+ if (!element) return;++ if (position === "before") {+ element.classList.add("border-t-4", "border-primary");+ } else {+ element.classList.add("border-b-4", "border-primary");+ }+ }++ clearPlaceholders() {+ this.sectionTargets.forEach((section) => {+ section.classList.remove(+ "border-t-4",+ "border-b-4",+ "border-primary",+ "border-t-2",+ "border-b-2",+ );+ });+ }++ async saveOrder() {+ const order = this.sectionTargets.map(+ (section) => section.dataset.sectionKey,+ );++ // Safely obtain CSRF token+ const csrfToken = document.querySelector('meta[name="csrf-token"]');+ if (!csrfToken) {+ console.error(+ "[Reports Sortable] CSRF token not found. Cannot save section order.",+ );+ return;+ }++ try {+ const response = await fetch("/reports/update_preferences", {+ method: "PATCH",+ headers: {+ "Content-Type": "application/json",+ "X-CSRF-Token": csrfToken.content,+ },+ body: JSON.stringify({ preferences: { reports_section_order: order } }),+ });++ if (!response.ok) {+ const errorData = await response.json().catch(() => ({}));+ console.error(+ "[Reports Sortable] Failed to save section order:",+ response.status,+ errorData,+ );+ }+ } catch (error) {+ console.error(+ "[Reports Sortable] Network error saving section order:",+ error,+ );+ }+ }+}diff --git a/app/javascript/controllers/time_series_chart_controller.js b/app/javascript/controllers/time_series_chart_controller.jsindex 7c1a1a064c..981e227b92 100644--- a/app/javascript/controllers/time_series_chart_controller.js+++ b/app/javascript/controllers/time_series_chart_controller.js@@ -66,6 +66,18 @@ export default class extends Controller {}_draw() {+ // Guard against invalid dimensions (e.g., when container is collapsed or not yet rendered)+ const minWidth = 50;+ const minHeight = 50;++ if (+ this._d3ContainerWidth < minWidth ||+ this._d3ContainerHeight < minHeight+ ) {+ // Skip rendering if dimensions are invalid+ return;+ }+if (this._normalDataPoints.length < 2) {this._drawEmpty();} else {diff --git a/app/models/user.rb b/app/models/user.rbindex b62ab8b612..57e57ac043 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -177,7 +177,71 @@ class User < ApplicationRecordAccountOrder.find(default_account_order) || AccountOrder.defaultend+ # Dashboard preferences management+ def dashboard_section_collapsed?(section_key)+ preferences&.dig("collapsed_sections", section_key) == true+ end++ def dashboard_section_order+ preferences&.[]("section_order") || default_dashboard_section_order+ end++ def update_dashboard_preferences(prefs)+ # Use pessimistic locking to ensure atomic read-modify-write+ # This prevents race conditions when multiple sections are collapsed quickly+ transaction do+ lock! # Acquire row-level lock (SELECT FOR UPDATE)++ updated_prefs = (preferences || {}).deep_dup+ prefs.each do |key, value|+ if value.is_a?(Hash)+ updated_prefs[key] ||= {}+ updated_prefs[key] = updated_prefs[key].merge(value)+ else+ updated_prefs[key] = value+ end+ end++ update!(preferences: updated_prefs)+ end+ end++ # Reports preferences management+ def reports_section_collapsed?(section_key)+ preferences&.dig("reports_collapsed_sections", section_key) == true+ end++ def reports_section_order+ preferences&.[]("reports_section_order") || default_reports_section_order+ end++ def update_reports_preferences(prefs)+ # Use pessimistic locking to ensure atomic read-modify-write+ transaction do+ lock!++ updated_prefs = (preferences || {}).deep_dup+ prefs.each do |key, value|+ if value.is_a?(Hash)+ updated_prefs[key] ||= {}+ updated_prefs[key] = updated_prefs[key].merge(value)+ else+ updated_prefs[key] = value+ end+ end++ update!(preferences: updated_prefs)+ end+ end+private+ def default_dashboard_section_order+ %w[cashflow_sankey outflows_donut net_worth_chart balance_sheet]+ end++ def default_reports_section_order+ %w[trends_insights transactions_breakdown]+ enddef ensure_valid_profile_imagereturn unless profile_image.attached?diff --git a/app/views/pages/dashboard.html.erb b/app/views/pages/dashboard.html.erbindex 34f3796cda..daf6454b45 100644--- a/app/views/pages/dashboard.html.erb+++ b/app/views/pages/dashboard.html.erb@@ -27,34 +27,56 @@</div><% end %>-<div class="w-full space-y-6 pb-24">+<div class="w-full space-y-6 pb-24" data-controller="dashboard-sortable" data-action="dragover->dashboard-sortable#dragOver drop->dashboard-sortable#drop" role="list" aria-label="Dashboard sections"><% if Current.family.accounts.any? %>- <section class="bg-container py-4 rounded-xl shadow-border-xs mb-6">- <%= render partial: "pages/dashboard/cashflow_sankey", locals: {- sankey_data: @cashflow_sankey_data,- period: @period- } %>- </section>-- <% if @outflows_data[:categories].present? %>- <section class="bg-container py-4 rounded-xl shadow-border-xs mb-6">- <%= render partial: "pages/dashboard/outflows_donut", locals: {- outflows_data: @outflows_data,- period: @period- } %>+ <% @dashboard_sections.each do |section| %>+ <% next unless section[:visible] %>+ <section+ class="bg-container rounded-xl shadow-border-xs transition-all group focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2"+ data-dashboard-sortable-target="section"+ data-section-key="<%= section[:key] %>"+ data-controller="dashboard-section"+ data-dashboard-section-section-key-value="<%= section[:key] %>"+ data-dashboard-section-collapsed-value="<%= Current.user.dashboard_section_collapsed?(section[:key]) %>"+ draggable="true"+ tabindex="0"+ role="listitem"+ aria-grabbed="false"+ aria-label="<%= t(section[:title]) %> section. Press Enter or Space to grab for reordering, then use arrow keys to move."+ data-action="+ dragstart->dashboard-sortable#dragStart+ dragend->dashboard-sortable#dragEnd+ touchstart->dashboard-sortable#touchStart+ touchmove->dashboard-sortable#touchMove+ touchend->dashboard-sortable#touchEnd+ keydown->dashboard-sortable#handleKeyDown">+ <div class="px-4 py-2 flex items-center justify-between">+ <div class="flex items-center gap-2">+ <button+ type="button"+ class="text-secondary hover:text-primary transition-colors p-0.5"+ data-action="click->dashboard-section#toggle keydown->dashboard-section#handleToggleKeydown"+ data-dashboard-section-target="button"+ aria-label="<%= t("pages.dashboard.toggle_section") %>"+ aria-expanded="<%= !Current.user.dashboard_section_collapsed?(section[:key]) %>">+ <%= icon("chevron-down", size: "sm", class: "transition-transform", data: { dashboard_section_target: "chevron" }) %>+ </button>+ <h2 class="text-base font-medium">+ <%= t(section[:title]) %>+ </h2>+ </div>+ <button+ type="button"+ class="cursor-grab active:cursor-grabbing text-secondary hover:text-primary transition-colors p-0.5 opacity-0 group-hover:opacity-100"+ aria-label="<%= t("pages.dashboard.drag_to_reorder") %>">+ <%= icon("grip-vertical", size: "sm") %>+ </button>+ </div>+ <div class="py-4" data-dashboard-section-target="content">+ <%= render partial: section[:partial], locals: section[:locals] %>+ </div></section><% end %>-- <section class="bg-container py-4 rounded-xl shadow-border-xs">- <%= render partial: "pages/dashboard/net_worth_chart", locals: {- balance_sheet: @balance_sheet,- period: @period- } %>- </section>-- <section>- <%= render "pages/dashboard/balance_sheet", balance_sheet: @balance_sheet %>- </section><% else %><section><%= render "pages/dashboard/no_accounts_graph_placeholder" %>diff --git a/app/views/pages/dashboard/_balance_sheet.html.erb b/app/views/pages/dashboard/_balance_sheet.html.erbindex af9ab31652..5bacd50c50 100644--- a/app/views/pages/dashboard/_balance_sheet.html.erb+++ b/app/views/pages/dashboard/_balance_sheet.html.erb@@ -1,8 +1,8 @@<%# locals: (balance_sheet:, **args) %>-<div class="space-y-4" id="balance-sheet">+<div class="space-y-6" id="balance-sheet"><% balance_sheet.classification_groups.each do |classification_group| %>- <div class="bg-container shadow-border-xs rounded-xl space-y-4 p-4">+ <div class="space-y-4 px-4"><div class="flex items-center gap-2"><h2 class="text-lg font-medium inline-flex items-center gap-1.5"><span class="<%= "animate-pulse" if classification_group.syncing? %>">@@ -48,7 +48,7 @@</div></div>- <div class="shadow-border-xs rounded-lg bg-container font-medium text-sm min-w-fit">+ <div class="rounded-lg bg-container font-medium text-sm min-w-fit"><% classification_group.account_groups.each_with_index do |account_group, idx| %><details class="group open:bg-container<%= idx == 0 ? "rounded-t-lg" : "" %>diff --git a/app/views/pages/dashboard/_cashflow_sankey.html.erb b/app/views/pages/dashboard/_cashflow_sankey.html.erbindex bb49fa50f2..b47ca57398 100644--- a/app/views/pages/dashboard/_cashflow_sankey.html.erb+++ b/app/views/pages/dashboard/_cashflow_sankey.html.erb@@ -1,10 +1,6 @@<%# locals: (sankey_data:, period:) %><div id="cashflow-sankey-chart">- <div class="flex justify-between items-center gap-4 px-4 mb-4">- <h2 class="text-lg font-medium inline-flex items-center gap-1.5">- <%= t("pages.dashboard.cashflow_sankey.title") %>- </h2>-+ <div class="flex justify-end items-center gap-4 px-4 mb-4"><%= form_with url: root_path, method: :get, data: { controller: "auto-submit-form", turbo_frame: "_top" } do |form| %><%= form.select :period,Period.as_options,diff --git a/app/views/pages/dashboard/_net_worth_chart.html.erb b/app/views/pages/dashboard/_net_worth_chart.html.erbindex d368d3a540..2ac67416b0 100644--- a/app/views/pages/dashboard/_net_worth_chart.html.erb+++ b/app/views/pages/dashboard/_net_worth_chart.html.erb@@ -4,20 +4,14 @@<% series = balance_sheet.net_worth_series(period: period) %><div class="flex justify-between gap-4 px-4"><div class="space-y-2">- <div class="space-y-2">- <div class="flex items-center gap-2">- <h2 class="text-lg font-medium"><%= t(".title") %></h2>- </div>-- <% if series.trend.present? %>- <p class="text-primary -space-x-0.5 text-3xl font-medium <%= "animate-pulse" if balance_sheet.syncing? %>">- <%= series.trend.current.format %>- </p>- <%= render partial: "shared/trend_change", locals: { trend: series.trend, comparison_label: period.comparison_label } %>- <% else %>- <p class="text-sm text-secondary"><%= t(".data_not_available") %></p>- <% end %>- </div>+ <% if series.trend.present? %>+ <p class="text-primary -space-x-0.5 text-3xl font-medium <%= "animate-pulse" if balance_sheet.syncing? %>">+ <%= series.trend.current.format %>+ </p>+ <%= render partial: "shared/trend_change", locals: { trend: series.trend, comparison_label: period.comparison_label } %>+ <% else %>+ <p class="text-sm text-secondary"><%= t(".data_not_available") %></p>+ <% end %></div><%= form_with url: root_path, method: :get, data: { controller: "auto-submit-form", turbo_frame: "_top" } do |form| %>diff --git a/app/views/pages/dashboard/_outflows_donut.html.erb b/app/views/pages/dashboard/_outflows_donut.html.erbindex b1132cf9b1..c046a0a886 100644--- a/app/views/pages/dashboard/_outflows_donut.html.erb+++ b/app/views/pages/dashboard/_outflows_donut.html.erb@@ -1,10 +1,6 @@<%# locals: (outflows_data:, period:) %><div id="outflows-donut-section">- <div class="flex justify-between items-center gap-4 px-4 mb-4">- <h2 class="text-lg font-medium inline-flex items-center gap-1.5">- <%= t("pages.dashboard.outflows_donut.title") %>- </h2>-+ <div class="flex justify-end items-center gap-4 px-4 mb-4"><%= form_with url: root_path, method: :get, data: { controller: "auto-submit-form", turbo_frame: "_top" } do |form| %><%= form.select :period,Period.as_options,diff --git a/app/views/reports/_transactions_breakdown.html.erb b/app/views/reports/_transactions_breakdown.html.erbindex 9cb4d26d8e..fafb717e18 100644--- a/app/views/reports/_transactions_breakdown.html.erb+++ b/app/views/reports/_transactions_breakdown.html.erb@@ -1,11 +1,4 @@<div>- <%# Header %>- <div class="flex items-center justify-between mb-6">- <h2 class="text-lg font-medium text-primary">- <%= t("reports.transactions_breakdown.title") %>- </h2>- </div>-<%# Export Controls %><div class="flex items-center justify-end mb-4 flex-wrap gap-3"><%diff --git a/app/views/reports/_trends_insights.html.erb b/app/views/reports/_trends_insights.html.erbindex 84e05657d4..1b3d83b53d 100644--- a/app/views/reports/_trends_insights.html.erb+++ b/app/views/reports/_trends_insights.html.erb@@ -1,9 +1,4 @@-<div>- <h2 class="text-lg font-medium text-primary mb-6">- <%= t("reports.trends.title") %>- </h2>-- <div class="space-y-8">+<div class="space-y-8"><%# Month-over-Month Trends %><div><h3 class="text-sm font-medium text-secondary mb-4">diff --git a/app/views/reports/index.html.erb b/app/views/reports/index.html.erbindex 13fd9d1fc5..57cd109ba2 100644--- a/app/views/reports/index.html.erb+++ b/app/views/reports/index.html.erb@@ -86,7 +86,7 @@<div class="w-full space-y-6 pb-24"><% if Current.family.transactions.any? %>- <%# Summary Dashboard %>+ <%# Summary Dashboard - Always visible, not collapsible %><section><%= render partial: "reports/summary_dashboard", locals: {metrics: @summary_metrics,@@ -94,23 +94,57 @@} %></section>- <%# Trends & Insights %>- <section class="bg-container py-4 px-6 rounded-xl shadow-border-xs">- <%= render partial: "reports/trends_insights", locals: {- trends_data: @trends_data,- spending_patterns: @spending_patterns- } %>- </section>-- <%# Transactions Breakdown %>- <section class="bg-container py-4 px-6 rounded-xl shadow-border-xs">- <%= render partial: "reports/transactions_breakdown", locals: {- transactions: @transactions,- period_type: @period_type,- start_date: @start_date,- end_date: @end_date- } %>- </section>+ <%# Collapsible & Reorderable Sections %>+ <div data-controller="reports-sortable" data-action="dragover->reports-sortable#dragOver drop->reports-sortable#drop" role="list" aria-label="Reports sections">+ <% @reports_sections.each do |section| %>+ <% next unless section[:visible] %>+ <section+ class="bg-container rounded-xl shadow-border-xs transition-all mb-6 group focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2"+ data-reports-sortable-target="section"+ data-section-key="<%= section[:key] %>"+ data-controller="reports-section"+ data-reports-section-section-key-value="<%= section[:key] %>"+ data-reports-section-collapsed-value="<%= Current.user.reports_section_collapsed?(section[:key]) %>"+ draggable="true"+ tabindex="0"+ role="listitem"+ aria-grabbed="false"+ aria-label="<%= t(section[:title]) %> section. Press Enter or Space to grab for reordering, then use arrow keys to move."+ data-action="+ dragstart->reports-sortable#dragStart+ dragend->reports-sortable#dragEnd+ touchstart->reports-sortable#touchStart+ touchmove->reports-sortable#touchMove+ touchend->reports-sortable#touchEnd+ keydown->reports-sortable#handleKeyDown">+ <div class="px-4 py-2 flex items-center justify-between">+ <div class="flex items-center gap-2">+ <button+ type="button"+ class="text-secondary hover:text-primary transition-colors p-0.5"+ data-action="click->reports-section#toggle keydown->reports-section#handleToggleKeydown"+ data-reports-section-target="button"+ aria-label="<%= t("reports.index.toggle_section") %>"+ aria-expanded="<%= !Current.user.reports_section_collapsed?(section[:key]) %>">+ <%= icon("chevron-down", size: "sm", class: "transition-transform", data: { reports_section_target: "chevron" }) %>+ </button>+ <h2 class="text-base font-medium">+ <%= t(section[:title]) %>+ </h2>+ </div>+ <button+ type="button"+ class="cursor-grab active:cursor-grabbing text-secondary hover:text-primary transition-colors p-0.5 opacity-0 group-hover:opacity-100"+ aria-label="<%= t("reports.index.drag_to_reorder") %>">+ <%= icon("grip-vertical", size: "sm") %>+ </button>+ </div>+ <div class="py-4 px-6" data-reports-section-target="content">+ <%= render partial: section[:partial], locals: section[:locals] %>+ </div>+ </section>+ <% end %>+ </div><% else %><%# Empty State %><section>diff --git a/config/locales/views/pages/en.yml b/config/locales/views/pages/en.ymlindex 572b06c784..8fa38a4168 100644--- a/config/locales/views/pages/en.yml+++ b/config/locales/views/pages/en.yml@@ -7,6 +7,8 @@ en:welcome: "Welcome back, %{name}"subtitle: "Here's what's happening with your finances"new: "New"+ drag_to_reorder: "Drag to reorder section"+ toggle_section: "Toggle section visibility"net_worth_chart:data_not_available: Data not available for the selected periodtitle: Net Worth@@ -15,6 +17,7 @@ en:no_account_subtitle: Since no accounts have been added, there's no data to display. Add your first accounts to start viewing dashboard data.no_account_title: No accounts yetbalance_sheet:+ title: "Balance Sheet"no_items: "No %{name} yet"add_accounts: "Add your %{name} accounts to see a full breakdown"cashflow_sankey:diff --git a/config/locales/views/reports/en.yml b/config/locales/views/reports/en.ymlindex b09c5b97db..5b4ed7a49f 100644--- a/config/locales/views/reports/en.yml+++ b/config/locales/views/reports/en.yml@@ -5,6 +5,8 @@ en:title: Reportssubtitle: Comprehensive insights into your financial healthexport: Export CSV+ drag_to_reorder: "Drag to reorder section"+ toggle_section: "Toggle section visibility"periods:monthly: Monthlyquarterly: Quarterlydiff --git a/config/routes.rb b/config/routes.rbindex 8c82dc6068..740277b355 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -32,6 +32,7 @@ Rails.application.routes.draw doget "changelog", to: "pages#changelog"get "feedback", to: "pages#feedback"+ patch "dashboard/preferences", to: "pages#update_preferences"resource :current_session, only: %i[update]@@ -104,6 +105,7 @@ Rails.application.routes.draw doendresources :reports, only: %i[index] do+ patch :update_preferences, on: :collectionget :export_transactions, on: :collectionget :google_sheets_instructions, on: :collectionenddiff --git a/db/migrate/20251119122856_add_preferences_to_users.rb b/db/migrate/20251119122856_add_preferences_to_users.rbnew file mode 100644index 0000000000..8af204480a--- /dev/null+++ b/db/migrate/20251119122856_add_preferences_to_users.rb@@ -0,0 +1,6 @@+class AddPreferencesToUsers < ActiveRecord::Migration[7.2]+ def change+ add_column :users, :preferences, :jsonb, default: {}, null: false+ add_index :users, :preferences, using: :gin+ end+enddiff --git a/test/models/user_test.rb b/test/models/user_test.rbindex 16aa8cc72d..c8d16889d4 100644--- a/test/models/user_test.rb+++ b/test/models/user_test.rb@@ -152,4 +152,128 @@ class UserTest < ActiveSupport::TestCaseensureSetting.openai_access_token = previousend++ test "update_dashboard_preferences handles concurrent updates atomically" do+ @user.update!(preferences: {})++ # Simulate concurrent updates from multiple requests+ # Each thread collapses a different section simultaneously+ threads = []+ sections = %w[net_worth_chart outflows_donut cashflow_sankey balance_sheet]++ sections.each_with_index do |section, index|+ threads << Thread.new do+ # Small staggered delays to increase chance of race conditions+ sleep(index * 0.01)++ # Each thread loads its own instance and updates+ user = User.find(@user.id)+ user.update_dashboard_preferences({+ "collapsed_sections" => { section => true }+ })+ end+ end++ # Wait for all threads to complete+ threads.each(&:join)++ # Verify all updates persisted (no data loss from race conditions)+ @user.reload+ sections.each do |section|+ assert @user.dashboard_section_collapsed?(section),+ "Expected #{section} to be collapsed, but it was not. " \+ "Preferences: #{@user.preferences.inspect}"+ end++ # Verify all sections are in the preferences hash+ assert_equal sections.sort,+ @user.preferences.dig("collapsed_sections")&.keys&.sort,+ "Expected all sections to be in preferences"+ end++ test "update_dashboard_preferences merges nested hashes correctly" do+ @user.update!(preferences: {})++ # First update: collapse net_worth+ @user.update_dashboard_preferences({+ "collapsed_sections" => { "net_worth_chart" => true }+ })+ @user.reload++ assert @user.dashboard_section_collapsed?("net_worth_chart")+ assert_not @user.dashboard_section_collapsed?("outflows_donut")++ # Second update: collapse outflows (should preserve net_worth)+ @user.update_dashboard_preferences({+ "collapsed_sections" => { "outflows_donut" => true }+ })+ @user.reload++ assert @user.dashboard_section_collapsed?("net_worth_chart"),+ "First collapsed section should still be collapsed"+ assert @user.dashboard_section_collapsed?("outflows_donut"),+ "Second collapsed section should be collapsed"+ end++ test "update_dashboard_preferences handles section_order updates" do+ @user.update!(preferences: {})++ # Set initial order+ new_order = %w[outflows_donut net_worth_chart cashflow_sankey balance_sheet]+ @user.update_dashboard_preferences({ "section_order" => new_order })+ @user.reload++ assert_equal new_order, @user.dashboard_section_order+ end++ test "handles empty preferences gracefully for dashboard methods" do+ @user.update!(preferences: {})++ # dashboard_section_collapsed? should return false when key is missing+ assert_not @user.dashboard_section_collapsed?("net_worth_chart"),+ "Should return false when collapsed_sections key is missing"++ # dashboard_section_order should return default order when key is missing+ assert_equal %w[cashflow_sankey outflows_donut net_worth_chart balance_sheet],+ @user.dashboard_section_order,+ "Should return default order when section_order key is missing"++ # update_dashboard_preferences should work with empty preferences+ @user.update_dashboard_preferences({ "section_order" => %w[balance_sheet] })+ @user.reload++ assert_equal %w[balance_sheet], @user.preferences["section_order"]+ end++ test "handles empty preferences gracefully for reports methods" do+ @user.update!(preferences: {})++ # reports_section_collapsed? should return false when key is missing+ assert_not @user.reports_section_collapsed?("trends_insights"),+ "Should return false when reports_collapsed_sections key is missing"++ # reports_section_order should return default order when key is missing+ assert_equal %w[trends_insights transactions_breakdown],+ @user.reports_section_order,+ "Should return default order when reports_section_order key is missing"++ # update_reports_preferences should work with empty preferences+ @user.update_reports_preferences({ "reports_section_order" => %w[transactions_breakdown] })+ @user.reload++ assert_equal %w[transactions_breakdown], @user.preferences["reports_section_order"]+ end++ test "handles missing nested keys in preferences for collapsed sections" do+ @user.update!(preferences: { "section_order" => %w[cashflow] })++ # Should return false when collapsed_sections key is missing entirely+ assert_not @user.dashboard_section_collapsed?("net_worth_chart"),+ "Should return false when collapsed_sections key is missing"++ # Should return false when section_key is missing from collapsed_sections+ @user.update!(preferences: { "collapsed_sections" => {} })+ assert_not @user.dashboard_section_collapsed?("net_worth_chart"),+ "Should return false when section key is missing from collapsed_sections"+ endend