From 6c99b31d48951e3200a90eff71d97312b34c58df Mon Sep 17 00:00:00 2001 From: Lucas 'Paperboy' Rose-Winters Date: Thu, 6 Nov 2025 21:11:36 +1100 Subject: [PATCH] refactor: use resetLayout() for first-run initialization Refactored loadLayout() to call resetLayout() for first-run users instead of duplicating setup logic inline. This provides a single source of truth for "set to default layout" behavior. Previous Approach (Problems): - loadLayout() had ~40 lines of inline first-run setup - Different behavior: used autoLayout (repositions widgets) - Less comprehensive: no state reset, different persistence method - Code duplication between loadLayout() and resetLayout() - Error handler also duplicated applyDashboardConfig() logic - Emergency fallback tab creation needed after load New Approach (Benefits): - loadLayout() is now a simple router: saved layout vs. reset - Consistent behavior: first-run and manual reset use same code path - More comprehensive: fresh layout generation, state reset, validation - Better error recovery: always uses resetLayout() for clean state - Single source of truth for default layout initialization - ~40 lines removed, cleaner code Changes: 1. Removed inline applyDashboardConfig(defaultLayout) for first run 2. Removed manual autoLayout() loop (lines 1489-1494) 3. Removed manual saveLayout() call (line 1497) 4. Removed error handler's inline applyDashboardConfig() 5. Removed emergency fallback tab creation (lines 1506-1520) 6. Added resetLayout() call for first run (line 1494) 7. Added resetLayout() call for error recovery (line 1500) Result: - First-run users get comprehensive setup via resetLayout() - Preserves default positions (doesn't reposition with autoLayout) - Consistent layout behavior across first-run and manual reset - Better maintainability (single code path for default setup) - Proven reliability (resetLayout already works in production) --- src/systems/dashboard/dashboardManager.js | 47 +++++++---------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/src/systems/dashboard/dashboardManager.js b/src/systems/dashboard/dashboardManager.js index eb000fb..d6e5f32 100644 --- a/src/systems/dashboard/dashboardManager.js +++ b/src/systems/dashboard/dashboardManager.js @@ -1475,48 +1475,29 @@ export class DashboardManager { /** * Load saved layout + * + * For first-run users (no saved layout), calls resetLayout() for comprehensive + * initialization. This ensures consistent behavior between first-run and manual + * reset, using a single code path for default layout setup. */ async loadLayout() { try { const saved = await this.persistence.loadLayout(); if (saved) { + console.log('[DashboardManager] Loading saved layout'); this.applyDashboardConfig(saved); - } else if (this.defaultLayout) { - console.log('[DashboardManager] No saved layout, using default with auto-layout'); - this.applyDashboardConfig(this.defaultLayout); - - // Auto-layout each tab to prevent overlap (default positions may not fit screen) - this.dashboard.tabs.forEach(tab => { - if (tab.widgets && tab.widgets.length > 0) { - console.log(`[DashboardManager] Auto-laying out default tab "${tab.name}" (${tab.widgets.length} widgets)`); - this.gridEngine.autoLayout(tab.widgets, { preserveOrder: true }); - } - }); - - // Save the auto-laid-out default as the initial saved layout - await this.saveLayout(true); + } else { + // First run - use resetLayout() for comprehensive initialization + // This provides: fresh layout generation, state reset, validation, + // column-aware sizing, and proper UI rendering + console.log('[DashboardManager] No saved layout found, calling resetLayout() for first-run initialization'); + await this.resetLayout(); } } catch (error) { console.error('[DashboardManager] Failed to load layout:', error); - if (this.defaultLayout) { - this.applyDashboardConfig(this.defaultLayout); - } - } - - // Safety check: If still no tabs after loading, create emergency fallback - // This should never happen if setDefaultLayout() was called properly - if (this.dashboard.tabs.length === 0) { - console.warn('[DashboardManager] No tabs loaded, creating emergency fallback'); - this.dashboard.tabs.push({ - id: 'tab-status', - name: 'Status', - icon: 'fa-solid fa-user', - order: 0, - widgets: [] - }); - this.dashboard.defaultTab = 'tab-status'; - // Update TabManager's active tab - this.tabManager.setActiveTab('tab-status'); + // Fallback: use resetLayout() for clean state recovery + console.log('[DashboardManager] Recovering with resetLayout()'); + await this.resetLayout(); } }