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 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 | 'use client' import * as Popover from '@radix-ui/react-popover' import * as Tooltip from '@radix-ui/react-tooltip' import { useMemo, useState } from 'react' import { calculateEstimatedTimeRemainingMs, formatEstimatedTimeRemaining, type TimingStats, } from '@/hooks/useSessionTimeEstimate' import { calculateStreak } from '@/lib/calculateStreak' import { css } from '../../../styled-system/css' import { useIsTouchDevice } from './hooks/useDeviceDetection' import { SpeedMeter } from './SpeedMeter' export interface SessionMoodIndicatorProps { /** Current elapsed time on this problem in ms */ currentElapsedMs: number /** Mean response time for this part type */ meanMs: number /** Standard deviation of response times */ stdDevMs: number /** Threshold for "too slow" */ thresholdMs: number /** Whether we have enough data for stats */ hasEnoughData: boolean /** Number of problems remaining */ problemsRemaining: number /** Total problems in session */ totalProblems: number /** Recent results for accuracy display (last N) */ recentResults: boolean[] /** Overall accuracy (0-1) */ accuracy: number /** Session health status */ healthStatus: 'good' | 'warning' | 'struggling' /** Is session paused */ isPaused: boolean /** Dark mode */ isDark: boolean } /** * Get streak message based on length */ function getStreakMessage(streak: number): { text: string; fires: string } { if (streak >= 10) return { text: 'LEGENDARY!', fires: 'π₯π₯π₯π₯' } if (streak >= 7) return { text: 'Unstoppable!', fires: 'π₯π₯π₯' } if (streak >= 5) return { text: "You're on fire!", fires: 'π₯π₯' } if (streak >= 3) return { text: 'Nice streak!', fires: 'π₯' } return { text: '', fires: '' } } /** * Determine the mood based on current state */ function getMood(props: SessionMoodIndicatorProps): { emoji: string label: string description: string } { const { currentElapsedMs, meanMs, stdDevMs, hasEnoughData, healthStatus, isPaused, recentResults, } = props // Check for streak const streak = calculateStreak(recentResults) const lastFiveCorrect = recentResults.slice(-5) const recentAccuracy = lastFiveCorrect.length > 0 ? lastFiveCorrect.filter(Boolean).length / lastFiveCorrect.length : 1 // Paused state if (isPaused) { return { emoji: 'βΈοΈ', label: 'Paused', description: "Taking a break - that's fine!", } } // Calculate how slow they are on current problem const slowThreshold = hasEnoughData ? meanMs + stdDevMs : 15000 const verySlowThreshold = hasEnoughData ? meanMs + 2 * stdDevMs : 30000 const isSlowOnCurrent = currentElapsedMs > slowThreshold const isVerySlowOnCurrent = currentElapsedMs > verySlowThreshold // Very stuck if (isVerySlowOnCurrent) { return { emoji: 'π€', label: 'Thinking hard', description: "This one's tricky! Take your time.", } } // On fire - fast and accurate with streak if (streak >= 3 && healthStatus === 'good' && !isSlowOnCurrent) { return { emoji: 'π₯', label: 'On fire!', description: "You're crushing it!", } } // Good overall if (healthStatus === 'good' && !isSlowOnCurrent) { return { emoji: 'π', label: 'Cruising', description: "You're doing great!", } } // Bit slow but accurate if (isSlowOnCurrent && recentAccuracy >= 0.7) { return { emoji: 'π’', label: 'Slow and steady', description: 'Taking your time - accuracy is good!', } } // Warning - some mistakes or slow if (healthStatus === 'warning') { return { emoji: 'π', label: 'Hanging in there', description: 'Keep going, you got this!', } } // Struggling if (healthStatus === 'struggling') { return { emoji: 'πͺ', label: 'Tough stretch', description: "It's hard right now, but you're learning!", } } // Default return { emoji: 'π', label: 'Doing well', description: 'Keep it up!', } } /** * Format milliseconds as kid-friendly time */ function formatTimeKid(ms: number): string { const seconds = Math.floor(ms / 1000) const minutes = Math.floor(seconds / 60) const remainingSeconds = seconds % 60 if (minutes >= 1) { return `${minutes}m ${remainingSeconds}s` } return `${seconds}s` } // formatEstimatedTimeRemaining is imported from useSessionTimeEstimate // Animation names (defined in GlobalStyles below) const ANIM = { pulse: 'streak-pulse', glow: 'streak-glow', shake: 'streak-shake', rainbow: 'streak-rainbow', } /** * Global styles for streak animations * Injected once into the document */ function GlobalStreakStyles() { return ( <style>{` @keyframes ${ANIM.pulse} { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } } @keyframes ${ANIM.glow} { 0%, 100% { box-shadow: 0 0 5px rgba(251, 146, 60, 0.5); } 50% { box-shadow: 0 0 20px rgba(251, 146, 60, 0.8), 0 0 30px rgba(251, 146, 60, 0.4); } } @keyframes ${ANIM.shake} { 0%, 100% { transform: translateX(0) rotate(0deg); } 25% { transform: translateX(-2px) rotate(-2deg); } 75% { transform: translateX(2px) rotate(2deg); } } @keyframes ${ANIM.rainbow} { 0% { filter: hue-rotate(0deg); } 100% { filter: hue-rotate(360deg); } } `}</style> ) } /** * Streak Display Component with animations */ function StreakDisplay({ streak, isDark }: { streak: number; isDark: boolean }) { if (streak < 2) return null const { text, fires } = getStreakMessage(streak) // Animation intensity based on streak const animationDuration = Math.max(0.3, 1 - streak * 0.08) // Faster as streak grows const isLegendary = streak >= 10 const isUnstoppable = streak >= 7 const isOnFire = streak >= 5 return ( <div data-element="streak-display" data-streak={streak} className={css({ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: '0.5rem', padding: '0.5rem 0.75rem', marginTop: '0.5rem', borderRadius: '8px', backgroundColor: isDark ? 'orange.900' : 'orange.50', border: '2px solid', borderColor: isDark ? 'orange.700' : 'orange.200', })} style={{ animation: isLegendary ? `${ANIM.glow} ${animationDuration}s ease-in-out infinite, ${ANIM.shake} ${animationDuration * 0.5}s ease-in-out infinite` : isOnFire ? `${ANIM.glow} ${animationDuration}s ease-in-out infinite` : undefined, }} > {/* Fire emojis with animation */} <span className={css({ fontSize: isLegendary ? '1.5rem' : isUnstoppable ? '1.25rem' : '1rem', })} style={{ animation: streak >= 3 ? `${ANIM.pulse} ${animationDuration}s ease-in-out infinite` : undefined, }} > <span style={{ animation: isLegendary ? `${ANIM.rainbow} 2s linear infinite` : undefined, display: 'inline-block', }} > {fires} </span> </span> {/* Streak count and message */} <div className={css({ textAlign: 'center' })}> <div className={css({ fontSize: isLegendary ? '1.25rem' : '1rem', fontWeight: 'bold', color: isDark ? 'orange.300' : 'orange.600', })} style={{ animation: isUnstoppable ? `${ANIM.pulse} ${animationDuration}s ease-in-out infinite` : undefined, }} > {streak} in a row! </div> {text && ( <div className={css({ fontSize: '0.75rem', fontWeight: '600', color: isDark ? 'orange.400' : 'orange.500', textTransform: 'uppercase', letterSpacing: '0.05em', })} > {text} </div> )} </div> {/* Fire emojis on the right too for symmetry on big streaks */} {isOnFire && ( <span className={css({ fontSize: isLegendary ? '1.5rem' : isUnstoppable ? '1.25rem' : '1rem', })} style={{ animation: `${ANIM.pulse} ${animationDuration}s ease-in-out infinite`, animationDelay: `${animationDuration / 2}s`, }} > <span style={{ animation: isLegendary ? `${ANIM.rainbow} 2s linear infinite` : undefined, animationDelay: '1s', display: 'inline-block', }} > {fires} </span> </span> )} </div> ) } /** * The tooltip/popover content */ function MoodContent({ props, mood, streak, lastFive, correctCount, estimatedTimeMs, }: { props: SessionMoodIndicatorProps mood: { emoji: string; label: string; description: string } streak: number lastFive: boolean[] correctCount: number estimatedTimeMs: number }) { const { currentElapsedMs, meanMs, stdDevMs, thresholdMs, hasEnoughData, isDark } = props return ( <div className={css({ padding: '1rem', backgroundColor: isDark ? 'gray.800' : 'white', borderRadius: '12px', border: '1px solid', borderColor: isDark ? 'gray.700' : 'gray.200', boxShadow: 'lg', minWidth: '280px', maxWidth: '320px', })} > {/* Header with mood */} <div className={css({ display: 'flex', alignItems: 'center', gap: '0.75rem', marginBottom: '1rem', paddingBottom: '0.75rem', borderBottom: '1px solid', borderColor: isDark ? 'gray.700' : 'gray.200', })} > <span className={css({ fontSize: '2.5rem' })}>{mood.emoji}</span> <div> <div className={css({ fontSize: '1.125rem', fontWeight: 'bold', color: isDark ? 'gray.100' : 'gray.900', })} > {mood.label} </div> <div className={css({ fontSize: '0.875rem', color: isDark ? 'gray.400' : 'gray.600', })} > {mood.description} </div> </div> </div> {/* Time Remaining */} <div data-section="time-remaining" className={css({ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: '1rem', padding: '0.75rem', backgroundColor: isDark ? 'gray.900' : 'gray.50', borderRadius: '8px', })} > <div> <div className={css({ fontSize: '0.75rem', color: isDark ? 'gray.500' : 'gray.500', marginBottom: '0.125rem', })} > Time left </div> <div className={css({ fontSize: '1.25rem', fontWeight: 'bold', color: isDark ? 'gray.100' : 'gray.900', })} > {formatEstimatedTimeRemaining(estimatedTimeMs)} </div> </div> <div className={css({ fontSize: '2rem', opacity: 0.5, })} > β° </div> </div> {/* Current Problem Speed */} <div data-section="current-speed" className={css({ marginBottom: '1rem', padding: '0.75rem', backgroundColor: isDark ? 'gray.900' : 'gray.50', borderRadius: '8px', })} > <div className={css({ fontSize: '0.75rem', color: isDark ? 'gray.500' : 'gray.500', marginBottom: '0.5rem', })} > This problem </div> <div className={css({ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: '0.5rem', })} > <span className={css({ fontSize: '1.5rem', fontWeight: 'bold', fontFamily: 'monospace', color: isDark ? 'gray.100' : 'gray.900', })} > {formatTimeKid(currentElapsedMs)} </span> {hasEnoughData && ( <span className={css({ fontSize: '0.875rem', color: isDark ? 'gray.400' : 'gray.600', })} > (you usually take ~{formatTimeKid(meanMs)}) </span> )} </div> {/* Speed Meter */} {hasEnoughData && ( <SpeedMeter meanMs={meanMs} stdDevMs={stdDevMs} thresholdMs={thresholdMs} currentTimeMs={currentElapsedMs} isDark={isDark} compact={false} /> )} </div> {/* Accuracy - Last 5 with Streak */} <div data-section="accuracy" className={css({ padding: '0.75rem', backgroundColor: isDark ? 'gray.900' : 'gray.50', borderRadius: '8px', })} > <div className={css({ fontSize: '0.75rem', color: isDark ? 'gray.500' : 'gray.500', marginBottom: '0.5rem', })} > Last 5 answers </div> <div className={css({ display: 'flex', alignItems: 'center', gap: '0.5rem', })} > {/* Dots for last 5 */} <div className={css({ display: 'flex', gap: '0.375rem', })} > {lastFive.length === 0 ? ( <span className={css({ fontSize: '0.875rem', color: isDark ? 'gray.500' : 'gray.400', fontStyle: 'italic', })} > No answers yet </span> ) : ( lastFive.map((correct, i) => { // Is this dot part of the current streak? const isInStreak = correct && i >= lastFive.length - Math.min(streak, lastFive.length) const streakPosition = isInStreak ? lastFive.length - i : 0 return ( <span key={i} className={css({ width: '24px', height: '24px', borderRadius: '50%', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: '0.875rem', fontWeight: 'bold', backgroundColor: correct ? isDark ? 'green.900' : 'green.100' : isDark ? 'red.900' : 'red.100', color: correct ? isDark ? 'green.300' : 'green.700' : isDark ? 'red.300' : 'red.700', border: '2px solid', borderColor: correct ? isDark ? 'green.700' : 'green.300' : isDark ? 'red.700' : 'red.300', transition: 'all 0.2s ease', })} style={ isInStreak && streak >= 3 ? { animation: `${ANIM.pulse} ${Math.max(0.4, 0.8 - streakPosition * 0.1)}s ease-in-out infinite`, animationDelay: `${streakPosition * 0.1}s`, boxShadow: streak >= 5 ? '0 0 8px rgba(34, 197, 94, 0.6)' : '0 0 4px rgba(34, 197, 94, 0.4)', } : undefined } > {correct ? 'β' : 'β'} </span> ) }) )} </div> {/* Count */} {lastFive.length > 0 && ( <span className={css({ fontSize: '0.875rem', fontWeight: '600', color: correctCount >= 4 ? isDark ? 'green.400' : 'green.600' : correctCount >= 2 ? isDark ? 'yellow.400' : 'yellow.600' : isDark ? 'red.400' : 'red.600', })} > {correctCount} right! </span> )} </div> {/* Streak Display */} <StreakDisplay streak={streak} isDark={isDark} /> </div> </div> ) } /** * Session Mood Indicator * * A big emoji that synthesizes speed + accuracy into an at-a-glance mood. * Tooltip (desktop) or Popover (touch) reveals the detailed data in a kid-friendly layout. */ export function SessionMoodIndicator(props: SessionMoodIndicatorProps) { const { problemsRemaining, recentResults, isDark, meanMs, stdDevMs, thresholdMs, hasEnoughData } = props const isTouchDevice = useIsTouchDevice() const [popoverOpen, setPopoverOpen] = useState(false) const mood = getMood(props) // Use shared time estimation function const timingStats: TimingStats = { mean: meanMs, stdDev: stdDevMs, count: hasEnoughData ? 5 : 0, // We don't have exact count, but hasEnoughData tells us if >= 5 hasEnoughData, threshold: thresholdMs, } const estimatedTimeMs = calculateEstimatedTimeRemainingMs(timingStats, problemsRemaining) // Calculate streak const streak = useMemo(() => calculateStreak(recentResults), [recentResults]) // Last 5 results for dot display const lastFive = recentResults.slice(-5) const correctCount = lastFive.filter(Boolean).length // Trigger button styles const triggerClassName = css({ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '0.25rem', padding: '0.5rem 0.75rem', backgroundColor: isDark ? 'gray.800' : 'gray.100', borderRadius: '12px', border: '2px solid', borderColor: isDark ? 'gray.700' : 'gray.200', cursor: 'pointer', transition: 'all 0.15s ease', _hover: { backgroundColor: isDark ? 'gray.700' : 'gray.200', borderColor: isDark ? 'gray.600' : 'gray.300', transform: 'scale(1.02)', }, }) // Animation for streak on the main button const buttonAnimation = streak >= 10 ? `${ANIM.glow} 0.5s ease-in-out infinite` : streak >= 5 ? `${ANIM.glow} 0.8s ease-in-out infinite` : undefined const TriggerButton = ( <button type="button" data-element="session-mood-indicator" data-streak={streak} className={triggerClassName} style={{ animation: buttonAnimation, borderColor: streak >= 5 ? (isDark ? 'orange.600' : 'orange.300') : undefined, }} > {/* Big emoji */} <span className={css({ fontSize: '2rem', lineHeight: 1, })} style={{ animation: streak >= 3 ? `${ANIM.pulse} ${Math.max(0.5, 1 - streak * 0.05)}s ease-in-out infinite` : undefined, }} > {mood.emoji} </span> {/* Problems left + time estimate + streak indicator */} <span className={css({ fontSize: '0.75rem', fontWeight: '600', color: isDark ? 'gray.300' : 'gray.600', whiteSpace: 'nowrap', display: 'flex', alignItems: 'center', gap: '0.25rem', })} > {problemsRemaining} left Β· ~{Math.max(1, Math.round(estimatedTimeMs / 60000))}m {streak >= 3 && ( <span style={{ animation: `${ANIM.pulse} ${Math.max(0.4, 0.8 - streak * 0.05)}s ease-in-out infinite`, }} > π₯ </span> )} </span> </button> ) const contentProps = { props, mood, streak, lastFive, correctCount, estimatedTimeMs, } // Use Popover for touch devices, Tooltip for desktop if (isTouchDevice) { return ( <> <GlobalStreakStyles /> <Popover.Root open={popoverOpen} onOpenChange={setPopoverOpen}> <Popover.Trigger asChild>{TriggerButton}</Popover.Trigger> <Popover.Portal> <Popover.Content side="bottom" sideOffset={8} className={css({ zIndex: 1000, outline: 'none' })} > <MoodContent {...contentProps} /> <Popover.Arrow className={css({ fill: isDark ? 'gray.800' : 'white', })} /> </Popover.Content> </Popover.Portal> </Popover.Root> </> ) } return ( <> <GlobalStreakStyles /> <Tooltip.Provider delayDuration={200}> <Tooltip.Root> <Tooltip.Trigger asChild>{TriggerButton}</Tooltip.Trigger> <Tooltip.Portal> <Tooltip.Content side="bottom" sideOffset={8} className={css({ zIndex: 1000 })}> <MoodContent {...contentProps} /> <Tooltip.Arrow className={css({ fill: isDark ? 'gray.800' : 'white', })} /> </Tooltip.Content> </Tooltip.Portal> </Tooltip.Root> </Tooltip.Provider> </> ) } |