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

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

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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
import * as HoverCard from '@radix-ui/react-hover-card'
import type { Meta, StoryObj } from '@storybook/react'
import type React from 'react'
import { useState } from 'react'
import { CoachBar } from './CoachBar/CoachBar'
import type { PedagogicalSegment } from './DecompositionWithReasons'
import { ReasonTooltip } from './ReasonTooltip'
import { TutorialUIProvider, useTutorialUI } from './TutorialUIContext'
import './CoachBar/coachbar.css'
import './reason-tooltip.css'

// Enhanced demo components
function MockBeadTooltip({ children, content }: { children: React.ReactNode; content: string }) {
  const [isOpen, setIsOpen] = useState(false)
  const ui = useTutorialUI()

  const handleOpenChange = (open: boolean) => {
    if (open) {
      const granted = ui.requestFocus('bead')
      if (granted) {
        setIsOpen(true)
      }
    } else {
      ui.releaseFocus('bead')
      setIsOpen(false)
    }
  }

  return (
    <HoverCard.Root open={isOpen} onOpenChange={handleOpenChange} openDelay={150} closeDelay={400}>
      <HoverCard.Trigger asChild>
        <div
          style={{
            width: '40px',
            height: '40px',
            backgroundColor: ui.hintFocus === 'bead' ? '#059669' : '#10b981',
            borderRadius: '20px',
            cursor: 'pointer',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            color: 'white',
            fontWeight: 'bold',
            margin: '4px',
            opacity: ui.hintFocus === 'none' || ui.hintFocus === 'bead' ? 1 : 0.5,
            transition: 'all 0.2s ease',
            border: ui.hintFocus === 'bead' ? '2px solid #065f46' : '2px solid transparent',
          }}
        >
          {children}
        </div>
      </HoverCard.Trigger>

      <HoverCard.Portal>
        <HoverCard.Content
          style={{
            backgroundColor: '#ecfdf5',
            border: '1px solid #10b981',
            borderRadius: '8px',
            padding: '12px',
            boxShadow: '0 4px 12px rgba(0, 0, 0, 0.1)',
            maxWidth: '250px',
            zIndex: 50,
          }}
          sideOffset={8}
        >
          <div>
            <div
              style={{
                display: 'flex',
                alignItems: 'center',
                gap: '8px',
                marginBottom: '8px',
              }}
            >
              <span style={{ fontSize: '18px' }}>๐ŸŸข</span>
              <strong style={{ color: '#065f46' }}>Bead Movement</strong>
            </div>
            <p style={{ margin: 0, fontSize: '14px', color: '#047857' }}>{content}</p>
          </div>
          <HoverCard.Arrow style={{ fill: '#ecfdf5', stroke: '#10b981' }} />
        </HoverCard.Content>
      </HoverCard.Portal>
    </HoverCard.Root>
  )
}

function MockTermWithTooltip({
  children,
  segment,
  termIndex,
}: {
  children: React.ReactNode
  segment: PedagogicalSegment
  termIndex: number
}) {
  return (
    <ReasonTooltip termIndex={termIndex} segment={segment} originalValue="50">
      <span
        style={{
          display: 'inline-block',
          padding: '4px 8px',
          backgroundColor: '#dbeafe',
          border: '1px solid #3b82f6',
          borderRadius: '4px',
          cursor: 'pointer',
          margin: '0 2px',
          color: '#1e40af',
          fontFamily: 'monospace',
        }}
      >
        {children}
      </span>
    </ReasonTooltip>
  )
}

const createMockSegment = (overrides: Partial<PedagogicalSegment> = {}): PedagogicalSegment => ({
  id: 'demo-segment',
  place: 1,
  digit: 5,
  a: 0,
  L: 0,
  U: 0,
  goal: 'Add 5 to tens place',
  plan: [
    {
      rule: 'Direct',
      conditions: ['L+d <= 4'],
      explanation: ['Simple addition'],
    },
  ],
  expression: '50',
  stepIndices: [0],
  termIndices: [0],
  termRange: { startIndex: 0, endIndex: 2 },
  startValue: 0,
  endValue: 50,
  startState: { 1: { heavenActive: false, earthActive: 0 } },
  endState: { 1: { heavenActive: true, earthActive: 0 } },
  readable: {
    title: 'Direct Move',
    subtitle: 'Heaven bead helps',
    summary: 'Add 5 to the tens place using the heaven bead.',
    chips: [
      { label: 'Digit being added', value: '5' },
      { label: 'Target place', value: 'tens' },
      { label: 'Rod shows', value: '0 (empty)' },
    ],
    why: [],
    stepsFriendly: ['Press the heaven bead down in tens column'],
    validation: { ok: true, issues: [] },
  },
  ...overrides,
})

function IntegratedDemo() {
  const ui = useTutorialUI()
  const [currentSegment, setCurrentSegment] = useState(createMockSegment())

  const segments = [
    createMockSegment({
      id: 'step-1',
      readable: {
        title: 'Step 1: Direct Move',
        subtitle: 'Simple bead movement',
        summary: 'Add 3 to the ones place. It fits here, so just move 3 lower beads.',
        chips: [
          { label: 'Digit', value: '3' },
          { label: 'Place', value: 'ones' },
        ],
        why: [],
        stepsFriendly: ['Move 3 earth beads down'],
        validation: { ok: true, issues: [] },
      },
    }),
    createMockSegment({
      id: 'step-2',
      readable: {
        title: 'Step 2: Five Friend',
        subtitle: "Using 5's friend",
        summary: "Add 7 to the ones place using 5's friend: press the heaven bead (5) and lift 2.",
        chips: [
          { label: 'Target', value: '7' },
          { label: 'Strategy', value: '5 + 2' },
        ],
        why: [],
        stepsFriendly: ['Press heaven bead', 'Lift 2 earth beads'],
        validation: { ok: true, issues: [] },
      },
    }),
    createMockSegment({
      id: 'step-3',
      readable: {
        title: 'Step 3: Ten Friend',
        subtitle: "Using 10's friend",
        summary: 'Add 8 to the ones place to make 10. Carry to tens place, then take 2 here.',
        chips: [
          { label: 'Target', value: '8' },
          { label: 'Strategy', value: '10 - 2' },
        ],
        why: [],
        stepsFriendly: ['Add 1 to tens', 'Subtract 2 from ones'],
        validation: { ok: true, issues: [] },
      },
    }),
  ]

  // Update the active segment when buttons are clicked
  const handleStepChange = (segment: PedagogicalSegment) => {
    setCurrentSegment(segment)
    ui.setActiveSegment(segment)
  }

  return (
    <div style={{ height: '100vh', backgroundColor: '#f9fafb' }}>
      <CoachBar />

      <div style={{ padding: '20px' }}>
        <h2>Tutorial UI Integration Demo</h2>
        <p>This demonstrates the Coach Bar and single-owner tooltip gate working together.</p>

        {/* Focus status indicator */}
        <div
          style={{
            padding: '12px',
            backgroundColor:
              ui.hintFocus === 'none' ? '#f3f4f6' : ui.hintFocus === 'term' ? '#dbeafe' : '#d1fae5',
            borderRadius: '6px',
            marginBottom: '20px',
            fontFamily: 'monospace',
            fontSize: '14px',
          }}
        >
          <strong>Current Focus:</strong> <code>{ui.hintFocus}</code>
          {ui.hintFocus !== 'none' && (
            <span style={{ marginLeft: '10px', fontSize: '12px', color: '#6b7280' }}>
              (Only {ui.hintFocus} tooltips can open)
            </span>
          )}
        </div>

        {/* Step navigation */}
        <div style={{ marginBottom: '30px' }}>
          <h3>Tutorial Steps</h3>
          <div style={{ display: 'flex', gap: '10px', marginBottom: '20px' }}>
            {segments.map((segment, index) => (
              <button
                key={segment.id}
                onClick={() => handleStepChange(segment)}
                style={{
                  padding: '8px 16px',
                  backgroundColor: currentSegment.id === segment.id ? '#3b82f6' : '#e5e7eb',
                  color: currentSegment.id === segment.id ? 'white' : '#374151',
                  border: 'none',
                  borderRadius: '6px',
                  cursor: 'pointer',
                  fontWeight: currentSegment.id === segment.id ? 'bold' : 'normal',
                }}
              >
                Step {index + 1}
              </button>
            ))}
          </div>
          <p style={{ fontSize: '14px', color: '#6b7280' }}>
            Click the steps above to see how the Coach Bar updates with different content.
          </p>
        </div>

        {/* Mathematical expression with term tooltips */}
        <div style={{ marginBottom: '30px' }}>
          <h3>Mathematical Expression (Term Tooltips)</h3>
          <div
            style={{
              padding: '20px',
              backgroundColor: 'white',
              border: '1px solid #e5e7eb',
              borderRadius: '8px',
              fontSize: '18px',
              fontFamily: 'monospace',
            }}
          >
            <span>27 + </span>
            <MockTermWithTooltip segment={currentSegment} termIndex={0}>
              50
            </MockTermWithTooltip>
            <span> + </span>
            <MockTermWithTooltip segment={currentSegment} termIndex={1}>
              30
            </MockTermWithTooltip>
            <span> = ?</span>
          </div>
          <p style={{ fontSize: '14px', color: '#6b7280', marginTop: '10px' }}>
            Hover over the blue terms to see pedagogical tooltips (term focus).
          </p>
        </div>

        {/* Abacus representation with bead tooltips */}
        <div style={{ marginBottom: '30px' }}>
          <h3>Abacus Representation (Bead Tooltips)</h3>
          <div
            style={{
              padding: '20px',
              backgroundColor: 'white',
              border: '1px solid #e5e7eb',
              borderRadius: '8px',
            }}
          >
            <div style={{ display: 'flex', alignItems: 'center', gap: '20px' }}>
              <div>
                <h4 style={{ margin: '0 0 10px', fontSize: '14px' }}>Hundreds</h4>
                <div
                  style={{
                    display: 'flex',
                    flexDirection: 'column',
                    alignItems: 'center',
                  }}
                >
                  <MockBeadTooltip content="Heaven bead: Press down to add 5 to hundreds place">
                    โšช
                  </MockBeadTooltip>
                  <div
                    style={{
                      height: '2px',
                      width: '50px',
                      backgroundColor: '#8b5cf6',
                      margin: '4px 0',
                    }}
                  />
                  <MockBeadTooltip content="Earth bead 1: Press down to add 1 to hundreds place">
                    ๐Ÿ”ต
                  </MockBeadTooltip>
                  <MockBeadTooltip content="Earth bead 2: Press down to add 1 to hundreds place">
                    ๐Ÿ”ต
                  </MockBeadTooltip>
                </div>
              </div>

              <div>
                <h4 style={{ margin: '0 0 10px', fontSize: '14px' }}>Tens</h4>
                <div
                  style={{
                    display: 'flex',
                    flexDirection: 'column',
                    alignItems: 'center',
                  }}
                >
                  <MockBeadTooltip content="Heaven bead: Press down to add 5 to tens place">
                    โšช
                  </MockBeadTooltip>
                  <div
                    style={{
                      height: '2px',
                      width: '50px',
                      backgroundColor: '#8b5cf6',
                      margin: '4px 0',
                    }}
                  />
                  <MockBeadTooltip content="Earth bead 1: Currently active for this step">
                    ๐Ÿ”ต
                  </MockBeadTooltip>
                  <MockBeadTooltip content="Earth bead 2: Press down to add 1 to tens place">
                    ๐Ÿ”ต
                  </MockBeadTooltip>
                </div>
              </div>

              <div>
                <h4 style={{ margin: '0 0 10px', fontSize: '14px' }}>Ones</h4>
                <div
                  style={{
                    display: 'flex',
                    flexDirection: 'column',
                    alignItems: 'center',
                  }}
                >
                  <MockBeadTooltip content="Heaven bead: Press down to add 5 to ones place">
                    โšช
                  </MockBeadTooltip>
                  <div
                    style={{
                      height: '2px',
                      width: '50px',
                      backgroundColor: '#8b5cf6',
                      margin: '4px 0',
                    }}
                  />
                  <MockBeadTooltip content="Earth bead 1: Press down to add 1 to ones place">
                    ๐Ÿ”ต
                  </MockBeadTooltip>
                  <MockBeadTooltip content="Earth bead 2: Press down to add 1 to ones place">
                    ๐Ÿ”ต
                  </MockBeadTooltip>
                </div>
              </div>
            </div>
          </div>
          <p style={{ fontSize: '14px', color: '#6b7280', marginTop: '10px' }}>
            Hover over the beads to see movement instructions (bead focus).
          </p>
        </div>

        {/* Testing instructions */}
        <div
          style={{
            padding: '16px',
            backgroundColor: '#fef3c7',
            border: '1px solid #f59e0b',
            borderRadius: '8px',
          }}
        >
          <h4 style={{ margin: '0 0 10px', color: '#92400e' }}>๐Ÿงช Test the Single-Owner Gate:</h4>
          <ol style={{ margin: 0, paddingLeft: '20px', color: '#92400e' }}>
            <li>Hover over a blue mathematical term โ†’ term tooltip opens</li>
            <li>
              While keeping mouse over the term, try hovering a green bead โ†’ bead tooltip is blocked
            </li>
            <li>Move mouse away from term โ†’ term tooltip closes</li>
            <li>Now hover over a green bead โ†’ bead tooltip opens successfully</li>
            <li>Try hovering between beads โ†’ focus stays with bead tooltips</li>
            <li>Switch tutorial steps โ†’ Coach Bar updates with new content</li>
          </ol>
        </div>
      </div>
    </div>
  )
}

const meta: Meta<typeof TutorialUIProvider> = {
  title: 'Tutorial/Complete Tutorial UI',
  component: TutorialUIProvider,
  parameters: {
    layout: 'fullscreen',
    docs: {
      description: {
        component: `
Complete integration demo showing the Coach Bar and single-owner tooltip gate working together in a realistic tutorial scenario.

This story demonstrates:
- **Coach Bar** showing contextual guidance
- **Term tooltips** explaining mathematical concepts
- **Bead tooltips** showing physical movements
- **Single-owner gate** ensuring only one tooltip type is active
- **Dynamic content** updating as tutorial progresses
        `,
      },
    },
  },
}

export default meta
type Story = StoryObj<typeof meta>

export const Complete: Story = {
  args: {},
  render: () => (
    <TutorialUIProvider>
      <IntegratedDemo />
    </TutorialUIProvider>
  ),
}

export const CoachBarOnly: Story = {
  args: {},
  render: () => (
    <TutorialUIProvider
      initialSegment={createMockSegment({
        readable: {
          title: 'Adding Large Numbers',
          subtitle: 'Multi-step process',
          summary:
            'When adding 789 + 456, we break it down into place values and handle each column systematically.',
          chips: [
            { label: 'Problem', value: '789 + 456' },
            { label: 'Strategy', value: 'Column by column' },
          ],
          why: [],
          stepsFriendly: [],
          validation: { ok: true, issues: [] },
        },
      })}
    >
      <div style={{ height: '100vh', backgroundColor: '#f9fafb' }}>
        <CoachBar />
        <div style={{ padding: '20px' }}>
          <h2>Coach Bar Only Demo</h2>
          <p>This shows just the Coach Bar without the tooltip interactions.</p>
          <div
            style={{
              height: '400px',
              backgroundColor: '#e5e7eb',
              borderRadius: '8px',
              padding: '20px',
            }}
          >
            <p>Tutorial content would go here...</p>
          </div>
        </div>
      </div>
    </TutorialUIProvider>
  ),
}