import React, { useRef, useState } from 'react'; import { checkLine } from '../api/client'; import { LineCheckResponse, TradeDirection } from '../api/types'; import { ErrorText, PrimaryButton, SecondaryButton, TextInput, Field, Select } from './controls'; export function CodeCheckDialog({ open, onClose, }: { open: boolean; onClose: () => void; }) { const [code, setCode] = useState(''); const [direction, setDirection] = useState(TradeDirection.IMPORT); const [originCountry, setOriginCountry] = useState(''); const [destinationCountry, setDestinationCountry] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [result, setResult] = useState(null); const dialogRef = useRef(null); // Sync the dialog open/close with the prop React.useEffect(() => { const el = dialogRef.current; if (!el) return; if (open && !el.open) el.showModal(); if (!open && el.open) el.close(); }, [open]); const reset = () => { setCode(''); setDirection(TradeDirection.IMPORT); setOriginCountry(''); setDestinationCountry(''); setError(null); setResult(null); }; const handleClose = () => { reset(); onClose(); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!code.trim()) return; setLoading(true); setError(null); setResult(null); try { const res = await checkLine({ code: code.trim(), direction, originCountry: originCountry.trim() || null, destinationCountry: destinationCountry.trim() || null, }); setResult(res); } catch (err: any) { setError(err?.message ?? 'Request failed'); } finally { setLoading(false); } }; if (!open) return null; return (

Quick code check

setCode(e.target.value)} placeholder="e.g. 6203 42 31" autoFocus />
setOriginCountry(e.target.value)} placeholder="e.g. CN" /> setDestinationCountry(e.target.value)} placeholder="e.g. NL" />
{loading ? 'Checking...' : 'Check'} Close
{error ? {error} : null}
{result ? : null}
); } const STATUS_LABELS: Record = { '1': 'OK', '2': 'Ambiguous', '3': 'Invalid', '4': 'Expiring', }; const CONFIDENCE_LABELS: Record = { '1': 'Red', '2': 'Amber', '3': 'Green', }; const CONFIDENCE_COLORS: Record = { '1': 'text-confidence-red', '2': 'text-confidence-amber', '3': 'text-green-600', }; function CodeCheckResult({ result }: { result: LineCheckResponse }) { return (
Input code {result.inputCode} Resolved code {result.formattedCode ?? result.resolvedCode ?? '-'} Description {result.codeDescription ?? '-'} Status {STATUS_LABELS[String(result.status)] ?? result.status} Code confidence {CONFIDENCE_LABELS[String(result.codeConfidence)] ?? result.codeConfidence} {result.codeConfidenceReason ? ` — ${result.codeConfidenceReason}` : ''} Document confidence {CONFIDENCE_LABELS[String(result.documentConfidence)] ?? result.documentConfidence} {result.documentConfidenceReason ? ` — ${result.documentConfidenceReason}` : ''} Checked on {result.checkedOn}
{result.warnings.length > 0 ? (
Warnings ({result.warnings.length})
    {result.warnings.map((w, i) => (
  • = 2 ? 'text-confidence-amber' : ''}> {w.message}
  • ))}
) : null} {result.documentsRequired.length > 0 ? (
Required documents ({result.documentsRequired.length})
    {result.documentsRequired.map((d, i) => (
  • {d.code} {d.description ? ` — ${d.description}` : ''}
  • ))}
) : null}
); }