Collaborative-Fraud-Intelligence-Simulator / frontend /src /components /cases /CrossBankIntelligenceWorkspace.tsx
yusufcalisir's picture
deploy: Hugging Face space upload
b2e19c9
Raw History Blame Contribute Delete
47.4 kB
import { useState, useEffect, useId, FC, ChangeEvent } from 'react';
import {
ShieldAlert,
ShieldCheck,
AlertTriangle,
CheckCircle2,
XCircle,
Copy,
Check,
Clock,
Layers,
Database,
Lock,
RefreshCw,
Search,
} from 'lucide-react';
import {
useCaseEligibleEntities,
useCaseIntelligenceEvidence,
useQueryMultiCounterpartyIntelligence,
useAttachMultiCounterpartyEvidence,
useCrossBankQueryCapabilities,
} from '../../api/queries';
import {
MultiCounterpartyIntelligenceQueryResponse,
CrossBankMultiEvidenceAttachResponse,
} from '../../api/types';
import { formatApiError } from '../../api/client';
export interface CrossBankIntelligenceWorkspaceProps {
caseId: string;
bankId?: string | null;
assignedTo?: string | null;
isOpen?: boolean;
}
const formatPurposeLabel = (purp: string): string => {
return purp
.replace(/_/g, ' ')
.toLowerCase()
.replace(/\b\w/g, (ch) => ch.toUpperCase());
};
export const CrossBankIntelligenceWorkspace: FC<CrossBankIntelligenceWorkspaceProps> = ({
caseId,
bankId,
assignedTo: _assignedTo = 'analyst',
}) => {
const [selectedEntityId, setSelectedEntityId] = useState<string>('');
const [selectedConsortium, setSelectedConsortium] = useState<string>('');
const [selectedTargets, setSelectedTargets] = useState<string[] | null>(null);
const [purpose, setPurpose] = useState<string>('');
const [analystNotes, setAnalystNotes] = useState<string>('');
const [queryError, setQueryError] = useState<string | null>(null);
const [attachError, setAttachError] = useState<string | null>(null);
const [copiedHash, setCopiedHash] = useState<string | null>(null);
// Stable idempotency keys across retries (Gate D)
const [queryIdempotencyKey, setQueryIdempotencyKey] = useState<string>(() => `query_${crypto.randomUUID()}`);
const [attachIdempotencyKey, setAttachIdempotencyKey] = useState<string>(() => `attach_${crypto.randomUUID()}`);
const [queryResponse, setQueryResponse] = useState<MultiCounterpartyIntelligenceQueryResponse | null>(null);
const [attachResponse, setAttachResponse] = useState<CrossBankMultiEvidenceAttachResponse | null>(null);
useEffect(() => {
const resetWorkspaceOnSessionChange = () => {
setSelectedEntityId('');
setSelectedConsortium('');
setSelectedTargets(null);
setPurpose('');
setAnalystNotes('');
setQueryError(null);
setAttachError(null);
setQueryResponse(null);
setAttachResponse(null);
setQueryIdempotencyKey(`query_${crypto.randomUUID()}`);
setAttachIdempotencyKey(`attach_${crypto.randomUUID()}`);
};
window.addEventListener('cfi-auth-changed', resetWorkspaceOnSessionChange);
window.addEventListener('storage', resetWorkspaceOnSessionChange);
return () => {
window.removeEventListener('cfi-auth-changed', resetWorkspaceOnSessionChange);
window.removeEventListener('storage', resetWorkspaceOnSessionChange);
};
}, []);
// Queries & Mutations
const {
data: rawEligibleEntities,
isLoading: isLoadingEntities,
error: entitiesError,
refetch: refetchEntities,
} = useCaseEligibleEntities(caseId);
const eligibleEntities = Array.isArray(rawEligibleEntities) ? rawEligibleEntities : [];
const {
data: rawPersistedEvidence,
isLoading: isLoadingEvidence,
refetch: refetchEvidence,
} = useCaseIntelligenceEvidence(caseId);
const persistedEvidence = Array.isArray(rawPersistedEvidence) ? rawPersistedEvidence : [];
// Gate C: Query Capabilities from server-authoritative bilateral grants (Fail-Closed)
const {
data: capabilitiesData,
isLoading: isLoadingCapabilities,
error: capabilitiesError,
refetch: refetchCapabilities,
} = useCrossBankQueryCapabilities(bankId ?? undefined);
const queryMultiMutation = useQueryMultiCounterpartyIntelligence();
const attachMultiMutation = useAttachMultiCounterpartyEvidence();
const notesInputId = useId();
// Active consortia from query capabilities (strictly server-authoritative, zero fallbacks)
const activeConsortia = (capabilitiesData?.consortia || []).filter(
(item) => item.status.toLowerCase() === 'active',
);
const effectiveConsortiumId =
selectedConsortium && activeConsortia.some((item) => item.consortium_id === selectedConsortium)
? selectedConsortium
: (activeConsortia[0]?.consortium_id ?? '');
const currentConsortium = activeConsortia.find(
(item) => item.consortium_id === effectiveConsortiumId,
);
const availableCounterparts = currentConsortium?.counterparts || [];
const authorizedCounterparts = availableCounterparts.filter((cp) => cp.is_authorized);
// Permitted purposes strictly derived from active consortium capability
const permittedPurposes = currentConsortium?.permitted_purposes || [];
const effectivePurpose =
purpose && permittedPurposes.includes(purpose)
? purpose
: (permittedPurposes[0] ?? '');
const authorizedCounterpartIds = authorizedCounterparts.map((cp) => cp.counterpart_bank_id);
// Valid targets strictly restricted to authorized counterparts (defaults to all authorized if not explicitly toggled)
const validSelectedTargets =
selectedTargets !== null
? selectedTargets.filter((t) => authorizedCounterpartIds.includes(t))
: authorizedCounterpartIds;
// Auto-select first entity when loaded if none selected
const activeEntityId =
selectedEntityId || (eligibleEntities.length > 0 ? (eligibleEntities[0]?.entity_id ?? '') : '');
const handleTargetBankToggle = (bankKey: string) => {
setQueryResponse(null);
setQueryError(null);
setAttachResponse(null);
setAttachError(null);
setQueryIdempotencyKey(`query_${crypto.randomUUID()}`);
setSelectedTargets((prev) => {
const current = prev !== null ? prev : authorizedCounterpartIds;
return current.includes(bankKey) ? current.filter((b) => b !== bankKey) : [...current, bankKey];
});
};
const handleEntityChange = (newEid: string) => {
setSelectedEntityId(newEid);
setQueryResponse(null);
setQueryError(null);
setAttachResponse(null);
setAttachError(null);
setQueryIdempotencyKey(`query_${crypto.randomUUID()}`);
};
const handleConsortiumChange = (newCid: string) => {
setSelectedConsortium(newCid);
setSelectedTargets(null);
setPurpose('');
setQueryResponse(null);
setQueryError(null);
setAttachResponse(null);
setAttachError(null);
setQueryIdempotencyKey(`query_${crypto.randomUUID()}`);
};
const handlePurposeChange = (newPurp: string) => {
setPurpose(newPurp);
setQueryResponse(null);
setQueryError(null);
setAttachResponse(null);
setAttachError(null);
setQueryIdempotencyKey(`query_${crypto.randomUUID()}`);
};
const handleCopy = (val: string, id: string) => {
navigator.clipboard.writeText(val);
setCopiedHash(id);
setTimeout(() => setCopiedHash(null), 2000);
};
// Fail-Closed Dispatch Eligibility: requires valid bank, entity, consortium, authorized targets, and purpose
const canDispatch = Boolean(
bankId &&
bankId.trim() !== '' &&
activeEntityId &&
effectiveConsortiumId &&
validSelectedTargets.length > 0 &&
effectivePurpose &&
!isLoadingCapabilities &&
!capabilitiesError &&
!isLoadingEntities &&
!queryMultiMutation.isPending
);
const handleDispatchQuery = async () => {
if (!canDispatch || !bankId) return;
setQueryError(null);
setAttachResponse(null);
const cleanBankId = bankId.toLowerCase().trim();
try {
const resp = await queryMultiMutation.mutateAsync({
requesting_bank_id: cleanBankId,
query_entity_id: activeEntityId,
consortium_id: effectiveConsortiumId,
target_bank_ids: validSelectedTargets,
case_id: caseId,
purpose: effectivePurpose,
execution_mode: 'VERIFIED_OPERATIONAL',
use_established_psi: true,
require_established_psi: true,
require_hardware_attestation: false,
idempotency_key: queryIdempotencyKey,
});
setQueryResponse(resp);
// Reset attachment idempotency key for newly received query batch
setAttachIdempotencyKey(`attach_${crypto.randomUUID()}`);
} catch (err: unknown) {
setQueryError(formatApiError(err, 'Multi-counterparty intelligence query execution failed.'));
}
};
const handleAttachEvidence = async () => {
if (!queryResponse || !caseId) return;
setAttachError(null);
try {
const resp = await attachMultiMutation.mutateAsync({
query_id: queryResponse.query_id,
case_id: caseId,
notes: analystNotes.trim() || undefined,
idempotency_key: attachIdempotencyKey,
});
setAttachResponse(resp);
refetchEvidence();
} catch (err: unknown) {
setAttachError(formatApiError(err, 'Failed to assemble batch evidence into case dossier.'));
}
};
// Gate D: Allow attaching verified results (including negative matches NO_MATCH)
const hasEligibleSignals =
queryResponse &&
queryResponse.results &&
Object.keys(queryResponse.results).length > 0;
return (
<div
className="p-4 sm:p-6 rounded-2xl bg-gradient-to-br from-slate-50 via-slate-50 to-indigo-50 border border-indigo-200 backdrop-blur-md shadow-2xl space-y-6"
data-testid="cross-bank-intelligence-workspace"
>
{/* Workspace Header */}
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 pb-4 border-b border-indigo-200">
<div className="flex items-center gap-3">
<div className="p-2.5 rounded-xl bg-indigo-600/20 border border-indigo-200 text-indigo-600">
<ShieldAlert className="w-5 h-5" />
</div>
<div>
<div className="flex items-center gap-2">
<h2 className="text-base sm:text-lg font-bold text-slate-800 tracking-wide">
Cross-Bank Consortium Intelligence Workspace
</h2>
<span className="px-2 py-0.5 rounded text-[10px] font-mono font-bold bg-indigo-100 text-indigo-700 border border-indigo-200">
CROSS-BANK VERIFIED
</span>
</div>
<p className="text-xs text-slate-500 mt-0.5">
Multi-Counterparty Private Set Intersection (PSI) & Authoritative Case Evidence Assembly
</p>
</div>
</div>
<div className="flex items-center gap-2 flex-wrap">
<span className="px-2.5 py-1 rounded-lg text-xs font-mono font-bold bg-slate-100 text-slate-600 border border-slate-200">
TENANT: {bankId ? bankId.toUpperCase() : 'UNAUTHENTICATED'}
</span>
<span className="px-2.5 py-1 rounded-lg text-xs font-mono font-bold bg-emerald-50 text-emerald-700 border border-emerald-500/30 flex items-center gap-1.5">
<span className="w-2 h-2 rounded-full bg-emerald-400 animate-pulse" />
OPERATIONAL ASSURANCE
</span>
<span className="px-2.5 py-1 rounded-lg text-xs font-mono text-slate-500 bg-slate-50 border border-slate-200 flex items-center gap-1">
<Clock className="w-3.5 h-3.5 text-amber-600" />
24h Bounded Lifecycle
</span>
</div>
</div>
{/* Tenant Context Warning if unauthenticated / missing bankId */}
{(!bankId || bankId.trim() === '') && (
<div className="p-3.5 rounded-xl bg-amber-50 border border-amber-500/40 text-amber-700 text-xs flex items-center gap-2.5" data-testid="unauthenticated-warning">
<AlertTriangle className="w-4 h-4 text-amber-600 shrink-0" />
<span>
Tenant Context Required: An authenticated institution bank ID is required to query cross-bank intelligence and view authorized capabilities.
</span>
</div>
)}
{/* Grid: Left Column (Query Builder) & Right Column (Execution / Persisted Trail) */}
<div className="grid grid-cols-1 lg:grid-cols-12 gap-6">
{/* Sub-Panel 1: Query Builder (5 Cols) */}
<div className="lg:col-span-5 space-y-4">
<div className="p-4 rounded-xl bg-slate-50 border border-indigo-200 space-y-4">
<div className="flex items-center justify-between">
<h3 className="text-xs font-bold uppercase tracking-wider text-slate-600 flex items-center gap-2">
<Database className="w-4 h-4 text-indigo-600" />
Authorized Query Builder
</h3>
<div className="flex items-center gap-2">
<button
type="button"
onClick={() => {
refetchEntities();
refetchCapabilities();
}}
disabled={isLoadingEntities || isLoadingCapabilities}
className="text-[11px] font-mono text-indigo-600 hover:text-indigo-700 flex items-center gap-1 transition"
title="Refresh authorized case entities and consortium capabilities"
>
<RefreshCw className={`w-3 h-3 ${(isLoadingEntities || isLoadingCapabilities) ? 'animate-spin' : ''}`} />
Refresh
</button>
</div>
</div>
{/* Entity Selector (Strictly alert-linked entities only; no arbitrary inventory probing) */}
<div className="space-y-1.5">
<label htmlFor="eligible-entity-select" className="text-xs font-semibold text-slate-600">
Suspect Investigation Entity <span className="text-rose-600">*</span>
</label>
{isLoadingEntities ? (
<div className="p-2.5 rounded-lg bg-white text-xs font-mono text-slate-500 border border-slate-200 animate-pulse">
Loading authorized case entities...
</div>
) : entitiesError ? (
<div className="p-2.5 rounded-lg bg-rose-50 border border-rose-300 text-rose-700 text-xs font-mono">
Failed to load entities: {formatApiError(entitiesError)}
</div>
) : eligibleEntities.length === 0 ? (
<div className="p-3 rounded-lg bg-amber-50 border border-amber-500/30 text-amber-700 text-xs leading-relaxed">
No institution-owned entities currently linked to this case. Please link alerts or ensure bank customer/merchant entities exist.
</div>
) : (
<select
id="eligible-entity-select"
value={activeEntityId}
onChange={(e) => handleEntityChange(e.target.value)}
className="w-full px-3 py-2 rounded-lg bg-white border border-slate-200 text-xs font-mono text-slate-800 focus:outline-none focus:border-indigo-500"
data-testid="case-entity-select"
>
{eligibleEntities.map((ent) => (
<option key={ent.entity_id} value={ent.entity_id}>
{ent.display_label} ({ent.entity_type.toUpperCase()} · Risk: {ent.risk_level.toUpperCase()}
{ent.source_alert_id ? ` · Alert: ${ent.source_alert_id.slice(-6)}` : ''})
</option>
))}
</select>
)}
<p className="text-[10px] text-slate-500">
Authoritative local institution-owned entity derived directly from case alert ledger.
</p>
</div>
{/* Consortium Selector (Derived strictly from server-authoritative capabilities) */}
<div className="space-y-1.5">
<label htmlFor="consortium-select" className="text-xs font-semibold text-slate-600">
Governing Consortium <span className="text-rose-600">*</span>
</label>
{isLoadingCapabilities ? (
<div className="p-2.5 rounded-lg bg-white text-xs font-mono text-slate-500 border border-slate-200 animate-pulse">
Loading authorized consortium capabilities...
</div>
) : capabilitiesError ? (
<div className="p-2.5 rounded-lg bg-rose-50 border border-rose-300 text-rose-700 text-xs font-mono">
Failed to load capabilities: {formatApiError(capabilitiesError)}
</div>
) : activeConsortia.length === 0 ? (
<div className="p-3 rounded-lg bg-amber-50 border border-amber-500/30 text-amber-700 text-xs leading-relaxed" data-testid="empty-consortia-callout">
No verified consortium participation or authorized bilateral counterparts configured for this institution.
</div>
) : (
<select
id="consortium-select"
value={effectiveConsortiumId}
onChange={(e) => handleConsortiumChange(e.target.value)}
className="w-full px-3 py-2 rounded-lg bg-white border border-slate-200 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
data-testid="consortium-select"
>
{activeConsortia.map((item) => (
<option key={item.consortium_id} value={item.consortium_id}>
{item.name} ({item.status.toUpperCase()})
</option>
))}
</select>
)}
</div>
{/* Target Counterparty Selection (Derived strictly from real pairwise grants) */}
<div className="space-y-1.5">
<label className="text-xs font-semibold text-slate-600">
Target Counterparty Institutions <span className="text-rose-600">*</span>
</label>
{availableCounterparts.length === 0 ? (
<div className="p-3 rounded-lg bg-slate-50 border border-slate-200 text-slate-500 text-xs font-mono" data-testid="empty-counterparts-callout">
No counterpart institutions available in this consortium.
</div>
) : (
<div className="grid grid-cols-2 sm:grid-cols-3 gap-2">
{availableCounterparts.map((cp) => {
const bankKey = cp.counterpart_bank_id;
const isChecked = validSelectedTargets.includes(bankKey);
const isAuth = cp.is_authorized;
return (
<button
type="button"
key={bankKey}
onClick={() => isAuth && handleTargetBankToggle(bankKey)}
disabled={!isAuth}
className={`px-3 py-2 rounded-lg text-xs font-mono font-bold transition flex flex-col justify-between border text-left ${
isChecked
? 'bg-indigo-600/30 border-indigo-500 text-indigo-700'
: isAuth
? 'bg-slate-50 border-slate-200 text-slate-500 hover:border-slate-200'
: 'bg-slate-50 border-slate-200 text-slate-600 cursor-not-allowed opacity-60'
}`}
data-testid={`target-toggle-${bankKey}`}
>
<div className="flex items-center justify-between w-full">
<span>{cp.display_name || bankKey.toUpperCase()}</span>
{isChecked ? <Check className="w-3.5 h-3.5 text-indigo-600" /> : null}
</div>
<span className={`text-[9px] mt-1 font-mono ${isAuth ? (cp.execution_readiness === 'OPERATIONAL' ? 'text-emerald-600' : 'text-amber-600') : 'text-slate-500'}`}>
{isAuth ? cp.execution_readiness : cp.governance_status}
</span>
</button>
);
})}
</div>
)}
<p className="text-[10px] text-slate-500">
Private set intersection is evaluated separately and privately per counterpart institution.
</p>
</div>
{/* Purpose Selector (Derived strictly from consortium authorized purposes) */}
<div className="space-y-1.5">
<label htmlFor="purpose-select" className="text-xs font-semibold text-slate-600">
Regulatory & Investigative Purpose <span className="text-rose-600">*</span>
</label>
{permittedPurposes.length === 0 ? (
<div className="p-3 rounded-lg bg-slate-50 border border-slate-200 text-slate-500 text-xs font-mono" data-testid="empty-purposes-callout">
No regulatory purposes authorized for this consortium.
</div>
) : (
<select
id="purpose-select"
value={effectivePurpose}
onChange={(e) => handlePurposeChange(e.target.value)}
className="w-full px-3 py-2 rounded-lg bg-white border border-slate-200 text-xs text-slate-800 focus:outline-none focus:border-indigo-500"
data-testid="purpose-select"
>
{permittedPurposes.map((p) => (
<option key={p} value={p}>
{formatPurposeLabel(p)}
</option>
))}
</select>
)}
</div>
{/* Assurance Guarantees Indicator */}
<div className="p-2.5 rounded-lg bg-indigo-50 border border-indigo-200 text-[11px] text-indigo-700 space-y-1">
<div className="flex items-center gap-1.5 font-bold">
<Lock className="w-3.5 h-3.5 text-indigo-600" />
Operational Assurance Guarantees
</div>
<p className="text-[10px] text-slate-500 leading-normal">
Strict multi-party Diffie-Hellman / ECDH PSI protocol. Counterparties execute isolated queries without learning raw query identifiers. Anonymous counterparty masking enforced by default policy.
</p>
</div>
{/* Query Dispatch Error */}
{queryError && (
<div className="p-3 rounded-lg bg-rose-50 border border-rose-300 text-rose-700 text-xs flex items-start gap-2">
<AlertTriangle className="w-4 h-4 text-rose-600 shrink-0 mt-0.5" />
<div className="break-all">{queryError}</div>
</div>
)}
{/* Dispatch Action */}
<button
type="button"
onClick={handleDispatchQuery}
disabled={!canDispatch}
className="w-full py-2.5 px-4 rounded-xl text-xs font-bold tracking-wide uppercase bg-gradient-to-r from-indigo-600 to-purple-600 hover:from-indigo-500 hover:to-purple-500 text-white shadow-lg shadow-indigo-600/30 transition disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
data-testid="dispatch-multi-query-btn"
>
{queryMultiMutation.isPending ? (
<>
<RefreshCw className="w-4 h-4 animate-spin" />
<span>Executing Private Set Intersection...</span>
</>
) : (
<>
<Search className="w-4 h-4" />
<span>Dispatch Multi-Counterparty Query</span>
</>
)}
</button>
</div>
</div>
{/* Sub-Panel 2: Execution Results & Truthful Partial Outcomes (7 Cols) */}
<div className="lg:col-span-7 space-y-4">
<div className="p-4 rounded-xl bg-slate-50 border border-indigo-200 space-y-4">
<div className="flex items-center justify-between">
<h3 className="text-xs font-bold uppercase tracking-wider text-slate-600 flex items-center gap-2">
<Layers className="w-4 h-4 text-purple-600" />
Execution Results & Partial Outcomes
</h3>
{queryResponse && (
<div className="flex items-center gap-2">
<span
className={`px-2 py-0.5 rounded text-[10px] font-mono font-bold uppercase ${
queryResponse.overall_outcome === 'COMPLETED'
? 'bg-emerald-50 text-emerald-700 border border-emerald-500/30'
: queryResponse.overall_outcome === 'PARTIAL_FAILURE'
? 'bg-amber-50 text-amber-700 border border-amber-500/30'
: 'bg-rose-50 text-rose-700 border border-rose-300'
}`}
>
{queryResponse.overall_outcome}
</span>
</div>
)}
</div>
{!queryResponse && !queryMultiMutation.isPending ? (
<div className="py-12 px-4 text-center rounded-xl border border-dashed border-slate-200 text-slate-500 text-xs">
<Search className="w-8 h-8 text-slate-600 mx-auto mb-2" />
<p className="font-semibold text-slate-600">No active query execution results</p>
<p className="text-[11px] text-slate-500 mt-1 max-w-sm mx-auto">
Select an authorized case entity and target counterparties on the left to execute genuine cross-bank private matching.
</p>
</div>
) : queryMultiMutation.isPending ? (
<div className="py-12 px-4 text-center rounded-xl bg-slate-50 border border-indigo-200 space-y-3">
<RefreshCw className="w-8 h-8 text-indigo-600 animate-spin mx-auto" />
<div className="text-xs text-indigo-700 font-semibold">
Executing Server-Authoritative Multi-Counterparty PSI...
</div>
<p className="text-[11px] text-slate-500 max-w-md mx-auto">
Coordinating cryptographic zero-knowledge intersection protocols across requested counterpart institutions.
</p>
</div>
) : queryResponse ? (
<div className="space-y-4">
{/* Batch Query Metadata */}
<div className="p-3 rounded-lg bg-white border border-slate-200 flex flex-wrap items-center justify-between gap-2 text-xs">
<div className="flex items-center gap-2">
<span className="text-slate-500 font-mono">BATCH ID:</span>
<span className="font-mono text-cyan-600 font-bold">{queryResponse.query_id}</span>
<button
type="button"
onClick={() => handleCopy(queryResponse.query_id, 'batch-id')}
className="p-1 text-slate-500 hover:text-slate-700 transition"
title="Copy batch query ID"
>
{copiedHash === 'batch-id' ? (
<Check className="w-3.5 h-3.5 text-emerald-600" />
) : (
<Copy className="w-3.5 h-3.5" />
)}
</button>
</div>
<div className="text-[11px] font-mono text-slate-500">
{new Date(queryResponse.executed_at).toLocaleTimeString()} · Counterparts:{' '}
{queryResponse.counterpart_count}
</div>
</div>
{/* Per-Counterparty Outcomes */}
<div className="space-y-2.5" data-testid="counterparty-outcomes-list">
{Array.from(
new Set([
...validSelectedTargets,
...Object.keys(queryResponse.results || {}),
...Object.keys(queryResponse.failures || {}),
]),
).map((bankKey, index) => {
const signal = queryResponse.results ? queryResponse.results[bankKey] : null;
const failure = queryResponse.failures ? queryResponse.failures[bankKey] : null;
// Gate B: Strictly enforce anonymous masking (null target bank) under ANONYMIZED_SUMMARY
const resolvedTargetBankId =
signal?.target_bank_id ||
(signal?.disclosure_mode === 'ATTRIBUTED_INSTITUTIONS'
? signal.contributing_institutions?.[0] || bankKey
: null);
const isAnonymized =
signal?.disclosure_mode === 'ANONYMIZED_SUMMARY' ||
signal?.disclosure_mode === 'ANONYMOUS_FLAG' ||
!resolvedTargetBankId;
const displayCounterpart = isAnonymized
? 'Anonymous Counterparty'
: (resolvedTargetBankId || bankKey).toUpperCase();
return (
<div
key={bankKey}
className={`p-3 rounded-xl border transition-all ${
signal?.match_confirmed
? 'bg-emerald-50 border-emerald-500/40'
: signal
? 'bg-slate-50 border-slate-200'
: failure
? 'bg-rose-50 border-rose-300'
: 'bg-slate-50 border-slate-200'
}`}
data-testid={isAnonymized ? `outcome-card-anonymized-${index}` : `outcome-card-${bankKey}`} data-outcome-index={index}
>
<div className="flex items-center justify-between gap-2 flex-wrap">
<div className="flex items-center gap-2">
<span className="text-xs font-mono font-bold text-slate-700">
{displayCounterpart}
</span>
{isAnonymized && signal && (
<span className="px-1.5 py-0.5 rounded text-[9px] font-mono bg-purple-50 text-purple-700 border border-purple-500/30">
ANONYMOUS MASKED
</span>
)}
</div>
{signal?.match_confirmed ? (
<span className="px-2 py-0.5 rounded-full text-[10px] font-bold bg-emerald-500/20 text-emerald-700 border border-emerald-500/40 flex items-center gap-1">
<CheckCircle2 className="w-3 h-3 text-emerald-600" />
MATCH CONFIRMED ({signal.match_category})
</span>
) : signal ? (
<span className="px-2 py-0.5 rounded-full text-[10px] font-bold bg-slate-100 text-slate-600 border border-slate-200 flex items-center gap-1">
<ShieldCheck className="w-3 h-3 text-slate-500" />
VERIFIED NO MATCH
</span>
) : failure ? (
<span className="px-2 py-0.5 rounded-full text-[10px] font-bold bg-rose-500/20 text-rose-700 border border-rose-300 flex items-center gap-1">
<XCircle className="w-3 h-3 text-rose-600" />
{failure.error_category}
</span>
) : (
<span className="px-2 py-0.5 rounded-full text-[10px] font-bold bg-slate-100 text-slate-500">
PENDING
</span>
)}
</div>
{/* Signal Match Details */}
{signal?.match_confirmed && (
<div className="mt-2.5 pt-2.5 border-t border-emerald-200 flex flex-wrap items-center gap-3 text-xs">
<div className="text-slate-600 font-mono">
Confidence: <span className="font-bold text-emerald-700">{(signal.match_confidence * 100).toFixed(0)}%</span>
</div>
<div className="text-slate-600 font-mono">
Risk Level: <span className="font-bold text-amber-700">{signal.sanitized_risk_level}</span>
</div>
{signal.sanitized_typology_indicators && signal.sanitized_typology_indicators.length > 0 && (
<div className="flex items-center gap-1 flex-wrap">
{signal.sanitized_typology_indicators.map((typ) => (
<span
key={typ}
className="px-1.5 py-0.5 rounded text-[10px] font-mono bg-amber-500/20 text-amber-700 border border-amber-500/30"
>
{typ}
</span>
))}
</div>
)}
</div>
)}
{/* Truthful Sanitized Failure Details */}
{failure && (
<div className="mt-2 pt-2 border-t border-rose-300 text-[11px] text-rose-700/90 leading-relaxed font-mono">
{isAnonymized && failure.error_message ? failure.error_message.replace(/\bbank_[a-zA-Z0-9_-]+\b/gi, '[redacted-counterparty]') : failure.error_message}
</div>
)}
</div>
);
})}
</div>
{/* Batch Evidence Attachment Action */}
<div className="p-3.5 rounded-xl bg-slate-50 border border-indigo-200 space-y-3">
<div className="flex items-center justify-between">
<span className="text-xs font-bold uppercase tracking-wider text-slate-700 flex items-center gap-1.5">
<ShieldCheck className="w-4 h-4 text-emerald-600" />
Assemble Verified Signals into Case Dossier
</span>
<span className="text-[10px] font-mono text-slate-500">
Single-Case Binding Enforced
</span>
</div>
<div className="space-y-1">
<label htmlFor={notesInputId} className="text-xs text-slate-600">
Analyst Investigative Notes (Optional)
</label>
<input
id={notesInputId}
type="text"
placeholder="e.g. Verified multi-counterparty intersection confirmed rapid velocity pattern across peer."
value={analystNotes}
onChange={(e: ChangeEvent<HTMLInputElement>) => setAnalystNotes(e.target.value)}
className="w-full px-3 py-2 rounded-lg bg-white border border-slate-200 text-xs text-slate-700 placeholder-slate-500 focus:outline-none focus:border-indigo-500"
/>
</div>
{attachError && (
<div className="p-2.5 rounded-lg bg-rose-50 border border-rose-300 text-rose-700 text-xs flex items-center gap-2">
<AlertTriangle className="w-4 h-4 text-rose-600 shrink-0" />
<span>{attachError}</span>
</div>
)}
{attachResponse && (
<div
className="p-3 rounded-lg bg-emerald-50 border border-emerald-500/40 text-emerald-700 text-xs space-y-1.5"
data-testid="attach-success-banner"
>
<div className="font-bold flex items-center gap-1.5 text-emerald-700">
<CheckCircle2 className="w-4 h-4 text-emerald-600" />
Batch Evidence Assembly: {attachResponse.assembly_status}
</div>
<div className="text-[11px] font-mono text-slate-600 flex items-center gap-3">
<span>Newly Attached: {attachResponse.attached_count}</span>
<span>Replayed: {attachResponse.replayed_count}</span>
<span>Omitted/Failed: {attachResponse.failed_count}</span>
</div>
{attachResponse.omitted_or_failed && attachResponse.omitted_or_failed.length > 0 && (
<div className="pt-1.5 border-t border-emerald-200 text-[10px] font-mono text-amber-700 space-y-0.5">
{attachResponse.omitted_or_failed.map((fail, idx) => (
<div key={idx}>
[{fail.category}] {fail.reason}
</div>
))}
</div>
)}
</div>
)}
<button
type="button"
onClick={handleAttachEvidence}
disabled={!hasEligibleSignals || attachMultiMutation.isPending}
className="w-full py-2.5 px-4 rounded-xl text-xs font-bold tracking-wide uppercase bg-gradient-to-r from-emerald-600 to-teal-600 hover:from-emerald-500 hover:to-teal-500 text-white shadow-lg shadow-emerald-600/20 transition disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
data-testid="attach-batch-evidence-btn"
>
{attachMultiMutation.isPending ? (
<>
<RefreshCw className="w-4 h-4 animate-spin" />
<span>Binding Evidence to Case Ledger...</span>
</>
) : (
<>
<ShieldCheck className="w-4 h-4" />
<span>Attach Eligible Signals to Case Evidence</span>
</>
)}
</button>
</div>
</div>
) : null}
</div>
</div>
</div>
{/* Sub-Panel 3: Persisted Case Intelligence Evidence Trail */}
<div className="p-4 sm:p-5 rounded-xl bg-slate-50 border border-indigo-200 space-y-4">
<div className="flex items-center justify-between flex-wrap gap-2">
<div>
<h3 className="text-xs sm:text-sm font-bold uppercase tracking-wider text-slate-700 flex items-center gap-2">
<ShieldCheck className="w-4 h-4 text-emerald-600" />
Persisted Authoritative Intelligence Evidence Trail
</h3>
<p className="text-[11px] text-slate-500 mt-0.5">
Cryptographically verified evidence items permanently committed to this investigation dossier
</p>
</div>
<button
type="button"
onClick={() => refetchEvidence()}
disabled={isLoadingEvidence}
className="px-2.5 py-1 rounded-lg text-xs font-mono text-indigo-600 hover:text-indigo-700 bg-white border border-slate-200 flex items-center gap-1 transition"
>
<RefreshCw className={`w-3 h-3 ${isLoadingEvidence ? 'animate-spin' : ''}`} />
Refresh Ledger
</button>
</div>
{isLoadingEvidence ? (
<div className="py-8 text-center text-xs font-mono text-slate-500 animate-pulse">
Loading persisted evidence ledger...
</div>
) : persistedEvidence.length === 0 ? (
<div
className="py-8 px-4 text-center rounded-xl border border-dashed border-slate-200 text-slate-500 text-xs font-mono"
data-testid="no-persisted-evidence"
>
No cross-bank intelligence evidence registered on this case yet.
</div>
) : (
<div className="space-y-3" data-testid="persisted-evidence-list">
{persistedEvidence.map((ev) => {
const isAnonymized =
ev.disclosure_mode === 'ANONYMIZED_SUMMARY' ||
ev.disclosure_mode === 'ANONYMOUS_FLAG' ||
!ev.target_bank_id;
const displayCounterpart = isAnonymized
? 'Anonymous Counterparty'
: (ev.target_bank_id || 'Consortium Peer');
const isExpired = ev.retention_status === 'RETENTION_EXPIRED';
return (
<div
key={ev.evidence_id}
className="p-3.5 rounded-xl bg-slate-50 border border-indigo-200 hover:border-indigo-500/40 transition space-y-2.5"
data-testid={`evidence-item-${ev.evidence_id}`}
>
<div className="flex items-center justify-between gap-2 flex-wrap">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-xs font-bold text-slate-800">{ev.title}</span>
<span className="px-2 py-0.5 rounded text-[10px] font-mono font-bold bg-indigo-100 text-indigo-700 border border-indigo-200">
{isAnonymized ? 'ANONYMOUS MASKED' : ev.target_bank_id?.toUpperCase()}
</span>
<span
className={`px-2 py-0.5 rounded text-[10px] font-mono font-bold border ${
ev.verification_state === 'VERIFIED'
? 'bg-emerald-500/20 text-emerald-700 border-emerald-500/40'
: ev.verification_state === 'INTEGRITY_VERIFIED_ONLY'
? 'bg-amber-500/20 text-amber-700 border-amber-500/40'
: ev.verification_state === 'FAILED_VERIFICATION'
? 'bg-rose-500/20 text-rose-700 border-rose-300'
: 'bg-slate-100 text-slate-500 border-slate-200'
}`}
>
{ev.verification_state}
</span>
</div>
<div className="flex items-center gap-2">
{isExpired ? (
<span
className="px-2 py-0.5 rounded text-[10px] font-mono bg-slate-100 text-slate-500 border border-slate-200 flex items-center gap-1"
title="Intermediate query batch record expired per 24h retention policy; evidence remains permanently in this case ledger."
>
<Clock className="w-3 h-3 text-slate-500" />
Batch Record Purged (Retention Bounded)
</span>
) : (
<span className="px-2 py-0.5 rounded text-[10px] font-mono bg-emerald-50 text-emerald-700 border border-emerald-500/30 flex items-center gap-1">
<CheckCircle2 className="w-3 h-3 text-emerald-600" />
Batch Active
</span>
)}
</div>
</div>
{/* Evidence Payload Details */}
<div className="grid grid-cols-1 sm:grid-cols-3 gap-2 text-xs">
<div className="p-2 rounded-lg bg-slate-50 border border-slate-200/80">
<span className="text-[10px] uppercase font-mono text-slate-500 block">Counterpart Scope</span>
<span className="font-mono text-slate-700 font-semibold">{displayCounterpart}</span>
</div>
<div className="p-2 rounded-lg bg-slate-50 border border-slate-200/80">
<span className="text-[10px] uppercase font-mono text-slate-500 block">Match Status</span>
<span className="font-mono text-emerald-600 font-semibold">
{ev.match_confirmed ? `MATCH CONFIRMED (${ev.match_category || 'EXACT'})` : 'NO MATCH'}
</span>
</div>
<div className="p-2 rounded-lg bg-slate-50 border border-slate-200/80">
<span className="text-[10px] uppercase font-mono text-slate-500 block">Assurance Level</span>
<span className="font-mono text-cyan-700 font-semibold">{ev.operational_status}</span>
</div>
</div>
{/* Analyst Notes if recorded */}
{ev.analyst_notes && (
<div className="text-xs text-slate-600 bg-slate-50 p-2.5 rounded-lg border border-slate-200">
<span className="text-slate-500 font-mono text-[10px] block mb-0.5">Analyst Note:</span>
{ev.analyst_notes}
</div>
)}
{/* Verification Explanation */}
{ev.explanation && (
<div className="text-[11px] font-mono text-slate-500 bg-slate-50 p-2 rounded border border-slate-200/80">
<span className="text-slate-500 block mb-0.5">Verification Detail:</span>
{ev.explanation}
</div>
)}
{/* Cryptographic SHA-256 Digest & Audit Footer */}
<div className="pt-2 border-t border-slate-200/60 flex flex-wrap items-center justify-between gap-2 text-[11px] font-mono text-slate-500">
<div className="flex items-center gap-1.5">
<span>SHA-256 Digest:</span>
<span className="text-cyan-600 font-bold break-all">{ev.content_hash}</span>
<button
type="button"
onClick={() => handleCopy(ev.content_hash, ev.evidence_id)}
className="p-1 text-slate-500 hover:text-slate-700 transition"
title="Copy cryptographic digest"
>
{copiedHash === ev.evidence_id ? (
<Check className="w-3.5 h-3.5 text-emerald-600" />
) : (
<Copy className="w-3.5 h-3.5" />
)}
</button>
</div>
<div className="text-slate-500 text-[10px]">
By {ev.uploaded_by} on {new Date(ev.uploaded_at).toLocaleString()}
</div>
</div>
</div>
);
})}
</div>
)}
</div>
</div>
);
};