Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | 819x 273x 273x 273x 715x 10x 10x 10x 10x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 10x 45x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 44x 41x 41x 41x 41x 41x 40x 40x 40x 40x 40x 188x 85x 41x 41x 40x 40x 40x 188x 66x 188x 12x 12x 12x 12x 12x 12x 12x 12x 11x 11x 1x 1x 1x 10x 10x 10x 1x 1x 12x 3x 3x 3x 188x 3x 3x 3x 3x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 3x 273x 273x 180x 93x 91x 2x 429x 42x 429x 188x 1x 286x 286x 143x 34x 3x 273x 273x 273x 273x | "use client";
import { fetchApi } from "@/lib/api";
import { Suspense, useEffect, useState } from "react";
import { useSearchParams } from "next/navigation";
import {
FileSignature,
Loader2,
CheckCircle2,
Clock,
XCircle,
ArrowRight,
Search,
AlertCircle,
BookOpen,
} from "lucide-react";
import PageIntro from "@/components/PageIntro";
interface ParticipantCtx {
"@id": string;
identity: string;
participantId?: string;
displayName?: string;
role?: string;
}
interface CatalogOffer {
assetId: string;
offerId: string;
assignerDid: string;
name?: string;
description?: string;
contentType?: string;
}
interface Negotiation {
"@id": string;
state?: string;
"edc:state"?: string;
contractAgreementId?: string;
"edc:contractAgreementId"?: string;
counterPartyId?: string;
"edc:counterPartyId"?: string;
assetId?: string;
[key: string]: unknown;
}
/** Read a negotiation field that may be edc:-prefixed or unprefixed */
function negField(n: Negotiation, field: string): string {
return ((n as Record<string, unknown>)[field] ??
(n as Record<string, unknown>)[`edc:${field}`] ??
"") as string;
}
/** Resolve a DID to a short human-readable name */
function didToName(did: string): string {
const slug = decodeURIComponent(did).split(":").pop()?.toLowerCase() ?? "";
const names: Record<string, string> = {
"alpha-klinik": "AlphaKlinik Berlin",
pharmaco: "PharmaCo Research AG",
medreg: "MedReg DE",
lmc: "Limburg Medical Centre",
irs: "Institut de Recherche Santé",
};
return names[slug] || slug || did.slice(0, 16);
}
/** Pretty-print an asset ID as a title */
function assetLabel(id: string): string {
return id.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
}
/** Parse ODRL offers from a DCAT catalog response (handles multiple JSON-LD shapes) */
function parseOffers(catalog: Record<string, unknown>): CatalogOffer[] {
const offers: CatalogOffer[] = [];
let datasets = catalog["dataset"] ?? catalog["dcat:dataset"] ?? [];
if (!Array.isArray(datasets)) datasets = [datasets];
for (const ds of datasets as Record<string, unknown>[]) {
const assetId = (ds["@id"] ?? ds["id"] ?? "") as string;
const name = (ds["name"] ?? ds["dct:title"] ?? ds["title"] ?? "") as string;
const description = (ds["description"] ??
ds["dct:description"] ??
"") as string;
const contentType = (ds["contenttype"] ?? "") as string;
let policies = ds["hasPolicy"] ?? ds["odrl:hasPolicy"] ?? [];
if (!Array.isArray(policies)) policies = [policies];
for (const p of policies as Record<string, unknown>[]) {
const offerId = (p["@id"] ?? "") as string;
const assigner = p["assigner"] ?? p["odrl:assigner"] ?? "";
const assignerDid =
typeof assigner === "object"
? ((assigner as Record<string, unknown>)["@id"] as string) ?? ""
: (assigner as string);
Eif (assetId && offerId) {
offers.push({
assetId,
offerId,
assignerDid,
name,
description,
contentType,
});
}
}
}
return offers;
}
export default function NegotiatePage() {
return (
<Suspense
fallback={
<div className="flex items-center gap-2 text-[var(--text-secondary)] p-10">
<Loader2 size={16} className="animate-spin" />
Loading…
</div>
}
>
<NegotiateContent />
</Suspense>
);
}
function NegotiateContent() {
const searchParams = useSearchParams();
const preAssetId = searchParams.get("assetId") || "";
const preProviderId = searchParams.get("providerId") || "";
const preProviderDid = searchParams.get("providerDid") || "";
const [participants, setParticipants] = useState<ParticipantCtx[]>([]);
const [negotiations, setNegotiations] = useState<Negotiation[]>([]);
const [loading, setLoading] = useState(true);
const [selectedCtx, setSelectedCtx] = useState("");
// Step 1 — Catalog discovery
const [selectedProviderCtx, setSelectedProviderCtx] = useState(
preProviderId || "",
);
const [providerCtxId, setProviderCtxId] = useState(preProviderId);
const [providerDid, setProviderDid] = useState(preProviderDid);
const [dspBase] = useState("http://controlplane:8082/api/dsp");
const [catalogLoading, setCatalogLoading] = useState(false);
const [catalogError, setCatalogError] = useState<string | null>(null);
const [offers, setOffers] = useState<CatalogOffer[]>([]);
const [selectedOffer, setSelectedOffer] = useState<CatalogOffer | null>(null);
// Step 2 — Negotiation form (populated from selected offer)
const [assetId, setAssetId] = useState(preAssetId);
const [offerId, setOfferId] = useState("");
const [initiating, setInitiating] = useState(false);
const [result, setResult] = useState<string | null>(null);
useEffect(() => {
fetchApi("/api/participants")
.then((r) => (r.ok ? r.json() : []))
.then((d) => {
const list: ParticipantCtx[] = Array.isArray(d)
? d
: d.participants ?? [];
setParticipants(list);
if (list.length > 0) setSelectedCtx(list[0]["@id"]);
// Pre-select first non-consumer as provider
if (list.length > 1 && !preProviderId) {
const first = list[0];
setSelectedProviderCtx(first["@id"]);
setProviderCtxId(first["@id"]);
// identity holds the real DID; participantId is the UUID context ID
const did = first.identity ?? "";
setProviderDid(did);
}
})
.catch(() => {});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (!selectedCtx) return;
setLoading(true);
fetchApi(`/api/negotiations?participantId=${selectedCtx}`)
.then((r) => (r.ok ? r.json() : []))
.then((d) => {
setNegotiations(Array.isArray(d) ? d : d.negotiations ?? []);
setLoading(false);
})
.catch(() => setLoading(false));
}, [selectedCtx]);
useEffect(() => {
Iif (preAssetId && offers.length > 0) {
const match = offers.find((o) => o.assetId === preAssetId);
if (match) selectOffer(match);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [offers]);
/** Step 1: DCP catalog discovery via v1alpha participants/{ctxId}/catalog */
const handleDiscoverCatalog = async () => {
Iif (!selectedCtx || (!providerDid && !providerCtxId)) return;
setCatalogLoading(true);
setCatalogError(null);
setOffers([]);
setSelectedOffer(null);
const did =
providerDid ||
`did:web:identityhub%3A7083:${providerCtxId
.toLowerCase()
.replace(/\s+/g, "-")}`;
try {
const res = await fetchApi(
`/api/negotiations?participantId=${selectedCtx}&catalog=true&providerDid=${encodeURIComponent(
did,
)}`,
);
const data = await res.json();
if (!res.ok) {
const errData = data as Record<string, unknown>;
setCatalogError(
(errData.detail as string) ||
(errData.error as string) ||
"Failed to fetch catalog",
);
return;
}
const parsed = parseOffers(data as Record<string, unknown>);
setOffers(parsed);
if (parsed.length === 0) {
setCatalogError(
"No datasets with offers found. Ensure the provider has contract definitions and active participant contexts (VPAs in ACTIVE state).",
);
}
} catch {
setCatalogError("Network error fetching catalog");
} finally {
setCatalogLoading(false);
}
};
function selectOffer(o: CatalogOffer) {
setSelectedOffer(o);
setAssetId(o.assetId);
setOfferId(o.offerId);
}
/** Step 2: Submit DSP ContractRequest with real offer ID */
const handleNegotiate = async (e: React.FormEvent) => {
e.preventDefault();
setInitiating(true);
setResult(null);
try {
const res = await fetchApi("/api/negotiations", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
participantId: selectedCtx,
assetId,
counterPartyAddress: dspBase,
counterPartyId: providerCtxId,
providerDid: selectedOffer?.assignerDid || providerDid,
offerId,
}),
});
if (res.ok) {
const data = (await res.json()) as Record<string, unknown>;
setResult(
`Negotiation initiated: ${data["@id"] || JSON.stringify(data)}`,
);
const updated = await fetchApi(
`/api/negotiations?participantId=${selectedCtx}`,
);
const ud = await updated.json();
setNegotiations(Array.isArray(ud) ? ud : ud.negotiations ?? []);
} else {
const err = (await res.json()) as Record<string, unknown>;
setResult(
`Error: ${
(err.detail as string) ||
(err.error as string) ||
"Negotiation failed"
}`,
);
}
} catch {
setResult("Error: Failed to initiate negotiation");
} finally {
setInitiating(false);
}
};
function stateIcon(state: string) {
const s = state?.toUpperCase() || "";
if (s.includes("FINALIZED") || s.includes("AGREED"))
return <CheckCircle2 size={16} className="text-[var(--success-text)]" />;
if (s.includes("TERMINATED") || s.includes("ERROR"))
return <XCircle size={16} className="text-[var(--danger-text)]" />;
return <Clock size={16} className="text-[var(--warning-text)]" />;
}
function displayId(p: ParticipantCtx) {
if (p.displayName) return p.displayName;
const did = p.participantId ?? p.identity ?? "";
return (
did.replace("did:web:", "").replace(/%3A/gi, ":").split(":").pop() ||
p["@id"].slice(0, 12)
);
}
return (
<div className="min-h-screen bg-[var(--bg)]">
<div className="max-w-5xl mx-auto px-6 py-10">
<PageIntro
title="Contract Negotiation"
icon={FileSignature}
description="Negotiate data access contracts via the Dataspace Protocol using ODRL policies. Select a consumer participant, choose a dataset, and initiate a contract negotiation request that the data holder can accept or reject."
prevStep={{ href: "/data/discover", label: "Discover Data" }}
nextStep={{ href: "/data/transfer", label: "Data Transfer" }}
infoText="Two steps: (1) choose a data provider and click Discover Offers to see what datasets are available; (2) select a dataset and click Start Negotiation to request access."
docLink={{
href: "https://docs.internationaldataspaces.org/ids-knowledgebase/dataspace-protocol",
label: "DSP Specification",
external: true,
}}
/>
{/* Consumer context selector */}
<div className="mb-6">
<label className="text-xs text-[var(--text-secondary)] mb-1 block">
Requesting as (your participant)
</label>
<select
aria-label="Requesting as (your participant)"
value={selectedCtx}
onChange={(e) => setSelectedCtx(e.target.value)}
className="w-full max-w-md px-3 py-2 bg-[var(--surface-2)] border border-[var(--border-ui)] rounded text-sm"
>
{participants.map((p) => (
<option key={p["@id"]} value={p["@id"]}>
{displayId(p)}
{p.role ? ` [${p.role}]` : ""}
</option>
))}
</select>
</div>
{/* ── Step 1: Catalog discovery ── */}
<div className="border border-[var(--border)] rounded-xl p-5 mb-5">
<div className="flex items-center gap-2 mb-1">
<Search size={16} className="text-teal-800 dark:text-teal-300" />
<h2 className="font-semibold text-sm">
Step 1 — Choose Data Provider
</h2>
</div>
<p className="text-xs text-[var(--text-secondary)] mb-4">
Choose a data provider below — the catalog will be fetched
automatically.
</p>
<div className="mb-4">
<label className="text-xs text-[var(--text-secondary)] mb-1 block">
Data Provider
</label>
<select
aria-label="Data Provider"
value={selectedProviderCtx}
onChange={(e) => {
const chosen = participants.find(
(p) => p["@id"] === e.target.value,
);
if (!chosen) return;
setSelectedProviderCtx(chosen["@id"]);
setProviderCtxId(chosen["@id"]);
// identity holds the real DID; participantId is the UUID context ID
setProviderDid(chosen.identity ?? "");
// Reset previous catalog results when provider changes
setOffers([]);
setSelectedOffer(null);
setCatalogError(null);
}}
className="w-full max-w-md px-3 py-2 bg-[var(--surface-2)] border border-[var(--border-ui)] rounded text-sm"
disabled={participants.length === 0}
>
{participants.length === 0 && (
<option value="">Loading participants…</option>
)}
{participants
.filter((p) => p["@id"] !== selectedCtx)
.map((p) => (
<option key={p["@id"]} value={p["@id"]}>
{displayId(p)}
{p.role ? ` [${p.role}]` : ""}
</option>
))}
</select>
</div>
<button
type="button"
disabled={catalogLoading || !selectedCtx || !selectedProviderCtx}
onClick={handleDiscoverCatalog}
className="flex items-center gap-2 px-4 py-2 bg-gray-700 text-white rounded text-sm font-medium hover:bg-gray-600 disabled:opacity-50"
>
{catalogLoading ? (
<Loader2 size={14} className="animate-spin" />
) : (
<BookOpen size={14} />
)}
Discover Offers
</button>
{catalogError && (
<div className="mt-3 p-3 rounded bg-red-100 dark:bg-red-900/30 border border-red-300 dark:border-red-700 text-red-800 dark:text-red-300 text-xs flex gap-2">
<AlertCircle size={14} className="shrink-0 mt-0.5" />
<span>{catalogError}</span>
</div>
)}
{offers.length > 0 && (
<div className="mt-4 space-y-2">
<p className="text-xs text-[var(--text-secondary)] mb-2">
{offers.length} offer(s) found — select one to negotiate:
</p>
{offers.map((o) => (
<button
key={o.offerId}
type="button"
onClick={() => selectOffer(o)}
className={`w-full text-left p-3 rounded-lg border text-sm transition-colors ${
selectedOffer?.offerId === o.offerId
? "border-layer2 bg-layer2/10"
: "border-[var(--border)] hover:border-gray-500 bg-[var(--surface-2)]/50"
}`}
>
<div className="flex items-center justify-between">
<span className="font-medium text-[var(--text-primary)]">
{o.name || assetLabel(o.assetId)}
</span>
<div className="flex items-center gap-2">
{o.contentType && (
<span className="text-[10px] px-1.5 py-0.5 rounded bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200">
{o.contentType}
</span>
)}
{selectedOffer?.offerId === o.offerId && (
<CheckCircle2
size={14}
className="text-teal-800 dark:text-teal-300"
/>
)}
</div>
</div>
{o.description && (
<p className="text-xs text-[var(--text-secondary)] mt-0.5 truncate">
{o.description}
</p>
)}
</button>
))}
</div>
)}
</div>
{/* ── Step 2: Negotiation ── */}
<div className="border border-[var(--border)] rounded-xl p-5 mb-8">
<div className="flex items-center gap-2 mb-1">
<FileSignature
size={18}
className="text-teal-800 dark:text-teal-300"
/>
<h2 className="font-semibold text-sm">
Step 2 — Initiate Negotiation
</h2>
</div>
<p className="text-xs text-[var(--text-secondary)] mb-4">
Submit a DSP{" "}
<code className="text-[var(--text-secondary)]">
ContractRequest
</code>{" "}
with the selected ODRL offer. Protocol:{" "}
<code className="text-[var(--text-secondary)]">
dataspace-protocol-http:2025-1
</code>
</p>
{result && (
<div
className={`mb-4 p-3 rounded text-sm ${
result.startsWith("Error")
? "bg-[var(--badge-inactive-bg)] border border-[var(--badge-inactive-border)] text-[var(--badge-inactive-text)]"
: "bg-[var(--badge-active-bg)] border border-[var(--badge-active-border)] text-[var(--badge-active-text)]"
}`}
>
{result}
</div>
)}
{!selectedOffer && (
<div className="mb-4 p-3 rounded bg-yellow-100 dark:bg-yellow-900/20 border border-yellow-300 dark:border-yellow-700/40 text-yellow-800 dark:text-yellow-400 text-xs flex gap-2">
<AlertCircle size={14} className="shrink-0 mt-0.5" />
<span>
Complete Step 1 first — select a dataset offer above to
negotiate.
</span>
</div>
)}
{selectedOffer && (
<div className="mb-4 p-3 rounded bg-[var(--surface-2)]/60 border border-[var(--border)] text-sm">
<span className="text-[var(--text-secondary)]">Selected:</span>{" "}
<span className="text-[var(--text-primary)] font-medium">
{selectedOffer.name || assetLabel(selectedOffer.assetId)}
</span>
{selectedOffer.contentType && (
<span className="ml-2 text-[10px] px-1.5 py-0.5 rounded bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200">
{selectedOffer.contentType}
</span>
)}
</div>
)}
<form onSubmit={handleNegotiate} className="space-y-3">
{/* Hidden fields — populated automatically from Step 1 */}
<input type="hidden" value={assetId} />
<input type="hidden" value={offerId} />
<button
type="submit"
disabled={initiating || !offerId}
className="flex items-center gap-2 px-4 py-2 bg-[var(--accent)] text-white dark:text-gray-900 rounded text-sm font-medium hover:bg-[var(--accent-hover)] disabled:opacity-50"
>
{initiating ? (
<Loader2 size={14} className="animate-spin" />
) : (
<FileSignature size={14} />
)}
Start Negotiation
</button>
</form>
</div>
{/* Negotiation history */}
<h2 className="font-semibold text-sm mb-3">Negotiation History</h2>
{loading ? (
<div className="flex items-center gap-2 text-[var(--text-secondary)]">
<Loader2 size={16} className="animate-spin" />
Loading negotiations…
</div>
) : negotiations.length === 0 ? (
<p className="text-[var(--text-secondary)] text-sm">
No negotiations found
</p>
) : (
<div className="grid gap-2">
{negotiations.map((n) => {
const agreementId = negField(n, "contractAgreementId");
const state = negField(n, "state");
const counterParty = negField(n, "counterPartyId");
return (
<div
key={n["@id"]}
className="flex items-center gap-3 p-3 border border-[var(--border)] rounded-lg"
>
{stateIcon(state)}
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-[var(--text-primary)] truncate">
{n.assetId
? assetLabel(n.assetId as string)
: n["@id"].slice(0, 12)}
</p>
<p className="text-xs text-[var(--text-secondary)]">
Provider: {counterParty ? didToName(counterParty) : "—"}
{state === "FINALIZED" && " · Agreement ready"}
</p>
</div>
<div className="flex items-center gap-2 shrink-0">
<span
className={`text-xs px-2 py-0.5 rounded-full ${
state.includes("FINALIZED")
? "bg-[var(--badge-active-bg)] text-[var(--badge-active-text)] border border-[var(--badge-active-border)]"
: state.includes("ERROR")
? "bg-[var(--badge-inactive-bg)] text-[var(--badge-inactive-text)] border border-[var(--badge-inactive-border)]"
: "bg-yellow-100 dark:bg-yellow-900/40 text-yellow-800 dark:text-yellow-400"
}`}
>
{state || "UNKNOWN"}
</span>
{agreementId && (
<a
href={`/data/transfer?participantId=${selectedCtx}&contractId=${agreementId}`}
className="flex items-center gap-1 text-xs text-teal-800 dark:text-teal-300 hover:underline"
>
Transfer <ArrowRight size={12} />
</a>
)}
</div>
</div>
);
})}
</div>
)}
</div>
</div>
);
}
|