All files / web/src/components/tutorial TutorialEditor.stories.tsx

0% Statements 0/385
0% Branches 0/1
0% Functions 0/1
0% Lines 0/385

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { action } from '@storybook/addon-actions'
import type { Meta, StoryObj } from '@storybook/react'
import { DevAccessProvider } from '../../hooks/useAccessControl'
import { createBasicSkillSet, type TutorialValidation } from '../../types/tutorial'
import { getTutorialForEditor } from '../../utils/tutorialConverter'
import { TutorialEditor } from './TutorialEditor'

const meta: Meta<typeof TutorialEditor> = {
  title: 'Tutorial/TutorialEditor',
  component: TutorialEditor,
  parameters: {
    layout: 'fullscreen',
    docs: {
      description: {
        component: `
The TutorialEditor component provides a comprehensive editing interface for creating and modifying tutorial content.
It includes both the editor interface and an integrated preview player for testing changes.

## Features
- Visual step editor with form-based editing
- Real-time validation and error reporting
- Integrated tutorial player for preview
- Step management (add, duplicate, delete, reorder)
- Tutorial metadata editing
- Save/load functionality hooks
- Access control integration

## Editor Capabilities
- Edit tutorial metadata (title, description, category, difficulty, tags)
- Manage tutorial steps with detailed form controls
- Set start/target values and highlight configurations
- Edit tooltips, error messages, and multi-step instructions
- Validate tutorial structure and content
- Preview changes in real-time
        `,
      },
    },
  },
  decorators: [
    (Story) => (
      <DevAccessProvider>
        <div style={{ height: '100vh' }}>
          <Story />
        </div>
      </DevAccessProvider>
    ),
  ],
  tags: ['autodocs'],
}

export default meta
type Story = StoryObj<typeof meta>

const mockTutorial = getTutorialForEditor()

// Mock validation function that returns realistic validation results
const mockValidate = async (tutorial: any): Promise<TutorialValidation> => {
  const errors = []
  const warnings = []

  // Simulate some validation logic
  if (!tutorial.title.trim()) {
    errors.push({
      stepId: '',
      field: 'title',
      message: 'Tutorial title is required',
      severity: 'error' as const,
    })
  }

  if (tutorial.steps.length === 0) {
    errors.push({
      stepId: '',
      field: 'steps',
      message: 'Tutorial must have at least one step',
      severity: 'error' as const,
    })
  }

  // Add some warnings for demonstration
  if (tutorial.description.length < 50) {
    warnings.push({
      stepId: '',
      field: 'description',
      message: 'Tutorial description could be more detailed',
      severity: 'warning' as const,
    })
  }

  tutorial.steps.forEach((step: any, index: number) => {
    if (step.startValue === step.targetValue) {
      warnings.push({
        stepId: step.id,
        field: 'values',
        message: `Step ${index + 1}: Start and target values are the same`,
        severity: 'warning' as const,
      })
    }
  })

  return {
    isValid: errors.length === 0,
    errors,
    warnings,
  }
}

const mockOnSave = async (tutorial: any) => {
  action('save-tutorial')(tutorial)
}

export const Default: Story = {
  args: {
    tutorial: mockTutorial,
    onSave: mockOnSave,
    onValidate: mockValidate,
    onPreview: action('preview-step'),
  },
  parameters: {
    docs: {
      description: {
        story: 'Default tutorial editor with the guided addition tutorial loaded for editing.',
      },
    },
  },
}

export const EditingMode: Story = {
  args: {
    ...Default.args,
  },
  parameters: {
    docs: {
      description: {
        story: `
Tutorial editor in editing mode. This story demonstrates:

**Editor Features:**
- Click "Edit Tutorial" to enable editing mode
- Modify tutorial metadata in the left sidebar
- Click on steps to expand detailed editing forms
- Add, duplicate, or delete steps using step controls
- Use "Preview" buttons to test steps in the integrated player
- Real-time validation shows errors and warnings

**Try These Actions:**
1. Click "Edit Tutorial" to enable editing
2. Modify the tutorial title or description
3. Click on a step to expand its editing form
4. Change step values and see validation updates
5. Add a new step using the "+ Add Step" button
6. Preview specific steps using the "Preview" buttons
        `,
      },
    },
  },
}

export const WithValidationErrors: Story = {
  args: {
    tutorial: {
      ...mockTutorial,
      title: '', // This will trigger a validation error
      description: 'Short desc', // This will trigger a warning
      steps: mockTutorial.steps.map((step) => ({
        ...step,
        startValue: step.targetValue, // This will trigger warnings
      })),
    },
    onSave: mockOnSave,
    onValidate: mockValidate,
    onPreview: action('preview-step'),
  },
  parameters: {
    docs: {
      description: {
        story:
          'Tutorial editor with validation errors and warnings to demonstrate the validation system.',
      },
    },
  },
}

export const MinimalTutorial: Story = {
  args: {
    tutorial: {
      ...mockTutorial,
      steps: mockTutorial.steps.slice(0, 2), // Only 2 steps for easier editing demo
    },
    onSave: mockOnSave,
    onValidate: mockValidate,
    onPreview: action('preview-step'),
  },
  parameters: {
    docs: {
      description: {
        story:
          'Tutorial editor with a minimal tutorial (2 steps) for easier demonstration of editing features.',
      },
    },
  },
}

export const ReadOnlyPreview: Story = {
  args: {
    tutorial: mockTutorial,
    onSave: undefined, // No save function = read-only mode
    onValidate: mockValidate,
    onPreview: action('preview-step'),
  },
  parameters: {
    docs: {
      description: {
        story:
          'Tutorial editor in read-only mode (no save function provided) showing the preview functionality.',
      },
    },
  },
}

export const WithPracticeSteps: Story = {
  args: {
    tutorial: {
      ...mockTutorial,
      practiceSteps: [
        {
          id: 'practice-basic',
          title: 'Practice: Basic Addition (1-4)',
          description: 'Practice adding numbers 1-4 using only earth beads',
          problemCount: 12,
          maxTerms: 3,
          allowedSkills: createBasicSkillSet(),
          numberRange: { min: 1, max: 4 },
          sumConstraints: { maxSum: 9 },
        },
        {
          id: 'practice-five-complements',
          title: 'Practice: Five Complements',
          description: 'Practice using five complement techniques',
          problemCount: 15,
          maxTerms: 4,
          allowedSkills: {
            basic: {
              directAddition: true,
              heavenBead: true,
              simpleCombinations: true,
              directSubtraction: false,
              heavenBeadSubtraction: false,
              simpleCombinationsSub: false,
            },
            fiveComplements: {
              '4=5-1': true,
              '3=5-2': true,
              '2=5-3': false,
              '1=5-4': false,
            },
            tenComplements: {
              '9=10-1': false,
              '8=10-2': false,
              '7=10-3': false,
              '6=10-4': false,
              '5=10-5': false,
              '4=10-6': false,
              '3=10-7': false,
              '2=10-8': false,
              '1=10-9': false,
            },
            fiveComplementsSub: {
              '-4=-5+1': false,
              '-3=-5+2': false,
              '-2=-5+3': false,
              '-1=-5+4': false,
            },
            tenComplementsSub: {
              '-9=+1-10': false,
              '-8=+2-10': false,
              '-7=+3-10': false,
              '-6=+4-10': false,
              '-5=+5-10': false,
              '-4=+6-10': false,
              '-3=+7-10': false,
              '-2=+8-10': false,
              '-1=+9-10': false,
            },
            advanced: {
              cascadingCarry: false,
              cascadingBorrow: false,
            },
          },
          targetSkills: {
            fiveComplements: {
              '4=5-1': true,
              '3=5-2': true,
              '2=5-3': false,
              '1=5-4': false,
            },
          },
          numberRange: { min: 1, max: 9 },
          sumConstraints: { maxSum: 9 },
        },
      ],
    },
    onSave: mockOnSave,
    onValidate: mockValidate,
    onPreview: action('preview-step'),
  },
  parameters: {
    docs: {
      description: {
        story:
          'Tutorial editor with practice steps demonstrating the skill-based problem generation system.',
      },
    },
  },
}

export const CustomTutorial: Story = {
  args: {
    tutorial: {
      id: 'custom-tutorial',
      title: 'Custom Math Tutorial',
      description:
        'A custom tutorial for demonstrating the editor capabilities with different content.',
      category: 'Advanced Operations',
      difficulty: 'intermediate' as const,
      estimatedDuration: 15,
      steps: [
        {
          id: 'custom-1',
          title: 'Custom Step 1',
          problem: '5 + 5',
          description: 'Add 5 to 5 using the heaven bead',
          startValue: 5,
          targetValue: 10,
          highlightBeads: [{ placeValue: 1, beadType: 'heaven' as const }],
          expectedAction: 'add' as const,
          actionDescription: 'Click the heaven bead',
          tooltip: {
            content: 'Using heaven bead for 10',
            explanation: 'When adding 5 to 5, use the tens place heaven bead',
          },
        },
        {
          id: 'custom-2',
          title: 'Custom Step 2',
          problem: '7 + 8',
          description: 'A more complex addition problem',
          startValue: 7,
          targetValue: 15,
          highlightBeads: [
            { placeValue: 10, beadType: 'heaven' as const },
            { placeValue: 1, beadType: 'heaven' as const },
          ],
          expectedAction: 'multi-step' as const,
          actionDescription: 'Activate both heaven beads for 15',
          multiStepInstructions: [
            'Click the tens place heaven bead',
            'Click the ones place heaven bead',
          ],
          tooltip: {
            content: 'Complex addition',
            explanation: '7 + 8 = 15, which needs both tens and ones heaven beads',
          },
        },
      ],
      tags: ['custom', 'demo', 'advanced'],
      author: 'Demo Author',
      version: '1.0.0',
      createdAt: new Date(),
      updatedAt: new Date(),
      isPublished: false,
    },
    onSave: mockOnSave,
    onValidate: mockValidate,
    onPreview: action('preview-step'),
  },
  parameters: {
    docs: {
      description: {
        story:
          'Tutorial editor with custom tutorial content to demonstrate editing different types of mathematical operations.',
      },
    },
  },
}