All files / web/src/components/toys/euclid/ledger LedgerEntry.tsx

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

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

/**
 * Individual entry in the construction log.
 *
 * Uses the same shared proof components as GuidedProofPanel:
 * StepIndicator for the numbered circle, ProofInstruction for entity-marked
 * text, and StepCitation for the citation block below the instruction.
 *
 * Double-click the instruction to edit it (re-annotated via markup API).
 */

import type React from 'react'
import { useState, useRef, useEffect } from 'react'
import { stripEntityMarkers } from '@/lib/character/parseEntityMarkers'
import { EUCLID_ENTITY_MARKERS } from '../euclidEntityMarkers'
import type { EuclidEntityRef } from '../chat/parseGeometricEntities'
import { StepIndicator } from '../proof/StepIndicator'
import { StepCitation } from '../proof/StepCitation'
import { ProofInstruction } from '../proof/ProofInstruction'
import { PROOF_COLORS, PROOF_FONTS, getProofFontSizes } from '../proof/styles'

interface LedgerEntryProps {
  index: number
  stepNumber?: number
  citation: string | null
  /** Progressive disclosure ordinal — passed to StepCitation */
  citationOrdinal?: number
  markedDescription: string
  isEditing: boolean
  isLoadingMarkup: boolean
  onStartEdit: () => void
  onCommitEdit: (text: string) => void
  onCancelEdit: () => void
  onRevert?: () => void
  renderEntity: (entity: EuclidEntityRef, displayText: string, index: number) => React.ReactNode
  onCitationPointerEnter?: (key: string, e: React.PointerEvent) => void
  onCitationPointerLeave?: () => void
  onCitationPointerDown?: (key: string, e: React.PointerEvent) => void
  isGiven?: boolean
  isMobile?: boolean
}

export function LedgerEntry({
  stepNumber,
  citation,
  citationOrdinal,
  markedDescription,
  isEditing,
  isLoadingMarkup,
  onStartEdit,
  onCommitEdit,
  onCancelEdit,
  onRevert,
  renderEntity,
  onCitationPointerEnter,
  onCitationPointerLeave,
  onCitationPointerDown,
  isGiven,
  isMobile,
}: LedgerEntryProps) {
  const [editText, setEditText] = useState('')
  const [isHovered, setIsHovered] = useState(false)
  const inputRef = useRef<HTMLInputElement>(null)
  const proofFont = getProofFontSizes(isMobile ?? false)

  useEffect(() => {
    if (isEditing) {
      // Initialize edit text from stripped markers
      setEditText(stripEntityMarkers(markedDescription, EUCLID_ENTITY_MARKERS))
      if (inputRef.current) {
        inputRef.current.focus()
        inputRef.current.select()
      }
    }
  }, [isEditing, markedDescription])

  const handleKeyDown = (e: React.KeyboardEvent) => {
    if (e.key === 'Enter') {
      e.preventDefault()
      onCommitEdit(editText)
    } else if (e.key === 'Escape') {
      e.preventDefault()
      onCancelEdit()
    }
  }

  // Given entries: italic text with [Given] badge, no step indicator
  if (isGiven) {
    return (
      <div
        data-element="ledger-entry"
        style={{
          padding: '3px 0',
          paddingLeft: 8,
          borderLeft: `2px solid ${PROOF_COLORS.factBorder}`,
          opacity: 0.8,
        }}
      >
        <span
          data-element="ledger-description"
          style={{
            fontSize: proofFont.stepText,
            fontFamily: PROOF_FONTS.serif,
            lineHeight: 1.4,
            color: PROOF_COLORS.textGiven,
            fontStyle: 'italic',
          }}
        >
          <ProofInstruction text={markedDescription} renderEntity={renderEntity} />
          <span
            style={{
              color: PROOF_COLORS.textMuted,
              fontFamily: PROOF_FONTS.serif,
              fontSize: proofFont.citation,
              fontWeight: 600,
              fontStyle: 'normal',
              marginLeft: 6,
            }}
          >
            [Given]
          </span>
        </span>
      </div>
    )
  }

  // Action entries: StepIndicator + instruction + citation (matches GuidedProofPanel layout)
  return (
    <div
      data-element="ledger-entry"
      onMouseEnter={() => setIsHovered(true)}
      onMouseLeave={() => setIsHovered(false)}
      style={{
        marginBottom: isMobile ? 8 : 12,
        borderRadius: 6,
        background: isHovered ? PROOF_COLORS.stepHoverBg : undefined,
        transition: 'background 0.15s ease',
      }}
    >
      <div style={{ display: 'flex', alignItems: 'flex-start', gap: 8 }}>
        {/* Step indicator — green done circle; click to rewind */}
        {stepNumber != null && (
          <StepIndicator
            state="done"
            stepNumber={stepNumber}
            isHovered={isHovered}
            onClick={onRevert}
            size={isMobile ? 18 : 20}
          />
        )}

        <div style={{ flex: 1, minWidth: 0 }}>
          {/* Instruction text (double-click to edit) */}
          <div
            data-element="ledger-description"
            onDoubleClick={isEditing ? undefined : onStartEdit}
            style={{
              fontSize: proofFont.stepTitle,
              fontFamily: PROOF_FONTS.serif,
              lineHeight: isMobile ? 1.25 : 1.4,
              color: PROOF_COLORS.text,
              cursor: 'pointer',
            }}
          >
            {isEditing ? (
              <span style={{ display: 'flex', alignItems: 'center', gap: 4 }}>
                <input
                  ref={inputRef}
                  data-element="ledger-edit-input"
                  type="text"
                  value={editText}
                  onChange={(e) => setEditText(e.target.value)}
                  onKeyDown={handleKeyDown}
                  onBlur={onCancelEdit}
                  style={{
                    flex: 1,
                    fontSize: proofFont.stepTitle,
                    fontFamily: PROOF_FONTS.serif,
                    border: '1px solid rgba(78, 121, 167, 0.3)',
                    borderRadius: 3,
                    padding: '2px 6px',
                    outline: 'none',
                    background: 'white',
                    color: PROOF_COLORS.text,
                  }}
                />
                {isLoadingMarkup && (
                  <span style={{ fontSize: 10, color: PROOF_COLORS.textMuted }}>...</span>
                )}
              </span>
            ) : (
              <ProofInstruction text={markedDescription} renderEntity={renderEntity} />
            )}
          </div>

          {/* Citation block below instruction */}
          {citation && (
            <StepCitation
              citationKey={citation}
              ordinal={citationOrdinal}
              color="#6b9b6b"
              fontSize={proofFont.stepText}
              citationFontSize={proofFont.citation}
              lineHeight={isMobile ? 1.25 : 1.4}
              onPointerEnter={onCitationPointerEnter}
              onPointerLeave={onCitationPointerLeave}
              onPointerDown={onCitationPointerDown}
              isMobile={isMobile}
            />
          )}
        </div>
      </div>
    </div>
  )
}