import React, { useState } from 'react'
import { api } from '../api.js'
const LEVEL_COLOR = { HIGH: '#c92a2a', MEDIUM: '#d9750b', LOW: '#5b6478' }
function RiskBadge({ risk }) {
return (
{risk.title} ({risk.severity})
)
}
function Side({ side }) {
if (!side.clauses.length && !side.risks.length) {
return Not present
}
return (
{side.clauses.map((c, i) => (
{c.heading || `Clause ${c.number || c.id}`}
))}
{side.risks.map((r, i) =>
)}
)
}
export default function ContractCompare({ currentId, currentFilename, existing, onClose }) {
const [otherId, setOtherId] = useState('')
const [result, setResult] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const others = existing.filter((c) => c.contract_id !== currentId)
async function runCompare() {
if (!otherId) return
setLoading(true)
setError(null)
setResult(null)
try {
const res = await api(`/api/compare?a=${currentId}&b=${otherId}`)
if (!res.ok) {
const body = await res.json().catch(() => ({}))
throw new Error(body.detail || `Compare failed (${res.status})`)
}
setResult(await res.json())
} catch (e) {
setError(e.message)
} finally {
setLoading(false)
}
}
return (
Contract Comparison
✕ Close
A
{currentFilename}
vs
B
setOtherId(e.target.value)}
>
Select contract…
{others.map((c) => (
{c.filename}
))}
{loading ? 'Comparing…' : 'Compare →'}
{others.length === 0 && (
Upload at least one other contract to compare against.
)}
{error &&
⚠ {error}
}
{result && (
{result.summary.both} shared categories
{result.summary.only_a} only in A
{result.summary.only_b} only in B
{result.summary.risk_increased > 0 && (
↑ {result.summary.risk_increased} higher risk in B
)}
{result.summary.risk_decreased > 0 && (
↓ {result.summary.risk_decreased} lower risk in B
)}
Category
A — {result.a.filename}
B — {result.b.filename}
Δ Risk
{result.rows.map((row, i) => (
{row.label}
{row.status !== 'both' && (
{row.status === 'only_a' ? 'A only' : 'B only'}
)}
{row.risk_delta != null ? (
0
? 'delta-worse'
: row.risk_delta < 0
? 'delta-better'
: 'delta-same'
}
>
{row.risk_delta > 0 ? `+${row.risk_delta}` : row.risk_delta}
) : (
—
)}
))}
)}
)
}