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

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

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

/**
 * ParsingProgressPanel - Collapsible panel showing AI reasoning text
 *
 * Displays the LLM's thinking process during worksheet parsing.
 * Shows below the photo tile, with smooth expand/collapse animation.
 */

import { useRef, useEffect } from 'react'
import { css } from '../../../styled-system/css'

export interface ParsingProgressPanelProps {
  /** Whether the panel is expanded */
  isExpanded: boolean
  /** The reasoning text from the LLM */
  reasoningText: string
  /** Current parsing status (from StreamingStatus) */
  status:
    | 'idle'
    | 'connecting'
    | 'reasoning'
    | 'processing'
    | 'generating'
    | 'complete'
    | 'error'
    | 'cancelled'
  /** Dark mode */
  isDark?: boolean
}

/**
 * Get status indicator text
 */
function getStatusLabel(status: ParsingProgressPanelProps['status']): string {
  switch (status) {
    case 'connecting':
      return 'Connecting...'
    case 'reasoning':
      return 'AI is thinking...'
    case 'generating':
      return 'Generating results...'
    case 'complete':
      return 'Complete'
    case 'error':
      return 'Error'
    default:
      return ''
  }
}

export function ParsingProgressPanel({
  isExpanded,
  reasoningText,
  status,
  isDark = false,
}: ParsingProgressPanelProps) {
  const contentRef = useRef<HTMLDivElement>(null)

  // Auto-scroll to bottom as new text arrives
  useEffect(() => {
    if (isExpanded && contentRef.current) {
      contentRef.current.scrollTop = contentRef.current.scrollHeight
    }
  }, [reasoningText, isExpanded])

  const statusLabel = getStatusLabel(status)
  const isActive = status === 'connecting' || status === 'reasoning' || status === 'generating'

  if (!isExpanded) {
    return null
  }

  return (
    <div
      data-component="parsing-progress-panel"
      className={css({
        marginTop: '0.5rem',
        borderRadius: '8px',
        backgroundColor: isDark ? 'gray.800' : 'gray.50',
        border: '1px solid',
        borderColor: isDark ? 'gray.700' : 'gray.200',
        overflow: 'hidden',
        animation: 'fadeIn 0.2s ease-out forwards',
      })}
    >
      {/* Header */}
      <div
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: '0.5rem',
          padding: '0.5rem 0.75rem',
          backgroundColor: isDark ? 'gray.750' : 'gray.100',
          borderBottom: '1px solid',
          borderColor: isDark ? 'gray.700' : 'gray.200',
        })}
      >
        {/* Pulsing dot for active state */}
        {isActive && (
          <span
            className={css({
              width: '8px',
              height: '8px',
              borderRadius: 'full',
              backgroundColor: 'blue.500',
              animation: 'pulseOpacity 1.5s ease-in-out infinite',
            })}
          />
        )}
        {status === 'complete' && (
          <span
            className={css({
              width: '8px',
              height: '8px',
              borderRadius: 'full',
              backgroundColor: 'green.500',
            })}
          />
        )}
        {status === 'error' && (
          <span
            className={css({
              width: '8px',
              height: '8px',
              borderRadius: 'full',
              backgroundColor: 'red.500',
            })}
          />
        )}
        <span
          className={css({
            fontSize: '0.75rem',
            fontWeight: 'medium',
            color: isDark ? 'gray.300' : 'gray.600',
          })}
        >
          {statusLabel}
        </span>
      </div>

      {/* Content */}
      <div
        ref={contentRef}
        className={css({
          padding: '0.75rem',
          maxHeight: '150px',
          overflowY: 'auto',
          // Responsive height
          '@media (min-width: 768px)': {
            maxHeight: '250px',
          },
        })}
      >
        {reasoningText ? (
          <p
            className={css({
              fontSize: '0.8125rem',
              lineHeight: '1.5',
              color: isDark ? 'gray.300' : 'gray.700',
              fontFamily: 'sans-serif',
              whiteSpace: 'pre-wrap',
              margin: 0,
            })}
          >
            {reasoningText}
          </p>
        ) : (
          <p
            className={css({
              fontSize: '0.8125rem',
              color: isDark ? 'gray.500' : 'gray.400',
              fontStyle: 'italic',
              margin: 0,
            })}
          >
            Waiting for AI response...
          </p>
        )}
      </div>
    </div>
  )
}