Spaces:
Sleeping
Sleeping
File size: 7,419 Bytes
be54038 140c7b5 be54038 140c7b5 be54038 140c7b5 be54038 140c7b5 7db0ced 140c7b5 be54038 140c7b5 be54038 140c7b5 be54038 140c7b5 be54038 140c7b5 be54038 140c7b5 | 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 | import { useMemo } from 'react'
import type { FieldEntry, GoldenRecord } from './types'
import { useStore } from './store'
import { FieldRow } from './FieldRow'
interface Props {
sessionId: string
}
const SECTION_LABELS: Record<string, string> = {
policy_header: 'Policy Header',
vehicle_details: 'Vehicle Details',
driver_details: 'Drivers',
cover_and_excesses: 'Cover & Excesses',
financial_summary: 'Financial Summary',
additional_risk_data: 'Additional Risk Data',
}
export function RecordPane({ sessionId }: Props) {
const sessionData = useStore((s) => s.sessionData)
const reviewState = useStore((s) => s.reviewState)
const activeFieldPath = useStore((s) => s.activeFieldPath)
const setActiveField = useStore((s) => s.setActiveField)
const fieldsBySection = useMemo(() => {
if (!sessionData) return []
return flattenRecord(sessionData.record, sessionData.provenance.reduce(
(acc, p) => { acc[p.field_path] = p; return acc },
{} as Record<string, import('./types').FieldProvenance>,
))
}, [sessionData])
if (!sessionData) return null
const conflicts = sessionData.conflicts ?? []
return (
<div className="flex flex-col h-full bg-paper">
{/* Header */}
<div className="px-5 h-[52px] flex items-center justify-between gap-3 border-b border-ink-700 bg-ink-800 flex-shrink-0">
<div className="min-w-0">
<h2 className="text-[14px] font-semibold text-ink-100 tracking-[-0.01em]">Golden Record</h2>
</div>
<p className="text-[12px] text-ink-400 truncate">Select a field to see its source in the PDF</p>
</div>
{/* Scrollable field list */}
<div className="flex-1 overflow-y-auto scroll-paper px-4 sm:px-5 py-5 space-y-7">
{conflicts.length > 0 && (
<section aria-label="Cross-document conflicts" className="rounded-xl border border-signal/50 bg-signal-tint px-4 py-3.5">
<p className="eyebrow text-signal-deep">
{conflicts.length} cross-document conflict{conflicts.length > 1 ? 's' : ''} · resolved by authority rules
</p>
<ul className="mt-2 space-y-2">
{conflicts.map((c) => (
<li key={c.field} className="text-[13px] text-paper-ink">
<span className="font-mono text-[12px] text-paper-muted">{c.field}</span>
<span className="block mt-0.5">
Winner: <span className="font-semibold capitalize">{c.winner}</span>
</span>
<span className="block mt-0.5 text-[12px] text-paper-muted">
Schedule: <span className="text-paper-ink">{c.schedule_value ?? '—'}</span>
{' · '}Certificate: <span className="text-paper-ink">{c.certificate_value ?? '—'}</span>
</span>
</li>
))}
</ul>
</section>
)}
{fieldsBySection.map(({ section, entries }) => (
<section key={section}>
<div className="flex items-baseline justify-between mb-2.5 px-1">
<h3 className="eyebrow text-signal-deep">{SECTION_LABELS[section] ?? section}</h3>
<span className="font-mono text-[11px] text-paper-muted">
{entries.filter((e) => e.provenance).length}/{entries.length} located
</span>
</div>
<div className="space-y-1.5">
{entries.map((entry) => (
<FieldRow
key={entry.fieldPath}
entry={entry}
sessionId={sessionId}
isActive={activeFieldPath === entry.fieldPath}
review={reviewState[entry.fieldPath]}
onClick={() =>
setActiveField(activeFieldPath === entry.fieldPath ? null : entry)
}
/>
))}
</div>
</section>
))}
</div>
</div>
)
}
// ── Field flattening helpers ───────────────────────────────────────────────
interface SectionGroup {
section: string
entries: FieldEntry[]
}
function flattenRecord(
record: GoldenRecord,
provenanceMap: Record<string, import('./types').FieldProvenance>,
): SectionGroup[] {
const groups: SectionGroup[] = []
for (const [sectionKey, sectionValue] of Object.entries(record)) {
if (sectionValue == null) continue
const entries: FieldEntry[] = []
if (Array.isArray(sectionValue)) {
// driver_details
sectionValue.forEach((item: Record<string, unknown>, idx: number) => {
walkObject(
item,
`${sectionKey}[${idx}]`,
`Driver ${idx + 1}`,
entries,
provenanceMap,
)
})
} else if (typeof sectionValue === 'object') {
walkObject(
sectionValue as Record<string, unknown>,
sectionKey,
'',
entries,
provenanceMap,
)
} else {
entries.push({
fieldPath: sectionKey,
label: formatLabel(sectionKey),
value: String(sectionValue),
section: sectionKey,
provenance: provenanceMap[sectionKey],
})
}
if (entries.length > 0) {
groups.push({ section: sectionKey, entries })
}
}
return groups
}
function walkObject(
obj: Record<string, unknown>,
pathPrefix: string,
_labelPrefix: string,
out: FieldEntry[],
provenanceMap: Record<string, import('./types').FieldProvenance>,
) {
for (const [key, val] of Object.entries(obj)) {
const path = `${pathPrefix}.${key}`
if (val == null) continue
if (typeof val === 'object' && !Array.isArray(val)) {
walkObject(val as Record<string, unknown>, path, key, out, provenanceMap)
} else if (Array.isArray(val)) {
val.forEach((item, i) => {
if (item == null) return
const iPath = `${path}[${i}]`
if (typeof item === 'object') {
walkObject(item as Record<string, unknown>, iPath, key, out, provenanceMap)
} else {
out.push({
fieldPath: iPath,
label: `${formatLabel(key)} [${i}]`,
value: String(item),
section: pathPrefix.split('.')[0],
provenance: provenanceMap[iPath],
})
}
})
} else {
out.push({
fieldPath: path,
label: driverPrefix(pathPrefix) + formatLabel(key),
value: String(val),
section: pathPrefix.split('.')[0],
provenance: provenanceMap[path],
})
}
}
}
const LABELS: Record<string, string> = {
vrm: 'Registration (VRM)',
dob: 'Date of Birth',
license_type: 'Licence Type',
is_main_driver: 'Main Driver',
has_security_device: 'Security Device',
overnight_postcode: 'Overnight Postcode',
standard_compulsory: 'Compulsory Excess',
total_accidental_damage: 'Accidental Damage Excess',
own_repairer_additional_excess: 'Own Repairer Excess',
years: 'NCD Years',
protected: 'NCD Protected',
}
function formatLabel(key: string): string {
return LABELS[key] ?? key
.replace(/_/g, ' ')
.replace(/\b\w/g, (c) => c.toUpperCase())
}
function driverPrefix(pathPrefix: string): string {
const m = pathPrefix.match(/^driver_details\[(\d+)\]$/)
return m ? `Driver ${Number(m[1]) + 1} · ` : ''
}
|