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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | 'use client' import { useState, useCallback, useMemo, forwardRef } from 'react' import { useRouter } from 'next/navigation' import * as Dialog from '@radix-ui/react-dialog' import * as Tabs from '@radix-ui/react-tabs' import type { ExecutableFlowchart, ProblemValue } from '@/lib/flowcharts/schema' import type { GeneratedExample } from '@/lib/flowcharts/loader' import type { DiagnosticReport } from '@/lib/flowcharts/doctor' import { PracticeTab } from './PracticeTab' import { FlowchartTab } from './FlowchartTab' import { WorksheetTab } from './WorksheetTab' import { DiagnosticAlert, DiagnosticList } from './FlowchartDiagnostics' import { css } from '../../../styled-system/css' import { hstack, vstack } from '../../../styled-system/patterns' export type ModalTab = 'practice' | 'flowchart' | 'worksheet' interface FlowchartModalProps { /** The loaded flowchart to display */ flowchart: ExecutableFlowchart /** Called when a problem is selected to start practicing */ onSubmit: (values: Record<string, ProblemValue>) => void /** Called when the modal should close */ onClose?: () => void /** Optional share URL for the flowchart */ shareUrl?: string /** Initial tab to show */ defaultTab?: ModalTab /** Generated examples from parent (cached) */ examples?: GeneratedExample[] /** Called when examples are generated (for parent caching) */ onExamplesGenerated?: (examples: GeneratedExample[]) => void /** Tier counts for worksheet tab */ tierCounts?: { easy: number; medium: number; hard: number } /** Optional diagnostic report to show health issues */ diagnosticReport?: DiagnosticReport /** Flowchart ID (for workshop navigation) */ flowchartId?: string /** Whether this is a database flowchart owned by current user */ isOwnedByUser?: boolean /** Source of the flowchart */ source?: 'hardcoded' | 'database' } /** * Modal component for flowcharts with three tabs: * - Practice: Select problems from generated examples * - Flowchart: View mermaid diagram + download PDF * - Worksheet: Generate PDF worksheets with difficulty controls */ export const FlowchartModal = forwardRef<HTMLDivElement, FlowchartModalProps>( function FlowchartModal( { flowchart, onSubmit, onClose, shareUrl, defaultTab = 'practice', examples: initialExamples = [], onExamplesGenerated, tierCounts: externalTierCounts, diagnosticReport, flowchartId, isOwnedByUser, source, }, ref ) { const router = useRouter() const [activeTab, setActiveTab] = useState<ModalTab>(defaultTab) const [showDiagnosticDetails, setShowDiagnosticDetails] = useState(false) const [isCreatingWorkshop, setIsCreatingWorkshop] = useState(false) // Handler to create a workshop session for editing (user's own flowchart) const handleEdit = useCallback(async () => { if (!flowchartId || isCreatingWorkshop) return setIsCreatingWorkshop(true) try { const response = await fetch('/api/flowchart-workshop/sessions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ editPublishedId: flowchartId }), }) if (!response.ok) { throw new Error('Failed to create edit session') } const data = await response.json() onClose?.() router.push(`/flowchart/workshop/${data.session.id}`) } catch (error) { console.error('Failed to start editing:', error) setIsCreatingWorkshop(false) } }, [flowchartId, isCreatingWorkshop, onClose, router]) // Handler to create a remix session const handleRemix = useCallback(async () => { if (!flowchartId || isCreatingWorkshop) return setIsCreatingWorkshop(true) try { const response = await fetch('/api/flowchart-workshop/sessions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ remixFromId: flowchartId }), }) if (!response.ok) { throw new Error('Failed to create remix session') } const data = await response.json() onClose?.() router.push(`/flowchart/workshop/${data.session.id}`) } catch (error) { console.error('Failed to remix flowchart:', error) setIsCreatingWorkshop(false) } }, [flowchartId, isCreatingWorkshop, onClose, router]) // Handler to create a workshop session and navigate to it (for fixing diagnostics) const handleFixInWorkshop = useCallback(async () => { if (!flowchartId || isCreatingWorkshop) return setIsCreatingWorkshop(true) try { // Build the request body based on ownership const body: Record<string, string> = {} if (source === 'database' && isOwnedByUser) { // User owns this published flowchart - edit in place body.editPublishedId = flowchartId } else if (source === 'database') { // Someone else's flowchart - remix it body.remixFromId = flowchartId } else { // Hardcoded flowchart - remix it body.remixFromId = flowchartId } const response = await fetch('/api/flowchart-workshop/sessions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) if (!response.ok) { throw new Error('Failed to create workshop session') } const data = await response.json() const sessionId = data.session.id // Close modal and navigate to workshop onClose?.() router.push(`/flowchart/workshop/${sessionId}`) } catch (error) { console.error('Failed to create workshop session:', error) setIsCreatingWorkshop(false) } }, [flowchartId, isOwnedByUser, source, isCreatingWorkshop, onClose, router]) // Track examples internally so worksheet tab can access them after PracticeTab generates them const [internalExamples, setInternalExamples] = useState<GeneratedExample[]>(initialExamples) // Handle examples being generated by PracticeTab const handleExamplesGenerated = useCallback( (newExamples: GeneratedExample[]) => { setInternalExamples(newExamples) onExamplesGenerated?.(newExamples) }, [onExamplesGenerated] ) // Calculate tier counts from examples if not provided externally const tierCounts = useMemo(() => { if (externalTierCounts) return externalTierCounts if (internalExamples.length === 0) return { easy: 0, medium: 0, hard: 0 } // Calculate difficulty range const scores = internalExamples.map( (ex) => ex.complexity.decisions + ex.complexity.checkpoints ) const min = Math.min(...scores) const max = Math.max(...scores) // Count by tier const counts = { easy: 0, medium: 0, hard: 0 } for (const ex of internalExamples) { const score = ex.complexity.decisions + ex.complexity.checkpoints if (max === min) { counts.easy++ } else { const normalized = (score - min) / (max - min) if (normalized < 0.25) counts.easy++ else if (normalized < 0.75) counts.medium++ else counts.hard++ } } return counts }, [internalExamples, externalTierCounts]) const handleTabChange = useCallback((value: string) => { setActiveTab(value as ModalTab) }, []) return ( <Dialog.Content ref={ref} data-component="flowchart-modal" className={css({ position: 'fixed', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', display: 'flex', flexDirection: 'column', backgroundColor: { base: 'white', _dark: 'gray.800' }, borderRadius: '2xl', boxShadow: '2xl', width: '95vw', maxWidth: '600px', maxHeight: '90vh', overflow: 'hidden', zIndex: 101, _focus: { outline: 'none' }, })} > {/* Close button */} <div data-element="close-corner" className={css({ position: 'absolute', top: '-4px', right: '-4px', width: '44px', height: '44px', backgroundColor: { base: 'gray.100', _dark: 'gray.700' }, borderBottomLeftRadius: 'xl', display: 'flex', alignItems: 'center', justifyContent: 'center', paddingTop: '4px', paddingRight: '4px', zIndex: 20, })} > <Dialog.Close asChild> <button type="button" aria-label="Close" onClick={onClose} className={css({ width: '28px', height: '28px', borderRadius: 'md', backgroundColor: 'transparent', border: 'none', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: '14px', color: { base: 'gray.500', _dark: 'gray.400' }, transition: 'all 0.2s', _hover: { backgroundColor: { base: 'gray.200', _dark: 'gray.600' }, transform: 'scale(1.1)', }, })} > ✕ </button> </Dialog.Close> </div> {/* Title */} <div className={css({ padding: '6', paddingBottom: '4', borderBottom: '1px solid', borderColor: { base: 'gray.200', _dark: 'gray.700' }, })} > <Dialog.Title asChild> <h2 data-element="title" className={css({ fontSize: 'xl', fontWeight: 'bold', color: { base: 'gray.800', _dark: 'gray.100' }, textAlign: 'center', letterSpacing: '-0.01em', paddingRight: '10', // Space for close button })} > {flowchart.definition.title} </h2> </Dialog.Title> <Dialog.Description className={css({ srOnly: true })}> Practice, view flowchart diagram, or generate worksheets for{' '} {flowchart.definition.title} </Dialog.Description> {/* Action buttons - Edit/Remix */} {flowchartId && ( <div data-element="modal-actions" className={hstack({ gap: '2', justifyContent: 'center', marginTop: '3' })} > {source === 'database' && isOwnedByUser && ( <button data-action="edit" onClick={handleEdit} disabled={isCreatingWorkshop} className={css({ paddingY: '2', paddingX: '4', fontSize: 'sm', fontWeight: 'medium', borderRadius: 'md', border: 'none', cursor: 'pointer', transition: 'all 0.15s', backgroundColor: { base: 'blue.100', _dark: 'blue.900' }, color: { base: 'blue.700', _dark: 'blue.300' }, _hover: { backgroundColor: { base: 'blue.200', _dark: 'blue.800' }, }, _disabled: { opacity: 0.5, cursor: 'not-allowed', }, })} > {isCreatingWorkshop ? 'Opening...' : 'Edit'} </button> )} <button data-action="remix" onClick={handleRemix} disabled={isCreatingWorkshop} className={css({ paddingY: '2', paddingX: '4', fontSize: 'sm', fontWeight: 'medium', borderRadius: 'md', border: 'none', cursor: 'pointer', transition: 'all 0.15s', backgroundColor: { base: 'gray.100', _dark: 'gray.700' }, color: { base: 'gray.700', _dark: 'gray.300' }, _hover: { backgroundColor: { base: 'gray.200', _dark: 'gray.600' }, }, _disabled: { opacity: 0.5, cursor: 'not-allowed', }, })} > {isCreatingWorkshop ? 'Opening...' : 'Remix'} </button> </div> )} </div> {/* Diagnostic Alert */} {diagnosticReport && (diagnosticReport.errorCount > 0 || diagnosticReport.warningCount > 0) && ( <div className={css({ padding: '4', paddingTop: '0', paddingBottom: '0' })}> <div className={hstack({ gap: '2', alignItems: 'flex-start' })}> <div className={css({ flex: 1 })}> <DiagnosticAlert report={diagnosticReport} onShowDetails={() => setShowDiagnosticDetails(!showDiagnosticDetails)} compact /> </div> {/* Fix in Workshop button - only show for database flowcharts */} {flowchartId && source === 'database' && ( <button data-action="fix-in-workshop" onClick={handleFixInWorkshop} disabled={isCreatingWorkshop} className={css({ paddingY: '2', paddingX: '3', fontSize: 'sm', fontWeight: 'medium', borderRadius: 'md', backgroundColor: { base: 'blue.600', _dark: 'blue.500' }, color: 'white', border: 'none', cursor: 'pointer', whiteSpace: 'nowrap', transition: 'all 0.15s', _hover: { backgroundColor: { base: 'blue.700', _dark: 'blue.600' }, }, _disabled: { opacity: 0.6, cursor: 'not-allowed', }, })} > {isCreatingWorkshop ? 'Opening...' : isOwnedByUser ? 'Fix in Workshop' : 'Remix & Fix'} </button> )} </div> {showDiagnosticDetails && ( <div className={css({ marginTop: '3', maxHeight: '200px', overflow: 'auto', borderRadius: 'md', border: '1px solid', borderColor: { base: 'gray.200', _dark: 'gray.700' }, padding: '3', backgroundColor: { base: 'gray.50', _dark: 'gray.900' }, })} > <DiagnosticList report={diagnosticReport} maxItems={5} /> </div> )} </div> )} {/* Tabs */} <Tabs.Root value={activeTab} onValueChange={handleTabChange} className={css({ display: 'flex', flexDirection: 'column', flex: 1, overflow: 'hidden', })} > {/* Tab List */} <Tabs.List data-element="tab-list" className={css({ display: 'flex', gap: '1', padding: '2', paddingTop: '0', borderBottom: '1px solid', borderColor: { base: 'gray.200', _dark: 'gray.700' }, justifyContent: 'center', })} > <Tabs.Trigger value="practice" className={css({ paddingY: '2', paddingX: '4', fontSize: 'sm', fontWeight: 'medium', borderRadius: 'md', border: 'none', cursor: 'pointer', transition: 'all 0.15s', backgroundColor: 'transparent', color: { base: 'gray.600', _dark: 'gray.400' }, _hover: { backgroundColor: { base: 'gray.100', _dark: 'gray.700' }, color: { base: 'gray.900', _dark: 'gray.100' }, }, '&[data-state="active"]': { backgroundColor: { base: 'blue.50', _dark: 'blue.900/30' }, color: { base: 'blue.700', _dark: 'blue.300' }, }, })} > Practice </Tabs.Trigger> <Tabs.Trigger value="flowchart" className={css({ paddingY: '2', paddingX: '4', fontSize: 'sm', fontWeight: 'medium', borderRadius: 'md', border: 'none', cursor: 'pointer', transition: 'all 0.15s', backgroundColor: 'transparent', color: { base: 'gray.600', _dark: 'gray.400' }, _hover: { backgroundColor: { base: 'gray.100', _dark: 'gray.700' }, color: { base: 'gray.900', _dark: 'gray.100' }, }, '&[data-state="active"]': { backgroundColor: { base: 'blue.50', _dark: 'blue.900/30' }, color: { base: 'blue.700', _dark: 'blue.300' }, }, })} > Flowchart </Tabs.Trigger> <Tabs.Trigger value="worksheet" className={css({ paddingY: '2', paddingX: '4', fontSize: 'sm', fontWeight: 'medium', borderRadius: 'md', border: 'none', cursor: 'pointer', transition: 'all 0.15s', backgroundColor: 'transparent', color: { base: 'gray.600', _dark: 'gray.400' }, _hover: { backgroundColor: { base: 'gray.100', _dark: 'gray.700' }, color: { base: 'gray.900', _dark: 'gray.100' }, }, '&[data-state="active"]': { backgroundColor: { base: 'blue.50', _dark: 'blue.900/30' }, color: { base: 'blue.700', _dark: 'blue.300' }, }, })} > Worksheet </Tabs.Trigger> </Tabs.List> {/* Tab Content */} <div className={css({ flex: 1, overflow: 'auto', })} > <Tabs.Content value="practice" className={css({ padding: '4', outline: 'none', })} > <PracticeTab schema={flowchart.definition.problemInput} flowchart={flowchart} onSubmit={onSubmit} examples={internalExamples} onExamplesGenerated={handleExamplesGenerated} shareUrl={shareUrl} /> </Tabs.Content> <Tabs.Content value="flowchart" className={css({ padding: '4', outline: 'none', })} > <FlowchartTab flowchart={flowchart} shareUrl={shareUrl} /> </Tabs.Content> <Tabs.Content value="worksheet" className={css({ padding: '4', outline: 'none', })} > <WorksheetTab flowchart={flowchart} tierCounts={tierCounts} examples={internalExamples} flowchartId={flowchartId} /> </Tabs.Content> </div> </Tabs.Root> </Dialog.Content> ) } ) |