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)
This commit is contained in:
Lucas 'Paperboy' Rose-Winters
2025-11-06 21:11:36 +11:00
parent fedc93f504
commit 6c99b31d48
+14 -33
View File
@@ -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();
}
}