Static vs Saved Variants - Critical Fix

Fixed "Variant not found" error by distinguishing between static demo variants and saved user variants

Static vs Saved Variants - Critical Fix

๐Ÿ› Problem Identified

Error: "Variant not found" when clicking "Edit" on static demo variants (e.g., "primary", "secondary", "danger").

Root Cause: The variant editor was trying to load static mock variants (defined in CoreComponentDetailComponent) from VariantStorageService, which doesn't contain them.

Error Flow

User clicks "Edit" on "Primary" button variant
  โ†“
Navigate to: /widgets/variant-editor/button/primary
  โ†“
VariantBuilderPage tries to load "primary" from storage
  โ†“
โŒ Error: "Variant not found" (it's a static demo, not in storage)

โœ… Solution Implemented

1. Added isStatic Flag to ComponentVariant

interface ComponentVariant {
  id: string;
  name: string;
  description: string;
  preview: string;
  code: string;
  properties?: Record<string, any>;
  isStatic?: boolean; // NEW: Flag to distinguish static demo variants
}

2. Flag Static Variants in allVariants Computed

// Merge static variants with loaded variants
readonly allVariants = computed((): ComponentVariant[] => {
  const data = this.componentData();
  const loaded = this.loadedVariants();

  // Convert loaded WidgetVariants to ComponentVariant format
  const loadedConverted: ComponentVariant[] = loaded.map(v => ({
    ...v,
    isStatic: false // โœ… Loaded variants are NOT static
  }));

  // Mark static variants with isStatic flag
  const allVariants: ComponentVariant[] = data.variants.map(v => ({
    ...v,
    isStatic: true // โœ… Static demo variants
  }));
  
  // Merge without duplicates
  for (const variant of loadedConverted) {
    if (!allVariants.find(v => v.id === variant.id)) {
      allVariants.push(variant);
    }
  }

  return allVariants;
});

3. Conditional Action Buttons in Template

<!-- Only show Edit/Duplicate for saved variants (non-static) -->
@if (!variant.isStatic) {
  <button class="btn-action btn-edit" (click)="editVariant(variant)">
    <span class="material-icons">edit</span>
    Edit
  </button>
  <button class="btn-action btn-duplicate" (click)="duplicateVariant(variant)">
    <span class="material-icons">content_copy</span>
    Duplicate
  </button>
} @else {
  <!-- For static variants, show "Use Template" -->
  <button class="btn-action btn-duplicate" (click)="duplicateVariant(variant)">
    <span class="material-icons">add_circle</span>
    Use Template
  </button>
}

4. Visual Indicator for Static Variants

Added a "TEMPLATE" badge to static variants:

<div class="variant-header">
  <h4>{{ variant.name }}</h4>
  @if (variant.isStatic) {
    <span class="badge-template">Template</span>
  }
</div>

๐ŸŽจ User Experience

Before Fix

After Fix


๐Ÿ”„ Variant Types

Static Variants (Templates)

Saved Variants (User-Created)


๐Ÿงช Testing

Test Scenario 1: Static Variant

  1. Navigate to: http://localhost:5200/space/chrome-space-1/widgets/guide/button
  2. Hover over "Primary" variant card
  3. โœ… Should see "TEMPLATE" badge
  4. โœ… Should see "Use Template" button
  5. โœ… Should NOT see "Edit" button
  6. Click "Use Template"
  7. โœ… Modal opens with pre-filled data for duplication

Test Scenario 2: Saved Variant

  1. Create a new variant (e.g., "My Button")
  2. Return to component detail page
  3. Hover over "My Button" variant card
  4. โœ… Should NOT see "TEMPLATE" badge
  5. โœ… Should see "Edit" button
  6. โœ… Should see "Duplicate" button
  7. Click "Edit"
  8. โœ… Navigates to full-page editor
  9. โœ… Variant loads successfully (no error)

๐Ÿ“Š Impact

Files Modified

  1. core-component-detail.component.ts (interface, computed property)
  2. core-component-detail.component.html (conditional rendering)
  3. core-component-detail.component.scss (badge styles)

Benefits


๐Ÿš€ Future Enhancements

  1. Template Library: Expand static variants into a rich template library
  2. Template Categories: Organize templates by use case (CTA, Navigation, Forms, etc.)
  3. Import/Export: Allow users to share custom variants as templates
  4. Template Preview: Interactive preview before using template
  5. Template Versioning: Track template updates and migrations

๐Ÿ“ Related Documentation


โœ… Summary

Problem: "Variant not found" error when editing static demo variants.

Solution: Added isStatic flag to distinguish templates from saved variants, conditionally showing Edit/Duplicate buttons, and adding visual "TEMPLATE" badge.

Result:

Status: Fixed and tested โœ