import React, { useContext, useEffect, useState } from 'react' import { type Toggle, ToggleProviderContext, } from '../ToggleProvider/ToggleProvider' import Button from '../Button/Button' import EmptyState from '../EmptyState/EmptyState' import Tag from '../Tag/Tag' import ToggleTable from './ToggleTable' // Mock data for demonstration with realistic toggle examples export const mockToggles: Toggle[] = [ // Library toggles { application: 'Library', enabled: true, key: 'new-component-system', name: 'New Component System', }, { application: 'Library', enabled: false, key: 'experimental-hooks', name: 'Experimental Hooks', }, { application: 'Library', enabled: true, key: 'design-tokens-v2', name: 'Design Tokens V2', }, // PXM toggles { application: 'PXM', enabled: true, key: 'enhanced-product-catalog', name: 'Enhanced Product Catalog', }, { application: 'PXM', enabled: false, key: 'ai-product-descriptions', name: 'AI Product Descriptions', }, { application: 'PXM', enabled: true, key: 'bulk-product-import', name: 'Bulk Product Import', }, // Predict toggles { application: 'Predict', enabled: true, key: 'advanced-analytics', name: 'Advanced Analytics', }, { application: 'Predict', enabled: false, key: 'machine-learning-insights', name: 'Machine Learning Insights', }, { application: 'Predict', enabled: false, key: 'predictive-modeling', name: 'Predictive Modeling', }, // Shelf toggles { application: 'Shelf', enabled: true, key: 'inventory-tracking', name: 'Inventory Tracking', }, { application: 'Shelf', enabled: false, key: 'automated-restocking', name: 'Automated Restocking', }, { application: 'Shelf', enabled: true, key: 'real-time-updates', name: 'Real-time Updates', }, // Admin toggles { application: 'Admin', enabled: true, key: 'user-management-v2', name: 'User Management V2', }, { application: 'Admin', enabled: false, key: 'advanced-permissions', name: 'Advanced Permissions', }, { application: 'Admin', enabled: false, key: 'audit-logging', name: 'Audit Logging', }, ] // Mock implementation that simulates the CDN fetch for ToggleProvider stories export const MockToggleProvider = ({ children, distributionKey, environment, errorCallback, finishedLoadingCallback, simulateError = false, simulateDelay = 1000, }: { children: React.ReactNode distributionKey: string environment: 'development' | 'staging' | 'production' errorCallback?: (error: string | Record) => void finishedLoadingCallback?: (isLoaded: boolean) => void simulateError?: boolean simulateDelay?: number }) => { const [toggles, setToggles] = useState(undefined) useEffect(() => { const timer = setTimeout(() => { if (simulateError) { const error = new Error('Failed to fetch toggles from CDN') errorCallback?.(error.message) setToggles([]) } else { setToggles(mockToggles) } finishedLoadingCallback?.(true) }, simulateDelay) return () => clearTimeout(timer) }, [ distributionKey, environment, simulateError, simulateDelay, errorCallback, finishedLoadingCallback, ]) return ( {children} ) } // Component that demonstrates ToggleTable functionality export const ToggleTableDemo = ({ title = 'ToggleTable Demo', showDescription = true, }) => { const { toggles } = useContext(ToggleProviderContext) if (toggles === undefined) { return (

Loading Toggles...

Fetching feature toggles from CDN...

) } if (toggles.length === 0) { return ( ) } // Convert Toggle[] to the format expected by ToggleTable const tableData = toggles.map((toggle) => ({ key: toggle.key, enabled: toggle.enabled, application: toggle.application, // ToggleTable uses this for grouping })) return (
{showDescription && (

{title}

This demonstrates the ToggleTable component used in DevTools. Features include:

  • Grouped by application
  • Toggle switches to override values locally
  • Visual indicators when local values differ from CDN
)} {}} />
) } // Simple card-based demo for comparison export const SimpleToggleDemo = () => { const { toggles } = useContext(ToggleProviderContext) const [filter, setFilter] = useState<'all' | 'enabled' | 'disabled'>('all') if (toggles === undefined) { return (

Loading Toggles...

Fetching feature toggles from CDN...

) } if (toggles.length === 0) { return ( ) } const filteredToggles = toggles.filter((toggle) => { if (filter === 'enabled') return toggle.enabled if (filter === 'disabled') return !toggle.enabled return true }) return (

Simple Toggle List ({toggles.length})

{filteredToggles.map((toggle) => (
{toggle.name}
{toggle.enabled ? 'Enabled' : 'Disabled'}
Key: {toggle.key}
Application: {toggle.application}
))}
) } // Standalone demo that doesn't require ToggleProvider context export const StandaloneToggleTableDemo = ({ customToggles = mockToggles, title = 'ToggleTable Component Demo', showDescription = true, }) => { // Convert Toggle[] to the format expected by ToggleTable const tableData = customToggles.map((toggle) => ({ key: toggle.key, enabled: toggle.enabled, application: toggle.application, })) return (
{showDescription && (

{title}

The ToggleTable component provides a professional interface for managing feature toggles. This standalone demo shows the component without requiring a ToggleProvider.

  • Application Grouping: Toggles are organized by application
  • Local Overrides: Switch toggles to override values in localStorage
  • Visual Indicators: Highlighted rows when local values differ from CDN
  • Responsive Design: Works on desktop and mobile devices
)} {}} />
) }