import React from 'react' import { type Meta, type StoryObj } from '@storybook/react-vite' import { DocsTemplate } from '../../../.storybook' import ToggleProvider, { ToggleProviderContext } from './ToggleProvider' import { MockToggleProvider, ToggleTableDemo, } from '../DevTools/toggle-stories-helper' const meta: Meta = { title: 'Providers/ToggleProvider', component: ToggleProvider, parameters: { layout: 'padded', docs: { page: () => ( The ToggleProvider component is a React context provider that fetches and manages feature toggles from a CDN endpoint. It provides toggle data to child components through React context, enabling feature flag functionality throughout the application.

This component handles the entire lifecycle of toggle management including loading states, error handling, and cleanup. It automatically fetches toggles based on the provided distribution key and environment.

The demo below shows the ToggleTable component from DevTools, which demonstrates real-world usage of toggles with grouping, local storage overrides, and toggle switching functionality. } infoBullets={[ Use the ToggleProvider to wrap your application or specific sections that need access to feature toggles. , Child components can access toggle data using the{' '} ToggleProviderContext with React's useContext hook. , The provider automatically handles loading states and provides callbacks for error handling and completion notifications. , Supports different environments (development, staging, production) and uses the appropriate CDN endpoint for each. , Includes automatic cleanup and request cancellation to prevent memory leaks and race conditions. , The ToggleTable demo shows advanced features like grouping by application, local storage overrides, and visual indicators for toggle differences. , ]} /> ), }, }, argTypes: { children: { description: 'Child components that will have access to the toggle context', control: false, }, distributionKey: { description: 'Distribution key used to identify which toggles to fetch', control: 'text', }, environment: { description: 'Environment to fetch toggles for', control: 'select', options: ['development', 'staging', 'production'], }, errorCallback: { description: 'Optional callback for error handling', action: 'error', }, finishedLoadingCallback: { description: 'Optional callback when loading finishes', action: 'finished loading', }, }, } export default meta type Story = StoryObj export const Default: Story = { render: (args) => ( ), args: { distributionKey: 'example-app', environment: 'development', }, } export const EmptyToggles: Story = { render: () => ( ), args: { distributionKey: 'empty-app', environment: 'development', }, }