File size: 7,250 Bytes
be54038
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { type CSSProperties, useState } from 'react'
import type { FieldEntry, FieldReview } from './types'
import { useStore } from './store'

interface Props {
  entry: FieldEntry
  sessionId: string
  isActive: boolean
  review?: FieldReview
  onClick: () => void
}

export function FieldRow({ entry, sessionId, isActive, review, onClick }: Props) {
  const [editing, setEditing] = useState(false)
  const [editValue, setEditValue] = useState(entry.value ?? '')
  const verifyField = useStore((s) => s.verifyField)
  const overrideField = useStore((s) => s.overrideField)
  const rejectField = useStore((s) => s.rejectField)

  const displayValue = review?.action === 'override' && review.overridden_value != null
    ? review.overridden_value
    : entry.value

  const isVerified  = review?.action === 'verify'
  const isRejected  = review?.action === 'reject'
  const isOverridden = review?.action === 'override'

  const borderStyle: CSSProperties = isVerified
    ? { borderColor: '#16a34a', backgroundColor: '#f0fdf4' }
    : isRejected
    ? { borderColor: '#fca5a5', backgroundColor: '#fef2f2' }
    : isOverridden
    ? { borderColor: '#2563EB', backgroundColor: '#eff6ff' }
    : isActive
    ? { borderColor: '#008080', backgroundColor: '#f0fdfc' }
    : { borderColor: 'transparent', backgroundColor: '#ffffff' }

  const handleSaveOverride = async () => {
    await overrideField(sessionId, entry.fieldPath, editValue)
    setEditing(false)
  }

  return (
    <div
      className="rounded-lg border px-3 py-2 cursor-pointer transition-all hover:shadow-sm"
      style={borderStyle}
      onClick={onClick}
    >
      <div className="flex items-start gap-2">
        {/* Label + value */}
        <div className="flex-1 min-w-0">
          <div className="flex items-center gap-2 flex-wrap">
            <span className="text-xs font-semibold text-gray-500 shrink-0">
              {entry.label}
            </span>
            {isVerified && (
              <span className="inline-flex items-center gap-0.5 text-xs text-green-700 font-medium">
                <CheckIcon /> Verified
              </span>
            )}
            {isOverridden && (
              <span className="text-xs text-blue-700 font-medium">Overridden</span>
            )}
            {isRejected && (
              <span className="text-xs text-red-600 font-medium">Flagged</span>
            )}
          </div>

          {/* Value */}
          {editing ? (
            <div
              className="flex gap-2 mt-1"
              onClick={(e) => e.stopPropagation()}
            >
              <input
                autoFocus
                className="flex-1 text-xs border rounded px-2 py-1 focus:outline-none focus:ring-1 focus:ring-blue-400"
                value={editValue}
                onChange={(e) => setEditValue(e.target.value)}
                onKeyDown={(e) => {
                  if (e.key === 'Enter') handleSaveOverride()
                  if (e.key === 'Escape') setEditing(false)
                }}
              />
              <button
                onClick={handleSaveOverride}
                className="text-xs px-2 py-1 bg-blue-600 text-white rounded hover:bg-blue-700"
              >
                Save
              </button>
              <button
                onClick={() => setEditing(false)}
                className="text-xs px-2 py-1 bg-gray-200 text-gray-700 rounded hover:bg-gray-300"
              >
                Cancel
              </button>
            </div>
          ) : (
            <p className="text-sm text-gray-800 mt-0.5 truncate">
              {displayValue ?? (
                <span className="text-gray-300 italic">Not extracted</span>
              )}
            </p>
          )}

          {/* Provenance source hint — or explicit "no location" notice */}
          {!editing && (
            entry.provenance ? (
              <p className="text-xs text-gray-400 mt-0.5 truncate">
                {entry.provenance.source_filename} · p.{entry.provenance.location.page} ·{' '}
                <span className="italic">"{entry.provenance.matched_text.slice(0, 60)}{entry.provenance.matched_text.length > 60 ? '…' : ''}"</span>
              </p>
            ) : (
              <p className="text-xs mt-0.5">
                <span className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded bg-gray-100 text-gray-400 font-medium">
                  <span aria-hidden></span> No location data
                </span>
              </p>
            )
          )}
        </div>

        {/* Right side: confidence badge + action buttons */}
        <div
          className="flex items-center gap-1 flex-shrink-0"
          onClick={(e) => e.stopPropagation()}
        >
          {entry.provenance && (
            <ConfidenceBadge score={entry.provenance.match_score} />
          )}

          {/* Verify */}
          <button
            title="Mark as verified"
            onClick={() => verifyField(sessionId, entry.fieldPath)}
            className={`w-7 h-7 rounded flex items-center justify-center text-sm transition-colors ${
              isVerified
                ? 'bg-green-500 text-white'
                : 'bg-gray-100 text-gray-500 hover:bg-green-100 hover:text-green-700'
            }`}
          >

          </button>

          {/* Edit */}
          <button
            title="Override value"
            onClick={() => {
              setEditValue(displayValue ?? '')
              setEditing(true)
            }}
            className="w-7 h-7 rounded flex items-center justify-center text-sm transition-colors"
            style={{ backgroundColor: '#f3f4f6', color: '#6b7280' }}
            onMouseEnter={e => { (e.currentTarget as HTMLElement).style.backgroundColor = '#eff6ff'; (e.currentTarget as HTMLElement).style.color = '#2563EB' }}
            onMouseLeave={e => { (e.currentTarget as HTMLElement).style.backgroundColor = '#f3f4f6'; (e.currentTarget as HTMLElement).style.color = '#6b7280' }}
          >

          </button>

          {/* Flag */}
          <button
            title="Flag for review"
            onClick={() => rejectField(sessionId, entry.fieldPath)}
            className={`w-7 h-7 rounded flex items-center justify-center text-sm transition-colors ${
              isRejected
                ? 'bg-red-500 text-white'
                : 'bg-gray-100 text-gray-500 hover:bg-red-100 hover:text-red-600'
            }`}
          >

          </button>
        </div>
      </div>
    </div>
  )
}

function ConfidenceBadge({ score }: { score: number }) {
  const pct = Math.round(score * 100)
  const [bg, text] =
    pct >= 90
      ? ['bg-green-100 text-green-700', '']
      : pct >= 70
      ? ['bg-yellow-100 text-yellow-700', '']
      : ['bg-red-100 text-red-600', '']

  return (
    <span className={`text-xs font-mono px-1.5 py-0.5 rounded ${bg} ${text}`}>
      {pct}%
    </span>
  )
}

function CheckIcon() {
  return (
    <svg className="w-3 h-3" viewBox="0 0 12 12" fill="currentColor">
      <path d="M10 3L5 8.5 2 5.5" stroke="currentColor" strokeWidth="1.5"
        strokeLinecap="round" strokeLinejoin="round" fill="none" />
    </svg>
  )
}