All files / web/src/components/flowchart TimeMachineHistory.tsx

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
'use client'

import { useRef, useEffect } from 'react'
import type { WorkingProblemHistoryEntry } from '@/lib/flowcharts/schema'
import { css, cx } from '../../../styled-system/css'
import { vstack } from '../../../styled-system/patterns'
import { MathDisplay } from './MathDisplay'

interface TimeMachineHistoryProps {
  /** History of working problem transformations */
  history: WorkingProblemHistoryEntry[]
  /** Callback when user clicks a history entry to rewind */
  onNavigate: (index: number) => void
  /** Font size for the current (hero) problem */
  fontSize?: 'lg' | 'xl'
}

/**
 * TimeMachineHistory - Apple Time Machine-style 3D perspective stack
 *
 * Displays the problem evolution as a stack of cards with the current
 * problem in front (hero) and past states fading into the background.
 *
 * - Current problem is prominent (full opacity, full scale)
 * - Previous problems are stacked behind with perspective depth
 * - Each layer: translateZ, scale down, fade opacity
 * - Clicking past layers triggers onNavigate to rewind
 */
export function TimeMachineHistory({
  history,
  onNavigate,
  fontSize = 'xl',
}: TimeMachineHistoryProps) {
  const containerRef = useRef<HTMLDivElement>(null)
  const latestEntryRef = useRef<HTMLDivElement>(null)

  // Scroll to show latest entry when history changes
  useEffect(() => {
    if (latestEntryRef.current) {
      latestEntryRef.current.scrollIntoView({
        behavior: 'smooth',
        block: 'nearest',
      })
    }
  }, [history.length])

  if (history.length === 0) {
    return null
  }

  // How many history items to show (limit to prevent overwhelming)
  const maxVisible = 5
  const visibleHistory = history.slice(-maxVisible)
  const hiddenCount = history.length - visibleHistory.length

  return (
    <div
      ref={containerRef}
      data-testid="time-machine-history"
      data-entry-count={history.length}
      data-visible-count={visibleHistory.length}
      className={css({
        position: 'relative',
        width: '100%',
        maxWidth: '600px',
        margin: '0 auto',
        perspective: '800px',
        perspectiveOrigin: 'center bottom',
        padding: '4',
      })}
    >
      {/* Stack container */}
      <div
        data-element="stack-container"
        className={vstack({
          gap: '3',
          alignItems: 'center',
          position: 'relative',
        })}
      >
        {/* Hidden count indicator */}
        {hiddenCount > 0 && (
          <div
            data-element="hidden-indicator"
            className={css({
              fontSize: 'xs',
              color: { base: 'gray.400', _dark: 'gray.500' },
              textAlign: 'center',
              paddingY: '1',
              paddingX: '2',
            })}
          >
            {hiddenCount} earlier step{hiddenCount > 1 ? 's' : ''} hidden
          </div>
        )}

        {/* History entries */}
        {visibleHistory.map((entry, visibleIdx) => {
          // Calculate actual index in full history
          const actualIdx = history.length - visibleHistory.length + visibleIdx
          const isLatest = actualIdx === history.length - 1
          // Depth from current: 0 for latest, 1 for previous, etc.
          const depth = visibleHistory.length - 1 - visibleIdx

          // 3D transform values based on depth
          const translateZ = -depth * 20 // Push back
          const scale = 1 - depth * 0.05 // Shrink slightly
          const opacity = 1 - depth * 0.15 // Fade

          return (
            <div
              key={`${actualIdx}-${entry.nodeId}`}
              ref={isLatest ? latestEntryRef : undefined}
              data-testid={`time-machine-entry-${actualIdx}`}
              data-entry-index={actualIdx}
              data-is-latest={isLatest}
              data-is-clickable={!isLatest}
              data-depth={depth}
              onClick={!isLatest ? () => onNavigate(actualIdx) : undefined}
              role={!isLatest ? 'button' : undefined}
              tabIndex={!isLatest ? 0 : undefined}
              onKeyDown={
                !isLatest
                  ? (e) => {
                      if (e.key === 'Enter' || e.key === ' ') {
                        e.preventDefault()
                        onNavigate(actualIdx)
                      }
                    }
                  : undefined
              }
              className={cx(
                css({
                  width: '100%',
                  padding: isLatest ? '4 5' : '3 4',
                  borderRadius: 'xl',
                  textAlign: 'center',
                  transformStyle: 'preserve-3d',
                  transition: 'all 0.4s cubic-bezier(0.4, 0, 0.2, 1)',
                  willChange: 'transform, opacity',

                  // Base styling
                  backgroundColor: isLatest
                    ? { base: 'blue.50', _dark: 'blue.900' }
                    : { base: 'gray.50', _dark: 'gray.800' },
                  border: isLatest ? '3px solid' : '2px solid',
                  borderColor: isLatest
                    ? { base: 'blue.400', _dark: 'blue.500' }
                    : { base: 'gray.200', _dark: 'gray.700' },
                  boxShadow: isLatest ? 'lg' : 'sm',

                  // Interactive states for non-latest entries
                  cursor: isLatest ? 'default' : 'pointer',
                  _hover: isLatest
                    ? {}
                    : {
                        opacity: '1 !important',
                        backgroundColor: { base: 'blue.50', _dark: 'blue.900/50' },
                        borderColor: { base: 'blue.300', _dark: 'blue.600' },
                        transform: 'translateZ(0) scale(1) !important',
                      },
                  _focusVisible: isLatest
                    ? {}
                    : {
                        outline: '2px solid',
                        outlineColor: { base: 'blue.500', _dark: 'blue.400' },
                        outlineOffset: '2px',
                      },
                }),
                // Animation class for new entries
                isLatest &&
                  css({
                    animation: 'timeMachineEnter 0.5s ease-out',
                  })
              )}
              style={{
                transform: `translateZ(${translateZ}px) scale(${scale})`,
                opacity,
                zIndex: visibleHistory.length - depth,
              }}
            >
              {/* Math expression */}
              <div
                data-element="problem-expression"
                className={css({
                  color: isLatest
                    ? { base: 'blue.900', _dark: 'blue.100' }
                    : { base: 'gray.700', _dark: 'gray.300' },
                })}
              >
                <MathDisplay
                  expression={entry.value}
                  size={isLatest ? fontSize : depth === 1 ? 'lg' : 'md'}
                />
              </div>

              {/* Step label - shown for all entries */}
              <div
                data-element="step-label"
                className={css({
                  fontSize: isLatest ? 'sm' : 'xs',
                  fontWeight: 'medium',
                  marginTop: '2',
                  color: isLatest
                    ? { base: 'blue.600', _dark: 'blue.400' }
                    : { base: 'gray.500', _dark: 'gray.500' },
                })}
              >
                {entry.label}
              </div>

              {/* Step number badge */}
              <div
                data-element="step-number"
                className={css({
                  position: 'absolute',
                  top: '-2',
                  left: '-2',
                  width: '6',
                  height: '6',
                  borderRadius: 'full',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                  fontSize: 'xs',
                  fontWeight: 'bold',
                  backgroundColor: isLatest
                    ? { base: 'blue.500', _dark: 'blue.500' }
                    : { base: 'gray.400', _dark: 'gray.600' },
                  color: 'white',
                  border: '2px solid',
                  borderColor: { base: 'white', _dark: 'gray.900' },
                })}
              >
                {actualIdx + 1}
              </div>

              {/* "Current" indicator for latest */}
              {isLatest && (
                <div
                  data-element="current-indicator"
                  className={css({
                    position: 'absolute',
                    top: '-2',
                    right: '-2',
                    padding: '0.5 2',
                    borderRadius: 'full',
                    fontSize: '2xs',
                    fontWeight: 'bold',
                    textTransform: 'uppercase',
                    letterSpacing: 'wide',
                    backgroundColor: { base: 'blue.500', _dark: 'blue.500' },
                    color: 'white',
                    border: '2px solid',
                    borderColor: { base: 'white', _dark: 'gray.900' },
                  })}
                >
                  Now
                </div>
              )}
            </div>
          )
        })}
      </div>
    </div>
  )
}