Skip to content

Latest commit

 

History

History
156 lines (116 loc) · 5.82 KB

File metadata and controls

156 lines (116 loc) · 5.82 KB

Background Pattern Settings Fix

Date: 2026-01-11
Issue: Background pattern settings in SettingsPage were not updating the dashboard background in real-time.

Root Causes Identified

1. Reactivity Issue in DashboardLayout (CRITICAL)

Problem: The component was reading settings from the store once during initial render but not subscribing to changes.

Before:

const { settings } = useSettingsStore();
const currentPattern = (settings.find(s => s.key === 'ui_pattern')?.value || 'circuit') as PatternType;
const patternOpacity = (settings.find(s => s.key === 'ui_pattern_opacity')?.value || 0.03) as number;

After:

// Use Zustand selectors to subscribe to specific settings for reactivity
const currentPattern = useSettingsStore(
  (state) => (state.settings.find(s => s.key === 'ui_pattern')?.value || 'circuit') as PatternType
);
const patternOpacity = useSettingsStore(
  (state) => (state.settings.find(s => s.key === 'ui_pattern_opacity')?.value || 0.03) as number
);

Why this works: Zustand's selector pattern creates a subscription to the specific derived value. When updateSetting() is called in SettingsPage, it triggers a re-render in DashboardLayout because the selector detects the change.


2. Theme Detection Issue in BackgroundPatterns

Problem: The component was checking document.body.classList.contains('light-mode') which doesn't integrate with the actual theme system.

Before:

const isLightMode = document.body.classList.contains('light-mode');

After:

const { theme } = useSystemStore();
const isLightMode = theme === 'light';

Why this works: Now the component properly subscribes to theme changes from the systemStore, ensuring the correct light/dark pattern variant is loaded when the theme changes.


3. Missing URL Quotes in CSS (CRITICAL - The Final Issue)

Problem: The backgroundImage style was missing quotes around the data URL, causing the browser to ignore the invalid CSS.

Before:

style={{
  backgroundImage: `url(${patternUrl})`,  // ❌ Invalid CSS - missing quotes
}}

After:

style={{
  backgroundImage: `url("${patternUrl}")`,  // ✅ Valid CSS with quoted URL
}}

Why this works: CSS url() function requires quotes around data URLs. Without quotes, the browser treats the data URL as an invalid value and ignores the entire background-image property, resulting in no pattern being displayed.


Files Modified

  1. src/components/layout/DashboardLayout.tsx (The correct, active layout file)

    • Added imports for useSettingsStore and BackgroundPatterns
    • Changed from destructuring entire settings array to using Zustand selectors
    • Removed static bg-grid-pattern CSS class
    • Added <BackgroundPatterns /> component with reactive props
    • Ensures component re-renders when pattern settings change
  2. src/components/ui/backgroundpatterns.tsx

    • Integrated useSystemStore() for proper theme detection
    • Fixed import paths to match actual folder casing (Patterns/Dark/ instead of patterns/dark/)
    • Fixed critical CSS bug: Added quotes around data URL in backgroundImage style
    • Fixed className concatenation to avoid "undefined" in class list
    • Added debug logging for troubleshooting
  3. src/lib/store/useSettingsStore.tsx

    • No changes needed (already using Zustand persist middleware correctly)

Testing Results ✅

All tests passed successfully:

Test Result
Pattern Visibility ✅ All patterns (geometric, circuit, wave, grid) render correctly
Real-time Updates ✅ Changing pattern in settings updates background immediately
Opacity Control ✅ Opacity slider works from 0.1 (subtle) to 0.8 (prominent)
Theme Integration ✅ Patterns switch between light/dark variants with theme
Persistence ✅ Settings saved to localStorage and restored on reload
Navigation ✅ Patterns persist when navigating between pages
"None" Option ✅ Selecting "none" removes the pattern completely

Testing Instructions

  1. Login to CortexBridge at http://localhost:6001
  2. Navigate to Settings (sidebar → Settings)
  3. Go to Appearance tab
  4. Test Pattern Changes:
    • Change "Background Pattern" dropdown between: geometric, circuit, wave, grid, none
    • Expected: Background should update immediately without page refresh
  5. Test Opacity Changes:
    • Adjust "Pattern Opacity" slider (0.0 - 1.0)
    • Expected: Background pattern opacity should change in real-time
  6. Test Theme Integration:
    • Toggle between Light/Dark theme
    • Expected: Pattern should switch to appropriate light/dark variant

Technical Notes

Zustand Reactivity Pattern

The key insight is that Zustand requires selector functions to create subscriptions:

// ❌ NOT REACTIVE - reads once
const { settings } = useStore();
const value = settings.find(x => x.key === 'foo')?.value;

// ✅ REACTIVE - subscribes to changes
const value = useStore(state => state.settings.find(x => x.key === 'foo')?.value);

Persist Middleware

The useSettingsStore uses Zustand's persist middleware with localStorage key 'cortexbridge-settings'. Changes are automatically saved and restored on page reload.


Known Issues

  • Lint Warning: ESLint flags window.location.hash = page in handleNavigate() as "modifying a value defined outside a component". This is a false positive - modifying window.location.hash is standard browser API usage and is safe.

Future Improvements

  1. Consider adding a "Preview" mode in settings that shows a live preview of the pattern
  2. Add animation transitions when switching patterns
  3. Consider adding custom pattern upload functionality
  4. Add pattern intensity/scale controls