All files / web/src/components/practice AssistanceDebugPanel.tsx

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

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

import { useCallback, useState } from 'react'
import { shouldUseDebugTiming } from '@/constants/helpTiming'
import { useVisualDebugSafe } from '@/contexts/VisualDebugContext'
import { css } from '../../../styled-system/css'
import { DebugMermaidDiagram } from '../flowchart/DebugMermaidDiagram'
import type {
  AssistanceMachineState,
  AssistanceEventLogEntry,
} from './hooks/useProgressiveAssistance'

interface AssistanceDebugPanelProps {
  machineState: AssistanceMachineState
}

const STATE_DIAGRAM_MERMAID = `stateDiagram-v2
    [*] --> idle
    idle --> encouraging: TIMER_ENCOURAGEMENT
    idle --> offeringHelp: WRONG_ANSWER×3
    idle --> inHelp: HELP_ENTERED
    encouraging --> offeringHelp: TIMER_HELP_OFFER
    encouraging --> idle: DIGIT_TYPED
    encouraging --> inHelp: HELP_ENTERED
    offeringHelp --> autoPaused: TIMER_AUTO_PAUSE
    offeringHelp --> idle: DIGIT_TYPED
    offeringHelp --> inHelp: HELP_ENTERED
    autoPaused --> idle: RESUMED
    inHelp --> idle: HELP_EXITED
    inHelp --> idle: DIGIT_TYPED`

const STATE_COLORS: Record<string, string> = {
  idle: '#94a3b8',
  encouraging: '#60a5fa',
  offeringHelp: '#f59e0b',
  autoPaused: '#ef4444',
  inHelp: '#a78bfa',
}

function formatTimestamp(ts: number): string {
  const d = new Date(ts)
  return `${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}:${d.getSeconds().toString().padStart(2, '0')}.${d.getMilliseconds().toString().padStart(3, '0')}`
}

function formatElapsed(ms: number): string {
  if (ms < 1000) return `${Math.round(ms)}ms`
  return `${(ms / 1000).toFixed(1)}s`
}

export function AssistanceDebugPanel({ machineState }: AssistanceDebugPanelProps) {
  const { isVisualDebugEnabled } = useVisualDebugSafe()
  const [isCollapsed, setIsCollapsed] = useState(false)
  const [debugTiming, setDebugTiming] = useState(shouldUseDebugTiming)

  const toggleDebugTiming = useCallback(() => {
    const next = !debugTiming
    setDebugTiming(next)
    try {
      if (next) {
        localStorage.setItem('helpDebugTiming', 'true')
      } else {
        localStorage.removeItem('helpDebugTiming')
      }
    } catch {
      /* ignore */
    }
    // Reload so timing config takes effect
    window.location.reload()
  }, [debugTiming])

  if (!isVisualDebugEnabled) return null

  const { state, context } = machineState
  const idleElapsed = Date.now() - context.idleStartedAt
  const stateColor = STATE_COLORS[state] || '#94a3b8'

  return (
    <div
      data-component="assistance-debug-panel"
      className={css({
        position: 'fixed',
        bottom: '1rem',
        left: '1rem',
        zIndex: 9999,
        backgroundColor: 'rgba(15, 23, 42, 0.95)',
        color: 'gray.200',
        borderRadius: '12px',
        border: '1px solid',
        borderColor: 'gray.700',
        fontSize: '0.6875rem',
        fontFamily: 'monospace',
        maxWidth: '380px',
        maxHeight: isCollapsed ? 'auto' : '500px',
        overflow: 'hidden',
        boxShadow: '0 4px 20px rgba(0,0,0,0.4)',
      })}
    >
      {/* Header */}
      <button
        type="button"
        onClick={() => setIsCollapsed(!isCollapsed)}
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: '0.5rem',
          width: '100%',
          padding: '0.5rem 0.75rem',
          backgroundColor: 'transparent',
          border: 'none',
          borderBottom: isCollapsed ? 'none' : '1px solid',
          borderColor: 'gray.700',
          cursor: 'pointer',
          color: 'gray.200',
          fontSize: '0.6875rem',
          fontFamily: 'monospace',
          textAlign: 'left',
        })}
      >
        <span>{isCollapsed ? '▶' : '▼'}</span>
        <span style={{ fontWeight: 600 }}>Assistance</span>
        <span
          style={{
            backgroundColor: stateColor,
            color: '#0f172a',
            padding: '1px 6px',
            borderRadius: '4px',
            fontWeight: 700,
            fontSize: '0.625rem',
          }}
        >
          {state}
        </span>
      </button>

      {!isCollapsed && (
        <div
          className={css({
            overflow: 'auto',
            maxHeight: '460px',
          })}
        >
          {/* State Diagram */}
          <div
            className={css({
              padding: '0.5rem',
              borderBottom: '1px solid',
              borderColor: 'gray.700',
            })}
          >
            <div
              className={css({ fontSize: '0.625rem', color: 'gray.500', marginBottom: '0.25rem' })}
            >
              STATE DIAGRAM
            </div>
            <div className={css({ maxHeight: '180px', overflow: 'auto' })}>
              <DebugMermaidDiagram mermaidContent={STATE_DIAGRAM_MERMAID} currentNodeId={state} />
            </div>
          </div>

          {/* Context Inspector */}
          <div
            className={css({
              padding: '0.5rem 0.75rem',
              borderBottom: '1px solid',
              borderColor: 'gray.700',
            })}
          >
            <div
              className={css({ fontSize: '0.625rem', color: 'gray.500', marginBottom: '0.25rem' })}
            >
              CONTEXT
            </div>
            <table className={css({ width: '100%', borderCollapse: 'collapse' })}>
              <tbody>
                <ContextRow
                  label="Wrong attempts"
                  value={`${context.wrongAttemptCount} / ${context.wrongAnswerThreshold}`}
                />
                <ContextRow
                  label="Helped terms"
                  value={`{${[...context.helpedTermIndices].join(', ')}} (${context.helpedTermIndices.size})`}
                />
                <ContextRow
                  label="Move on"
                  value={
                    context.moveOnAvailable
                      ? 'available'
                      : context.moveOnGraceStartedAt
                        ? `grace: ${formatElapsed(Date.now() - context.moveOnGraceStartedAt)} / ${formatElapsed(context.moveOnGraceMs)}`
                        : 'locked'
                  }
                />
                <ContextRow label="Idle elapsed" value={formatElapsed(idleElapsed)} />
                <ContextRow
                  label="Thresholds"
                  value={`enc: ${formatElapsed(context.thresholds.encouragementMs)} | help: ${formatElapsed(context.thresholds.helpOfferMs)} | pause: ${formatElapsed(context.thresholds.autoPauseMs)}`}
                />
              </tbody>
            </table>

            {/* Debug timing toggle */}
            <label
              className={css({
                display: 'flex',
                alignItems: 'center',
                gap: '0.375rem',
                marginTop: '0.375rem',
                cursor: 'pointer',
                fontSize: '0.625rem',
                color: 'gray.400',
              })}
            >
              <input
                type="checkbox"
                checked={debugTiming}
                onChange={toggleDebugTiming}
                className={css({ cursor: 'pointer' })}
              />
              Fast timing (reloads)
            </label>

            {/* Threshold progress bar */}
            <div
              className={css({
                marginTop: '0.375rem',
                height: '6px',
                borderRadius: '3px',
                backgroundColor: 'gray.800',
                position: 'relative',
                overflow: 'hidden',
              })}
            >
              {/* Green segment: 0 → encouragement */}
              <div
                style={{
                  position: 'absolute',
                  left: 0,
                  top: 0,
                  height: '100%',
                  width: `${Math.min(100, (context.thresholds.encouragementMs / context.thresholds.autoPauseMs) * 100)}%`,
                  backgroundColor: '#22c55e',
                  opacity: 0.3,
                }}
              />
              {/* Yellow segment: encouragement → helpOffer */}
              <div
                style={{
                  position: 'absolute',
                  left: `${(context.thresholds.encouragementMs / context.thresholds.autoPauseMs) * 100}%`,
                  top: 0,
                  height: '100%',
                  width: `${((context.thresholds.helpOfferMs - context.thresholds.encouragementMs) / context.thresholds.autoPauseMs) * 100}%`,
                  backgroundColor: '#f59e0b',
                  opacity: 0.3,
                }}
              />
              {/* Red segment: helpOffer → autoPause */}
              <div
                style={{
                  position: 'absolute',
                  left: `${(context.thresholds.helpOfferMs / context.thresholds.autoPauseMs) * 100}%`,
                  top: 0,
                  height: '100%',
                  width: `${((context.thresholds.autoPauseMs - context.thresholds.helpOfferMs) / context.thresholds.autoPauseMs) * 100}%`,
                  backgroundColor: '#ef4444',
                  opacity: 0.3,
                }}
              />
              {/* Progress indicator */}
              <div
                style={{
                  position: 'absolute',
                  left: `${Math.min(100, (idleElapsed / context.thresholds.autoPauseMs) * 100)}%`,
                  top: 0,
                  width: '2px',
                  height: '100%',
                  backgroundColor: 'white',
                  transition: 'left 0.5s linear',
                }}
              />
            </div>
          </div>

          {/* Event Log */}
          <div className={css({ padding: '0.5rem 0.75rem' })}>
            <div
              className={css({ fontSize: '0.625rem', color: 'gray.500', marginBottom: '0.25rem' })}
            >
              EVENT LOG ({context.eventLog.length})
            </div>
            <div
              className={css({
                maxHeight: '120px',
                overflow: 'auto',
              })}
            >
              {context.eventLog.length === 0 ? (
                <div className={css({ color: 'gray.600', fontStyle: 'italic' })}>No events yet</div>
              ) : (
                context.eventLog.map((entry, i) => (
                  <EventLogRow key={`${entry.timestamp}-${i}`} entry={entry} />
                ))
              )}
            </div>
          </div>
        </div>
      )}
    </div>
  )
}

function ContextRow({ label, value }: { label: string; value: string }) {
  return (
    <tr>
      <td
        className={css({
          color: 'gray.500',
          paddingRight: '0.5rem',
          paddingY: '1px',
          whiteSpace: 'nowrap',
        })}
      >
        {label}
      </td>
      <td className={css({ color: 'gray.200', paddingY: '1px' })}>{value}</td>
    </tr>
  )
}

function EventLogRow({ entry }: { entry: AssistanceEventLogEntry }) {
  const transitionChanged = entry.fromState !== entry.toState

  return (
    <div
      className={css({
        display: 'flex',
        gap: '0.5rem',
        paddingY: '1px',
        fontSize: '0.625rem',
        lineHeight: 1.4,
        borderBottom: '1px solid',
        borderColor: 'gray.800',
      })}
    >
      <span className={css({ color: 'gray.600', whiteSpace: 'nowrap' })}>
        {formatTimestamp(entry.timestamp)}
      </span>
      <span className={css({ color: 'cyan.400', minWidth: '100px' })}>{entry.event}</span>
      <span className={css({ color: transitionChanged ? 'yellow.400' : 'gray.600' })}>
        {entry.fromState} → {entry.toState}
      </span>
      {entry.note && <span className={css({ color: 'gray.500' })}>({entry.note})</span>}
    </div>
  )
}