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 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 | 3x 3x 3x 3x 14x 14x 3x 1455x 610x 425x 3x 3x 5x 5x 11x 4x 6x 6x 6x 6x 3x 3x 6x 6x 3x 3x 1x 2x 1x 1x 1x 6x 6x 1x 2x 5x 5x 6x 20x 20x 20x 20x 20x 1x 1x 19x 7x 7x 12x 4x 2x 2x 2x 2x 8x 5x 3x 20x 314x 172x 3x 80x 3x 3x 1x 1x 1x 1x 1x 3x 45x 5x 5x 5x 5x 5x 3x 2x 2x 2x 2x 2x 4x 12x 12x 12x 12x 12x 3x 3x 9x 2x 2x 2x 7x 2x 5x 1x 4x 1x 1x 3x 1x 2x 2x 1x 1x 1x 1x 12x 1x 15x 15x 15x 15x 15x 15x 15x 11x 7x 7x 11x 11x 7x 15x 1x 1x 1x 1x 1x 15x 12x 21x 1x 11x 56x 56x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 351x 4x 4x 4x 4x 4x 351x 351x 54x 51x 51x 51x 51x 49x 87x 2x 2x 1x 351x 104x 50x 50x 49x 49x 1x 49x 49x 49x 60x 60x 1x 50x 50x 50x 351x 351x 106x 1x 1x 1x 1x 1x 1x 1x 1x 351x 10x 10x 10x 10x 10x 10x 10x 351x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 5x 3x 3x 3x 1x 2x 2x 2x 2x 1x 6x 3x 3x 2x 1x 3x 1x 3x 3x 3x 6x 6x 351x 109x 80x 15x 351x 113x 95x 95x 18x 18x 18x 18x 18x 18x 18x 5x 18x 351x 109x 351x 220x 190x 220x 220x 162x 162x 220x 136x 220x 194x 168x 28x 168x 162x 168x 168x 168x 110x 168x 53x 168x 168x 168x 32x 5x 65x 351x 220x 220x 220x 351x 227x 227x 182x 351x 351x 351x 351x 104x 351x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 351x 351x 4x 7x 1x 351x 13x 1x 1x 12x 13x 12x 2x 2x 10x 10x 10x 10x 10x 9x 9x 10x 351x 24x 24x 24x 24x 24x 24x 12x 12x 11x 11x 1x 351x 1x 425x 1x 1x 1038x 3x 3x 4x 269x 269x 269x 269x 255x 269x 5x 2x 11x 4x 3x 3x 1x 4x 4x 4x 3x 4x 8x 6x 6x 6x 6x 6x | "use client";
import Link from "next/link";
import { fetchApi } from "@/lib/api";
import { didToName } from "@/lib/participant-name";
import { Suspense, useCallback, useEffect, useMemo, useState } from "react";
import { useSearchParams } from "next/navigation";
import {
ArrowRightLeft,
Box,
CheckCircle2,
ChevronDown,
ChevronRight,
Circle,
Copy,
Download,
FileJson2,
Loader2,
Play,
RefreshCw,
X,
XCircle,
Network,
} from "lucide-react";
import PageIntro from "@/components/PageIntro";
import RowSourceBadge from "@/components/RowSourceBadge";
import TransferDetails from "@/components/TransferDetails";
import SpeImport from "@/components/SpeImport";
import {
closedAfterImportInto,
closedAfterImportReason,
importViewUrl,
type ExistingImport,
} from "@/lib/spe-import";
import type { RowSource } from "@/lib/row-provenance";
import { T, useT } from "@/lib/i18n/translate";
import { DEFAULT_PAGE_SIZE } from "@/lib/list-page";
/* ── Types ── */
interface Transfer {
"@id": string;
state?: string;
"edc:state"?: string;
type?: string;
"edc:type"?: string;
stateTimestamp?: number;
"edc:stateTimestamp"?: number;
contractId?: string;
"edc:contractId"?: string;
transferType?: string;
assetId?: string;
[key: string]: unknown;
}
interface Agreement {
"@id": string;
assetId?: string;
"edc:assetId"?: string;
counterPartyId?: string;
counterPartyAddress?: string;
[key: string]: unknown;
}
interface ParticipantCtx {
"@id": string;
identity: string;
displayName?: string;
role?: string;
}
/* ── Helpers ── */
/**
* Where a contract stands for the data user (#876, #894): one list, one row
* per agreement, instead of the agreements, My data and the transfer
* processes as three lists of the same things.
*/
type Stage = "agreed" | "progress" | "ready" | "imported" | "done";
/** The filter chips, in the order a contract goes through. */
const STAGE_ORDER: Stage[] = [
"agreed",
"progress",
"ready",
"imported",
"done",
];
const STAGE_LABEL: Record<Stage, string> = {
agreed: "Ready to transfer",
progress: "Transfer in progress",
ready: "Data ready",
imported: "In an SPE",
done: "Transfer completed",
};
const STAGE_STYLE: Record<Stage, string> = {
agreed:
"bg-(--role-user-bg) text-(--role-user-text) border border-(--role-user-border)",
progress:
"bg-yellow-100 dark:bg-yellow-900/30 text-yellow-800 dark:text-yellow-300 border border-yellow-400/40",
ready:
"bg-teal-100 dark:bg-teal-900/30 text-teal-800 dark:text-teal-300 border border-teal-500/40",
imported:
"bg-violet-100 dark:bg-violet-900/30 text-violet-800 dark:text-violet-300 border border-violet-500/40",
done: "bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 border border-(--border)",
};
/** A date and time in the reader's locale. */
function when(v: number | string): string {
const d = new Date(v);
return Number.isNaN(d.getTime())
? ""
: d.toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" });
}
/** No import known for a transfer: one shared array, so a row does not refetch. */
const NO_IMPORTS: ExistingImport[] = [];
/** Read a transfer/agreement field that may be edc:-prefixed or unprefixed */
function f(obj: Record<string, unknown>, field: string): string {
return (obj[field] ?? obj[`edc:${field}`] ?? "") as string;
}
/** Pretty-print an asset ID as a title */
function assetLabel(id: string): string {
return id.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
}
function displayParticipant(p: ParticipantCtx): string {
if (p.displayName) return p.displayName;
return didToName(p.identity || p["@id"]);
}
/**
* DSP Transfer Process state machine (Signalling Protocol).
* @see https://eclipse-dataspace-protocol-base.github.io/DataspaceProtocol/2025-1/#transfer-protocol
*
* Consumer states: REQUESTED → STARTED → SUSPENDED → COMPLETED | TERMINATED
*/
const DSP_STATES = ["REQUESTED", "STARTED", "SUSPENDED", "COMPLETED"] as const;
type DspState = (typeof DSP_STATES)[number] | "TERMINATED" | string;
function stateIndex(state: DspState): number {
const s = state?.toUpperCase() || "";
if (s.includes("TERMINATED")) return -1; // error path
const idx = DSP_STATES.findIndex((ds) => s.includes(ds));
return idx;
}
function stateColor(state: DspState) {
const s = state?.toUpperCase() || "";
Iif (s.includes("COMPLETED")) return "text-(--role-user-text)";
Iif (s.includes("TERMINATED") || s.includes("ERROR"))
return "text-(--role-admin-text)";
if (s.includes("STARTED")) return "text-(--role-holder-text)";
Iif (s.includes("SUSPENDED")) return "text-(--role-hdab-text)";
return "text-(--role-hdab-text)";
}
function stateBg(state: DspState) {
const s = state?.toUpperCase() || "";
if (s.includes("COMPLETED"))
return "bg-(--role-user-bg) text-(--role-user-text) border border-(--role-user-border)";
if (s.includes("TERMINATED") || s.includes("ERROR")) {
return "bg-(--role-admin-bg) text-(--role-admin-text) border border-(--role-admin-border)";
}
if (s.includes("STARTED"))
return "bg-(--role-holder-bg) text-(--role-holder-text) border border-(--role-holder-border)";
Iif (s.includes("SUSPENDED"))
return "bg-(--role-hdab-bg) text-(--role-hdab-text) border border-(--role-hdab-border)";
return "bg-(--role-hdab-bg) text-(--role-hdab-text) border border-(--role-hdab-border)";
}
/* ── DSP Pipeline Stepper ── */
function DspPipeline({
state,
closedInto = null,
}: {
state: DspState;
/** The SPE the hub closed the transfer for, once its data was imported. */
closedInto?: string | null;
}) {
const t = useT("research");
// Terminated by the hub after the import: requested, started, then closed
// with the data in the SPE. Not a failure, so not drawn as one.
if (closedInto) {
return (
<div
data-testid="pipeline-closed-after-import"
className="flex items-center gap-0.5 w-full"
>
{["REQUESTED", "STARTED"].map((step) => (
<div key={step} className="flex items-center flex-1 min-w-0">
<div className="flex flex-col items-center gap-0.5">
<CheckCircle2 size={14} className="text-(--role-user-text)" />
<span className="text-[10px] text-(--role-user-text) whitespace-nowrap">
{step}
</span>
</div>
<div className="flex-1 h-px mx-1 bg-green-700" />
</div>
))}
<div className="flex flex-col items-center gap-0.5">
<CheckCircle2 size={14} className="text-(--role-user-text)" />
<span className="text-[10px] text-(--role-user-text) whitespace-nowrap">
{t("CLOSED AFTER IMPORT INTO {spe}", { spe: closedInto })}
</span>
</div>
</div>
);
}
const current = stateIndex(state);
const isTerminated =
state?.toUpperCase().includes("TERMINATED") ||
state?.toUpperCase().includes("ERROR");
return (
<div className="flex items-center gap-0.5 w-full">
{DSP_STATES.map((step, i) => {
const isActive = i === current;
const isPast = current >= 0 && i < current;
const isFuture = current >= 0 && i > current;
let icon;
let color = "text-(--text-secondary)";
if (isTerminated && i === 0) {
// Show error on first uncompleted step
icon = <XCircle size={14} className="text-(--role-admin-text)" />;
color = "text-(--role-admin-text)";
} else if (isPast) {
icon = <CheckCircle2 size={14} className="text-(--role-user-text)" />;
color = "text-(--role-user-text)";
} else if (isActive) {
if (step === "COMPLETED") {
icon = (
<CheckCircle2 size={14} className="text-(--role-user-text)" />
);
color = "text-(--role-user-text)";
} else {
icon = (
<div className="relative">
<Circle size={14} className={stateColor(state)} />
<div
className={`absolute inset-0 animate-ping rounded-full ${stateColor(
state,
)} opacity-30`}
style={{ width: 14, height: 14 }}
/>
</div>
);
color = stateColor(state);
}
} else if (isFuture) {
icon = <Circle size={14} className="text-gray-600" />;
} else {
icon = <Circle size={14} className="text-gray-600" />;
}
return (
<div key={step} className="flex items-center flex-1 min-w-0">
<div className="flex flex-col items-center gap-0.5">
{icon}
<span className={`text-[10px] ${color} whitespace-nowrap`}>
{step}
</span>
</div>
{i < DSP_STATES.length - 1 && (
<div
className={`flex-1 h-px mx-1 ${
isPast ? "bg-green-700" : "bg-gray-700"
}`}
/>
)}
</div>
);
})}
{isTerminated && (
<div className="flex flex-col items-center gap-0.5 ml-1">
<XCircle size={14} className="text-(--role-admin-text)" />
<span className="text-[10px] text-(--role-admin-text)">
TERMINATED
</span>
</div>
)}
</div>
);
}
/* ── FHIR Bundle Types ── */
interface FhirBundleEntry {
fullUrl?: string;
resource: { resourceType: string; id?: string; [key: string]: unknown };
search?: { mode?: string };
}
interface FhirBundle {
resourceType: "Bundle";
id?: string;
type: string;
total: number;
meta?: { lastUpdated?: string };
link?: { relation: string; url: string }[];
entry?: FhirBundleEntry[];
}
/** What GET /api/transfers/[id]/data answers (#876). */
interface LiveData {
bytes: number;
truncated: boolean;
fetchedAt: string;
data: unknown;
}
/** A transfer whose access is open on the connector: its data can be read. */
function isLiveTransfer(tr: Transfer): boolean {
return (
tr.source === "controlplane" &&
f(tr as Record<string, unknown>, "state").toUpperCase() === "STARTED"
);
}
/** A transfer the hub terminated once its data was in an SPE. */
function isClosedAfterImport(tr: Transfer): boolean {
return (
tr.source === "controlplane" &&
!!closedAfterImportInto(f(tr as Record<string, unknown>, "errorDetail"))
);
}
/** States in which a transfer stays until someone acts: nothing to wait for. */
const SETTLED_STATES = [
"STARTED",
"SUSPENDED",
"COMPLETED",
"TERMINATED",
"DEPROVISIONED",
];
/**
* A connector's transfer still on its way, INITIAL or REQUESTED: the page
* reloads it until it settles, or "Waiting for the provider" stays on a
* transfer that has long started.
*/
function isSettling(tr: Transfer): boolean {
return (
tr.source === "controlplane" &&
!SETTLED_STATES.includes(
f(tr as Record<string, unknown>, "state").toUpperCase(),
)
);
}
/** How often, and for how long, a settling transfer is reloaded. */
const SETTLING_POLL_MS = 3000;
const SETTLING_POLL_LIMIT = 40;
/** FHIR data as a bundle for the viewer: a Bundle, a list or one resource. */
function toBundle(data: unknown): FhirBundle | null {
const isResource = (r: unknown) =>
!!r && typeof r === "object" && "resourceType" in (r as object);
Eif (isResource(data) && (data as FhirBundle).resourceType === "Bundle") {
return data as FhirBundle;
}
const list = Array.isArray(data) ? data : isResource(data) ? [data] : null;
Iif (!list || !list.every(isResource)) return null;
return {
resourceType: "Bundle",
type: "collection",
total: list.length,
entry: list.map((resource) => ({
resource: resource as FhirBundleEntry["resource"],
})),
};
}
interface DataPayload {
resourceType: string;
type: string;
total: number;
containedResourceTypes?: string[];
link?: { relation: string; url: string }[];
provider?: string;
transferredAt?: string;
sizeBytes?: number;
}
/* ── FHIR Resource colors ── */
const FHIR_RESOURCE_COLORS: Record<string, string> = {
Patient:
"bg-(--role-holder-bg) text-(--role-holder-text) border-(--role-holder-border)",
Observation:
"bg-(--role-trust-bg) text-(--role-trust-text) border-(--role-trust-border)",
Condition:
"bg-(--role-hdab-bg) text-(--role-hdab-text) border-(--role-hdab-border)",
Encounter:
"bg-(--role-user-bg) text-(--role-user-text) border-(--role-user-border)",
MedicationRequest:
"bg-(--role-admin-bg) text-(--role-admin-text) border-(--role-admin-border)",
DiagnosticReport:
"bg-(--role-trust-bg) text-(--role-trust-text) border-(--role-trust-border)",
Immunization:
"bg-(--role-patient-bg) text-(--role-patient-text) border-(--role-patient-border)",
AllergyIntolerance:
"bg-(--role-admin-bg) text-(--role-admin-text) border-(--role-admin-border)",
Procedure:
"bg-(--role-patient-bg) text-(--role-patient-text) border-(--role-patient-border)",
CarePlan:
"bg-(--role-user-bg) text-(--role-user-text) border-(--role-user-border)",
};
function fhirColor(type: string): string {
return (
FHIR_RESOURCE_COLORS[type] ||
"bg-(--surface-2) text-(--text-primary) border-gray-600"
);
}
/* ── Collapsible JSON Tree ── */
function JsonNode({ data, depth = 0 }: { data: unknown; depth?: number }) {
const [collapsed, setCollapsed] = useState(depth > 1);
Iif (data === null || data === undefined) {
return <span className="text-(--text-secondary)">null</span>;
}
Iif (typeof data === "boolean") {
return <span className="text-(--role-hdab-text)">{String(data)}</span>;
}
Iif (typeof data === "number") {
return <span className="text-(--role-holder-text)">{data}</span>;
}
if (typeof data === "string") {
return <span className="text-(--role-user-text)">"{data}"</span>;
}
Iif (Array.isArray(data)) {
if (data.length === 0)
return <span className="text-(--text-secondary)">[]</span>;
return (
<span>
<button
onClick={() => setCollapsed(!collapsed)}
className="text-(--text-secondary) hover:text-(--text-primary) inline-flex items-center"
>
{collapsed ? <ChevronRight size={12} /> : <ChevronDown size={12} />}
<span className="text-(--text-secondary) text-xs ml-0.5">
[{data.length}]
</span>
</button>
{!collapsed && (
<div className="ml-4 border-l border-(--border) pl-2">
{data.map((item, i) => (
<div key={i}>
<span className="text-gray-600 text-xs mr-1">{i}:</span>
<JsonNode data={item} depth={depth + 1} />
</div>
))}
</div>
)}
</span>
);
}
Eif (typeof data === "object") {
const entries = Object.entries(data as Record<string, unknown>);
Iif (entries.length === 0) {
return <span className="text-(--text-secondary)">{"{}"}</span>;
}
return (
<span>
<button
onClick={() => setCollapsed(!collapsed)}
className="text-(--text-secondary) hover:text-(--text-primary) inline-flex items-center"
>
{collapsed ? <ChevronRight size={12} /> : <ChevronDown size={12} />}
<span className="text-(--text-secondary) text-xs ml-0.5">
{"{"}
{entries.length}
{"}"}
</span>
</button>
{!collapsed && (
<div className="ml-4 border-l border-(--border) pl-2">
{entries.map(([key, val]) => (
<div key={key}>
<span className="text-(--accent)">{key}</span>
<span className="text-(--text-secondary)">: </span>
<JsonNode data={val} depth={depth + 1} />
</div>
))}
</div>
)}
</span>
);
}
return <span>{String(data)}</span>;
}
/* ── FHIR Resource Card ── */
function FhirResourceCard({ entry }: { entry: FhirBundleEntry }) {
const [expanded, setExpanded] = useState(false);
const r = entry.resource;
const type = r.resourceType;
// Build summary line based on resource type
let summary = "";
if (type === "Patient") {
const name = (
r.name as Array<{ given?: string[]; family?: string; text?: string }>
)?.[0];
// The hub's facade writes the name as text (neo4j-proxy fhir-resource.ts).
summary =
`${name?.given?.[0] || ""} ${name?.family || ""}`.trim() ||
name?.text ||
r.id ||
"";
} else if (type === "Observation") {
const code = (r.code as { text?: string })?.text || "";
const val = r.valueQuantity as
| { value?: number; unit?: string }
| undefined;
summary = val ? `${code}: ${val.value} ${val.unit}` : code;
} else if (type === "Condition") {
summary = (r.code as { text?: string })?.text || "";
} else if (type === "MedicationRequest") {
summary = (r.medicationCodeableConcept as { text?: string })?.text || "";
} else if (type === "Encounter") {
const cls = r.class as { display?: string } | undefined;
summary = `${cls?.display || "visit"} — ${r.status}`;
} else if (type === "DiagnosticReport") {
summary = (r.code as { text?: string })?.text || "";
} else Iif (type === "CarePlan") {
summary = (r.title as string) || "";
} else if (type === "Immunization") {
summary = (r.vaccineCode as { text?: string })?.text || "";
} else Iif (type === "AllergyIntolerance") {
summary = (r.code as { text?: string })?.text || "";
} else if (type === "Procedure") {
summary = (r.code as { text?: string })?.text || "";
} else E{
summary = r.id || "";
}
return (
<div className="border border-(--border) rounded-lg overflow-hidden">
<button
onClick={() => setExpanded(!expanded)}
className="w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-(--surface-2)/50 transition-colors"
>
{expanded ? (
<ChevronDown size={12} className="text-(--text-secondary)" />
) : (
<ChevronRight size={12} className="text-(--text-secondary)" />
)}
<span
className={`text-[10px] px-1.5 py-0.5 rounded border ${fhirColor(
type,
)}`}
>
{type}
</span>
<span className="text-xs text-(--text-primary) truncate flex-1">
{summary}
</span>
<span className="text-[10px] text-gray-600">{r.id}</span>
</button>
{expanded && (
<div className="px-3 pb-3 pt-1 bg-(--surface)/50 font-mono text-xs overflow-x-auto max-h-64 overflow-y-auto">
<JsonNode data={r} depth={0} />
</div>
)}
</div>
);
}
/* ── FHIR Viewer Panel ── */
function FhirViewerPanel({
transfer,
bundle,
onClose,
}: {
transfer: Transfer;
bundle: FhirBundle | null;
onClose: () => void;
}) {
const t = useT("research");
const tc = useT("common");
const [viewMode, setViewMode] = useState<"resources" | "json">("resources");
const [copied, setCopied] = useState(false);
const payload = transfer.dataPayload as DataPayload | undefined;
const aId =
transfer.assetId || f(transfer as Record<string, unknown>, "assetId");
const resourceCounts = useMemo(() => {
if (!bundle?.entry) return {};
const counts: Record<string, number> = {};
bundle.entry.forEach((e) => {
const rt = e.resource.resourceType;
counts[rt] = (counts[rt] || 0) + 1;
});
return counts;
}, [bundle]);
const copyJson = useCallback(() => {
const data = bundle || payload;
Eif (data) {
navigator.clipboard.writeText(JSON.stringify(data, null, 2));
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
}, [bundle, payload]);
return (
<div className="border border-layer2/50 rounded-xl overflow-hidden bg-(--surface)/80 mt-3">
{/* Header */}
<div className="flex items-center justify-between px-4 py-3 bg-layer2/10 border-b border-layer2/30">
<div className="flex items-center gap-2">
<FileJson2 size={16} className="text-teal-800 dark:text-teal-300" />
<span className="text-sm font-medium text-(--text-primary)">
{t("FHIR Data: {name}", {
name: aId ? assetLabel(aId as string) : "Bundle",
})}
</span>
{payload?.provider && (
<span className="text-[10px] px-1.5 py-0.5 rounded-sm bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200">
{t("from {provider}", { provider: payload.provider })}
</span>
)}
</div>
<div className="flex items-center gap-2">
<Link
href={`/graph?highlight=${encodeURIComponent(
aId ? assetLabel(aId as string) : "FHIR",
)}`}
className="flex items-center gap-1 text-[11px] text-(--accent) hover:underline transition-colors"
>
{t("View in Graph")} <Network size={10} />
</Link>
<button
onClick={onClose}
aria-label={tc("Close")}
className="text-(--text-secondary) hover:text-(--text-primary)"
>
<X size={16} />
</button>
</div>
</div>
{/* Summary stats */}
<div className="px-4 py-3 border-b border-(--border) grid grid-cols-2 sm:grid-cols-4 gap-3">
<div>
<div className="text-[10px] text-(--text-secondary) uppercase tracking-wide">
{t("Total Resources")}
</div>
<div className="text-lg font-semibold text-(--text-primary)">
{bundle?.entry?.length ?? payload?.total ?? "—"}
</div>
</div>
<div>
<div className="text-[10px] text-(--text-secondary) uppercase tracking-wide">
{t("Resource Types")}
</div>
<div className="text-lg font-semibold text-(--text-primary)">
{Object.keys(resourceCounts).length ||
payload?.containedResourceTypes?.length ||
"—"}
</div>
</div>
<div>
<div className="text-[10px] text-(--text-secondary) uppercase tracking-wide">
{t("Transferred")}
</div>
<div className="text-sm text-(--text-primary)">
{payload?.transferredAt
? new Date(payload.transferredAt).toLocaleString(undefined, {
dateStyle: "short",
timeStyle: "short",
})
: "—"}
</div>
</div>
<div>
<div className="text-[10px] text-(--text-secondary) uppercase tracking-wide">
{t("Size")}
</div>
<div className="text-sm text-(--text-primary)">
{payload?.sizeBytes
? `${(payload.sizeBytes / 1024).toFixed(0)} KB`
: "—"}
</div>
</div>
</div>
{/* Resource type pills */}
{(Object.keys(resourceCounts).length > 0 ||
(payload?.containedResourceTypes?.length ?? 0) > 0) && (
<div className="px-4 py-2 border-b border-(--border) flex flex-wrap gap-1.5">
{Object.keys(resourceCounts).length > 0
? Object.entries(resourceCounts).map(([type, count]) => (
<span
key={type}
className={`text-[10px] px-2 py-0.5 rounded-full border ${fhirColor(
type,
)}`}
>
{type} ({count})
</span>
))
: payload?.containedResourceTypes?.map((type) => (
<span
key={type}
className={`text-[10px] px-2 py-0.5 rounded-full border ${fhirColor(
type,
)}`}
>
{type}
</span>
))}
</div>
)}
{/* View mode tabs & actions */}
<div className="px-4 py-2 border-b border-(--border) flex items-center justify-between">
<div className="flex gap-1">
<button
onClick={() => setViewMode("resources")}
className={`text-xs px-2.5 py-1 rounded ${
viewMode === "resources"
? "bg-layer2/20 text-teal-800 dark:text-teal-300"
: "text-(--text-secondary) hover:text-(--text-primary)"
}`}
>
{t("Resources")}
</button>
<button
onClick={() => setViewMode("json")}
className={`text-xs px-2.5 py-1 rounded ${
viewMode === "json"
? "bg-layer2/20 text-teal-800 dark:text-teal-300"
: "text-(--text-secondary) hover:text-(--text-primary)"
}`}
>
{t("Raw JSON")}
</button>
</div>
<button
onClick={copyJson}
className="flex items-center gap-1 text-[11px] text-(--text-secondary) hover:text-(--text-primary)"
>
<Copy size={10} />
{copied ? t("Copied!") : t("Copy")}
</button>
</div>
{/* Content */}
<div className="max-h-[500px] overflow-y-auto">
{viewMode === "resources" && bundle?.entry ? (
<div className="p-3 space-y-1.5">
{bundle.entry.map((entry, i) => (
<FhirResourceCard key={entry.resource.id || i} entry={entry} />
))}
</div>
) : viewMode === "resources" && !bundle?.entry ? (
<div className="p-6 text-center text-sm text-(--text-secondary)">
<FileJson2 size={24} className="mx-auto mb-2 text-gray-600" />
<p>{t("Full FHIR Bundle data not available for this transfer.")}</p>
<p className="text-xs mt-1">
{t("Transfer metadata: {n} resources ({types})", {
n: payload?.total ?? 0,
types:
payload?.containedResourceTypes?.join(", ") || t("unknown"),
})}
</p>
</div>
) : (
<pre className="p-4 font-mono text-xs text-(--text-primary) overflow-x-auto">
{JSON.stringify(bundle || payload, null, 2)}
</pre>
)}
</div>
</div>
);
}
/* ── Main Page ── */
export default function DataTransferPage() {
const tc = useT("common");
return (
<Suspense
fallback={
<div className="flex items-center gap-2 text-(--text-secondary) p-10">
<Loader2 size={16} className="animate-spin" />
{tc("Loading…")}
</div>
}
>
<DataTransferContent />
</Suspense>
);
}
function DataTransferContent() {
const t = useT("research");
const tn = useT("nav");
const searchParams = useSearchParams();
const preselectedCtx = searchParams.get("participantId") || "";
const preContractId = searchParams.get("contractId") || "";
const [participants, setParticipants] = useState<ParticipantCtx[]>([]);
const [transfers, setTransfers] = useState<Transfer[]>([]);
const [agreements, setAgreements] = useState<Agreement[]>([]);
const [loading, setLoading] = useState(true);
const [selectedCtx, setSelectedCtx] = useState(preselectedCtx);
// Starting a transfer: the agreement it runs under, while it is sent.
const [initiating, setInitiating] = useState<string | null>(null);
const [result, setResult] = useState<string | null>(null);
const [resultIsError, setResultIsError] = useState(false);
const [refreshing, setRefreshing] = useState(false);
// The one list: its filter, search and length (#894).
const [stageFilter, setStageFilter] = useState<Stage | "all">("all");
const [search, setSearch] = useState("");
const [showAll, setShowAll] = useState(false);
const PAGE_SIZE = 25;
// Which SPE holds which open transfer, from the duplicate check.
const [imports, setImports] = useState<Record<string, ExistingImport[]>>({});
// FHIR viewer
const [viewingTransferId, setViewingTransferId] = useState<string | null>(
null,
);
const [fhirBundles, setFhirBundles] = useState<Record<
string,
FhirBundle
> | null>(null);
const [loadingFhir, setLoadingFhir] = useState(false);
// The real data of a STARTED connector transfer, fetched through the
// consumer's siglet (api/transfers/[id]/data). Before #876 "View FHIR"
// showed the bundled sample file for every row, real ones included.
// Which My data rows show their details (#876).
const [detailsOpen, setDetailsOpen] = useState<Set<string>>(new Set());
const toggleDetails = (id: string) =>
setDetailsOpen((prev) => {
const next = new Set(prev);
Iif (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
const [liveData, setLiveData] = useState<
Record<
string,
{ loading: boolean; error?: string; expired?: boolean; result?: LiveData }
>
>({});
useEffect(() => {
fetchApi("/api/participants")
.then((r) => (r.ok ? r.json() : []))
.then((d) => {
const list: ParticipantCtx[] = Array.isArray(d)
? d
: d.participants || [];
setParticipants(list);
if (!selectedCtx && list.length > 0) {
// The data user transfers; a data holder is the other side. As on
// /negotiate (#876).
setSelectedCtx(
(list.find((p) => p.role === "DATA_USER") ?? list[0])["@id"],
);
} else Eif (list.length === 0) {
setLoading(false);
}
})
.catch(() => {
setLoading(false);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Load transfers + agreements when participant changes
useEffect(() => {
if (!selectedCtx) return;
setLoading(true);
Promise.all([
fetchApi(
`/api/transfers?participantId=${selectedCtx}&limit=${DEFAULT_PAGE_SIZE}`,
)
.then((r) => (r.ok ? r.json() : []))
.then((d) => (Array.isArray(d) ? d : d.transfers || []))
.catch(() => []),
fetchApi(
`/api/negotiations?participantId=${selectedCtx}&limit=${DEFAULT_PAGE_SIZE}`,
)
.then((r) => (r.ok ? r.json() : []))
.then((d) => {
const negs = Array.isArray(d) ? d : d.negotiations || [];
// Extract agreements from FINALIZED negotiations
return negs
.filter(
(n: Record<string, unknown>) =>
f(n, "state") === "FINALIZED" && f(n, "contractAgreementId"),
)
.map((n: Record<string, unknown>) => ({
"@id": f(n, "contractAgreementId"),
assetId: f(n, "assetId") || n.assetId,
counterPartyId: f(n, "counterPartyId"),
counterPartyAddress: f(n, "counterPartyAddress"),
type: f(n, "type"),
// Only a connector's agreement can be read back for Details.
source: n.source,
// When it was negotiated: the list is ordered by it (#894).
agreedAt: Number(n.createdAt ?? n["edc:createdAt"]) || 0,
}));
})
.catch(() => []),
]).then(([tList, aList]) => {
setTransfers(tList as Transfer[]);
setAgreements(aList as Agreement[]);
setLoading(false);
});
}, [selectedCtx]);
// Follow a transfer the connector is still working on (INITIAL, REQUESTED)
// until it settles; give up after two minutes and leave it to Refresh.
const settling = useMemo(() => transfers.some(isSettling), [transfers]);
useEffect(() => {
if (!selectedCtx || !settling) return;
let polls = 0;
const timer = setInterval(() => {
polls += 1;
Iif (polls > SETTLING_POLL_LIMIT) {
clearInterval(timer);
return;
}
fetchApi(
`/api/transfers?participantId=${selectedCtx}&limit=${DEFAULT_PAGE_SIZE}`,
)
.then((r) => (r.ok ? r.json() : null))
.then((d) => {
Eif (d) setTransfers(Array.isArray(d) ? d : d.transfers || []);
})
.catch(() => {
/* the next poll tries again */
});
}, SETTLING_POLL_MS);
return () => clearInterval(timer);
}, [selectedCtx, settling]);
const refreshTransfers = async () => {
Iif (!selectedCtx) return;
setRefreshing(true);
try {
const res = await fetchApi(
`/api/transfers?participantId=${selectedCtx}&limit=${DEFAULT_PAGE_SIZE}`,
);
const d = await res.json();
setTransfers(Array.isArray(d) ? d : d.transfers || []);
} catch {
/* ignore */
}
setRefreshing(false);
};
const startTransfers = async (ids: string[]) => {
Iif (ids.length === 0) return;
setInitiating(ids[0]);
setResult(null);
setResultIsError(false);
const started: string[] = [];
const errors: string[] = [];
// A transfer the API recorded rather than sent, because the contract exists
// only in this demonstrator. Reported separately: "started" would claim a
// DSP transfer that never happened.
const demoResults: string[] = [];
for (const agrId of ids) {
const agr = agreements.find((a) => a["@id"] === agrId);
const assetId = agr ? f(agr as Record<string, unknown>, "assetId") : "";
const counterPartyAddress =
(agr as Record<string, unknown>)?.counterPartyAddress ||
"http://controlplane:8082/api/dsp";
try {
const res = await fetchApi("/api/transfers", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
participantId: selectedCtx,
contractId: agrId,
assetId,
counterPartyAddress,
}),
});
if (res.ok) {
const data = (await res.json().catch(() => ({}))) as Record<
string,
unknown
>;
const label = assetLabel(assetId || agrId.slice(0, 12));
if (data.demo === true) {
demoResults.push(label);
} else {
started.push(label);
}
} else {
const err = (await res.json().catch(() => ({}))) as Record<
string,
string
>;
// A permit refusal says why (Art. 61(1)): whose permit was missing,
// revoked or expired. The bare "No data permit covers this
// transfer" left the user guessing.
const detail =
err.detail ||
[err.error, err.reason].filter(Boolean).join(": ") ||
`HTTP ${res.status}`;
errors.push(
t("Error: {name}: {detail}", {
name: assetLabel(assetId || agrId.slice(0, 12)),
detail,
}),
);
}
} catch {
errors.push(
t("Error: {name}: {detail}", {
name: assetLabel(assetId || agrId.slice(0, 12)),
detail: t("Network error"),
}),
);
}
}
if (errors.length === 0) {
const lines: string[] = [];
if (started.length === 1) {
lines.push(t("Transfer started for {name}", { name: started[0] }));
} else Iif (started.length > 1) {
lines.push(
t("{n} transfers started successfully", { n: started.length }),
);
}
if (demoResults.length > 0) {
lines.push(
t(
"Demo transfer recorded for {names}: the contract exists only in this demonstrator, so nothing was sent to the connector and the FHIR payload below is the demonstrator's own.",
{ names: demoResults.join(", ") },
),
);
}
setResult(lines.join("\n"));
} else {
setResultIsError(true);
setResult(errors.join("\n"));
}
await refreshTransfers();
setInitiating(null);
};
// The imports of open transfers and of transfers closed after their import,
// for the stage and the import status (#876).
const liveIds = useMemo(
() =>
transfers
.filter((tr) => isLiveTransfer(tr) || isClosedAfterImport(tr))
.map((tr) => tr["@id"]),
[transfers],
);
const loadImports = useCallback(async () => {
if (liveIds.length === 0) {
setImports({});
return;
}
const found: Record<string, ExistingImport[]> = {};
for (let i = 0; i < liveIds.length; i += 100) {
const ids = liveIds
.slice(i, i + 100)
.map(encodeURIComponent)
.join(",");
try {
const r = await fetchApi(`/api/spe-imports?ids=${ids}`);
const b = (await r.json()) as { imports?: ExistingImport[] };
for (const imp of b.imports ?? []) {
(found[imp.datasetId] ??= []).push(imp);
}
} catch {
/* no import status: the rows still work */
}
}
setImports(found);
}, [liveIds]);
useEffect(() => {
void loadImports();
}, [loadImports]);
/** One row per agreement, its transfers newest first, and its stage. */
const rows = useMemo(() => {
const ts = (tr: Transfer) =>
(tr.stateTimestamp as number) ||
(tr["edc:stateTimestamp"] as number) ||
0;
const byContract = new Map<string, Transfer[]>();
for (const tr of transfers) {
const cid =
f(tr as Record<string, unknown>, "contractId") ||
`transfer:${tr["@id"]}`;
byContract.set(cid, [...(byContract.get(cid) ?? []), tr]);
}
const ids = Array.from(
new Set([...agreements.map((a) => a["@id"]), ...byContract.keys()]),
);
return (
ids
.map((id) => {
const agreement = agreements.find((a) => a["@id"] === id) ?? null;
const trs = [...(byContract.get(id) ?? [])].sort(
(a, b) => ts(b) - ts(a),
);
const states = trs.map((tr) =>
f(tr as Record<string, unknown>, "state").toUpperCase(),
);
const open = trs.find(isLiveTransfer) ?? null;
// The latest transfer ended with its import into an SPE.
const closedImport =
!open && trs[0] && isClosedAfterImport(trs[0]) ? trs[0] : null;
const started =
open ??
trs.find(
(tr) =>
f(tr as Record<string, unknown>, "state").toUpperCase() ===
"STARTED",
) ??
null;
const stage: Stage =
(open && (imports[open["@id"]]?.length ?? 0) > 0) || closedImport
? "imported"
: started
? "ready"
: states.some(
(st) =>
!["COMPLETED", "TERMINATED", "SUSPENDED"].includes(st),
)
? "progress"
: states.includes("COMPLETED")
? "done"
: "agreed";
const assetId =
(agreement && f(agreement as Record<string, unknown>, "assetId")) ||
(trs[0] &&
(trs[0].assetId ||
f(trs[0] as Record<string, unknown>, "assetId")));
const providerDid = agreement
? f(agreement as Record<string, unknown>, "counterPartyId")
: "";
return {
id,
agreement,
transfers: trs,
open,
started,
stage,
name: assetId ? assetLabel(assetId as string) : id.slice(0, 12),
assetId: (assetId as string) || "",
providerDid,
provider: providerDid ? didToName(providerDid) : "",
isProviderSide:
!!agreement &&
f(agreement as Record<string, unknown>, "type") === "PROVIDER",
sample: agreement
? agreement.source !== "controlplane"
: trs.every((tr) => tr.source !== "controlplane"),
lastFailed: states[0] === "TERMINATED" && !closedImport,
closedImport,
lastAt: trs[0] ? ts(trs[0]) : 0,
agreedAt: Number(agreement?.agreedAt) || 0,
importedAt:
open ?? closedImport
? (imports[(open ?? closedImport)!["@id"]] ?? [])
.map((i) => i.importedAt)
.sort()
.pop() ?? ""
: "",
};
})
// Newest contract first. A row keeps its place when its stage
// changes, so an import does not move it away (#894).
.sort((a, b) => (b.agreedAt || b.lastAt) - (a.agreedAt || a.lastAt))
);
}, [agreements, transfers, imports]);
const stageCounts = useMemo(() => {
const c: Record<string, number> = { all: rows.length };
for (const r of rows) c[r.stage] = (c[r.stage] ?? 0) + 1;
return c;
}, [rows]);
const shownRows = useMemo(() => {
const q = search.trim().toLowerCase();
return rows.filter(
(r) =>
(stageFilter === "all" || r.stage === stageFilter) &&
(!q ||
`${r.name} ${r.assetId} ${r.provider} ${r.id}`
.toLowerCase()
.includes(q)),
);
}, [rows, stageFilter, search]);
// A link to one agreement (?contractId=) shows it even past the first page.
const pageRows =
showAll || shownRows.length <= PAGE_SIZE
? shownRows
: shownRows.slice(0, PAGE_SIZE);
const linked =
preContractId && !pageRows.some((r) => r.id === preContractId)
? shownRows.find((r) => r.id === preContractId)
: undefined;
const visibleRows = linked ? [linked, ...pageRows] : pageRows;
// The linked agreement opens with its details and is scrolled to.
useEffect(() => {
Eif (!preContractId || loading) return;
setDetailsOpen((prev) => new Set(prev).add(`agr:${preContractId}`));
document
.getElementById(`contract-${preContractId}`)
?.scrollIntoView?.({ behavior: "smooth", block: "start" });
}, [preContractId, loading]);
const loadLiveData = async (
transferId: string,
): Promise<LiveData | undefined> => {
const known = liveData[transferId];
Iif (known?.result) return known.result;
setLiveData((m) => ({ ...m, [transferId]: { loading: true } }));
try {
const res = await fetchApi(
`/api/transfers/${encodeURIComponent(
transferId,
)}/data?participantId=${encodeURIComponent(selectedCtx)}`,
);
const body = (await res.json()) as LiveData & {
error?: string;
detail?: string;
expired?: boolean;
};
if (!res.ok) {
const error = [body.error, body.detail].filter(Boolean).join(": ");
setLiveData((m) => ({
...m,
[transferId]: { loading: false, error, expired: !!body.expired },
}));
return undefined;
}
setLiveData((m) => ({
...m,
[transferId]: { loading: false, result: body },
}));
return body;
} catch (err) {
setLiveData((m) => ({
...m,
[transferId]: { loading: false, error: String(err) },
}));
return undefined;
}
};
const downloadLiveData = async (transferId: string, name: string) => {
const result = await loadLiveData(transferId);
if (!result) return;
const blob = new Blob([JSON.stringify(result.data, null, 2)], {
type: "application/json",
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `${name}-${transferId.slice(0, 8)}.json`;
a.click();
URL.revokeObjectURL(url);
};
/** When this agreement's newest STARTED transfer opened, or 0. */
const openedAtOf = (agrId: string): number =>
transfers
.filter(
(tr) =>
f(tr as Record<string, unknown>, "contractId") === agrId &&
f(tr as Record<string, unknown>, "state").toUpperCase() === "STARTED",
)
.reduce(
(max, tr) => Math.max(max, (tr.stateTimestamp as number) || 0),
0,
);
// Load FHIR bundles on demand
const openFhirViewer = async (transferId: string) => {
if (viewingTransferId === transferId) {
setViewingTransferId(null);
return;
}
setViewingTransferId(transferId);
const row = transfers.find((tr) => tr["@id"] === transferId);
if (row && isLiveTransfer(row)) {
await loadLiveData(transferId);
return;
}
Eif (!fhirBundles) {
setLoadingFhir(true);
try {
const basePath =
process.env.NEXT_PUBLIC_STATIC_EXPORT === "true"
? process.env.NEXT_PUBLIC_BASE_PATH ||
"/MinimumViableHealthDataspacev2"
: "";
const res = await fetch(`${basePath}/mock/fhir_bundles.json`);
Eif (res.ok) {
setFhirBundles(await res.json());
}
} catch {
/* ignore */
}
setLoadingFhir(false);
}
};
/** The data of one transfer, opened in its contract's row. */
const viewerFor = (tr: Transfer) => {
const aId = tr.assetId || f(tr as Record<string, unknown>, "assetId");
const live = isLiveTransfer(tr) ? liveData[tr["@id"]] : null;
const liveBundle = live?.result ? toBundle(live.result.data) : null;
const bundle = isLiveTransfer(tr)
? liveBundle
: fhirBundles?.[aId as string] ?? null;
const viewerLoading = isLiveTransfer(tr) ? !!live?.loading : loadingFhir;
if (viewerLoading) {
return (
<div className="flex items-center gap-2 text-(--text-secondary) py-4 justify-center">
<Loader2 size={14} className="animate-spin" />
{isLiveTransfer(tr)
? t("Fetching the data from the provider…")
: t("Loading FHIR data…")}
</div>
);
}
if (live?.error) return null;
Iif (live?.result && !liveBundle) {
return (
<div className="mt-3 font-mono text-xs overflow-x-auto max-h-96 overflow-y-auto p-3 border border-(--border) rounded-lg">
<JsonNode data={live.result.data} depth={0} />
</div>
);
}
return (
<FhirViewerPanel
transfer={tr}
bundle={bundle}
onClose={() => setViewingTransferId(null)}
/>
);
};
return (
<div className="min-h-screen bg-(--bg)">
<div className="max-w-5xl mx-auto px-6 py-10">
<PageIntro
title={t("Data Transfers")}
icon={ArrowRightLeft}
description={t(
"Monitor and initiate EDC-V data transfers via the DSP Transfer Process (Signalling Protocol). Once a contract is agreed, start a transfer to pull FHIR or OMOP data from the provider's data plane.",
)}
prevStep={{ href: "/negotiate", label: t("Contract Negotiation") }}
nextStep={{ href: "/admin", label: tn("Operator Dashboard") }}
infoText={t(
"The DSP Signalling Protocol defines the transfer state machine: REQUESTED → STARTED → COMPLETED (or TERMINATED on failure). Each transfer is secured by EDC-V access tokens issued during the STARTED phase.",
)}
docLink={{
href: "https://eclipse-dataspace-protocol-base.github.io/DataspaceProtocol/2025-1/#transfer-protocol",
label: t("DSP Transfer Process Spec"),
external: true,
}}
/>
{/* Participant selector */}
<div className="mb-6">
<label className="text-xs text-(--text-secondary) mb-1 block">
{t("Requesting as (your participant)")}
</label>
<select
aria-label={t("Requesting as (your participant)")}
value={selectedCtx}
onChange={(e) => setSelectedCtx(e.target.value)}
className="w-full max-w-md px-3 py-2 bg-(--surface-2) border border-gray-600 rounded-sm text-sm"
>
{participants.map((p) => (
<option key={p["@id"]} value={p["@id"]}>
{displayParticipant(p)}
{p.role ? ` [${p.role}]` : ""}
</option>
))}
</select>
</div>
{/* ── My contracts and data: one list, one row per agreement (#894) ── */}
<section
data-testid="contracts-list"
className="border border-(--border) rounded-xl p-5 mb-8"
>
<div className="flex flex-wrap items-center justify-between gap-2 mb-1">
<h2 className="flex items-center gap-2 font-semibold text-sm">
<Play size={16} className="text-teal-800 dark:text-teal-300" />
{t("My contracts and data")}
</h2>
<button
type="button"
onClick={() => {
void refreshTransfers();
void loadImports();
}}
disabled={refreshing}
className="flex items-center gap-1 text-xs text-(--text-secondary) hover:text-(--text-primary) disabled:opacity-50"
>
<RefreshCw
size={12}
className={refreshing ? "animate-spin" : ""}
/>
{t("Refresh")}
</button>
</div>
<p className="text-xs text-(--text-secondary) mb-4">
{t(
"Each agreed contract once, with where it stands: start the transfer, open or download the data, import it into an SPE. Its transfer history is under Details.",
)}
</p>
{result && (
<div
className={`mb-4 p-3 rounded text-sm whitespace-pre-line ${
resultIsError
? "bg-(--role-admin-bg) border border-(--role-admin-border) text-(--role-admin-text)"
: "bg-(--role-user-bg) border border-(--role-user-border) text-(--role-user-text)"
}`}
>
{result}
</div>
)}
{loading ? (
<div className="flex items-center gap-2 text-(--text-secondary)">
<Loader2 size={16} className="animate-spin" />
{t("Loading transfers…")}
</div>
) : rows.length === 0 ? (
<p className="text-xs text-(--text-secondary)">
<T
area="research"
tags={{
a: (
<Link
href="/negotiate"
className="text-teal-800 dark:text-teal-300 hover:underline"
/>
),
}}
>
{
"No finalized agreements found. Complete a <a>contract negotiation</a> first."
}
</T>
</p>
) : (
<>
{/* Filter and search */}
<div className="flex flex-wrap items-center gap-1.5 mb-3">
{(["all", ...STAGE_ORDER] as const).map((st) => (
<button
key={st}
type="button"
data-testid={`stage-filter-${st}`}
aria-pressed={stageFilter === st}
onClick={() => {
setStageFilter(st);
setShowAll(false);
}}
className={`text-xs px-2.5 py-1 rounded-full transition-colors ${
stageFilter === st
? "bg-layer2/20 text-teal-800 dark:text-teal-300 border border-layer2/40"
: "bg-(--surface-2) text-(--text-secondary) border border-(--border) hover:border-gray-500"
}`}
>
{st === "all" ? t("All") : t(STAGE_LABEL[st])} (
{stageCounts[st] ?? 0})
</button>
))}
<input
type="search"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder={t("Search dataset, provider or contract")}
aria-label={t("Search dataset, provider or contract")}
className="ml-auto min-w-[14rem] px-2 py-1 text-xs bg-(--surface-2) border border-(--border) rounded"
/>
</div>
{shownRows.length === 0 && (
<p className="text-sm text-(--text-secondary)">
{t("No contract matches.")}
</p>
)}
<div className="space-y-2">
{visibleRows.map((row) => {
const live = row.open;
const entry = live ? liveData[live["@id"]] : undefined;
const detailsKey = `agr:${row.id}`;
const viewing = row.transfers.find(
(tr) => tr["@id"] === viewingTransferId,
);
return (
<div
key={row.id}
id={`contract-${row.id}`}
data-testid="contract-row"
data-stage={row.stage}
className={`p-3 rounded-lg border bg-(--surface-2)/50 ${
row.id === preContractId
? "border-layer2"
: "border-(--border)"
}`}
>
<div className="flex flex-wrap items-center gap-3">
<div className="flex-1 min-w-0">
<p className="flex flex-wrap items-center gap-2 text-sm font-medium text-(--text-primary)">
{row.name}
<span
data-testid="contract-stage"
className={`text-[10px] px-1.5 py-0.5 rounded-sm ${
STAGE_STYLE[row.stage]
}`}
>
{t(STAGE_LABEL[row.stage])}
</span>
{row.sample && (
<RowSourceBadge
source={
(row.agreement?.source as
| RowSource
| undefined) ??
(row.transfers[0]?.source as
| RowSource
| undefined) ??
"mock"
}
/>
)}
</p>
<p className="text-xs text-(--text-secondary)">
{row.isProviderSide
? t("Consumer:")
: t("Provider:")}{" "}
{row.provider || "—"}
{row.agreedAt > 0 && (
<span data-testid="contract-agreed">
{" · "}
{t("agreed {date}", {
date: when(row.agreedAt),
})}
</span>
)}
{row.importedAt && (
<span data-testid="contract-imported">
{" · "}
{t("imported {date}", {
date: when(row.importedAt),
})}
</span>
)}
{row.lastFailed && row.stage === "agreed" && (
<span className="ml-2 text-(--badge-inactive-text)">
{t("the last transfer failed")}
</span>
)}
{entry?.result &&
` · ${Math.max(
1,
Math.round(entry.result.bytes / 1024),
)} KB`}
</p>
{entry?.error &&
(entry.expired ? (
<p
data-testid="transfer-expired"
className="text-xs text-(--badge-inactive-text) mt-1"
>
{t(
"The access to this transfer has expired. Start a new transfer to get the data again.",
)}
</p>
) : (
<p className="text-xs text-(--badge-inactive-text) mt-1">
{t("Could not fetch the data: {detail}", {
detail: entry.error,
})}
</p>
))}
</div>
{/* The one next action for this stage */}
{(row.stage === "agreed" ||
row.stage === "done" ||
row.closedImport) &&
row.agreement &&
!row.isProviderSide && (
<button
type="button"
data-testid="start-transfer"
disabled={initiating !== null}
onClick={() => void startTransfers([row.id])}
className="flex items-center gap-1 text-xs px-2 py-1 rounded bg-(--accent) text-white dark:text-gray-900 hover:bg-(--accent-hover) disabled:opacity-50"
>
{initiating === row.id ? (
<Loader2 size={12} className="animate-spin" />
) : (
<ArrowRightLeft size={12} />
)}
{row.stage === "done" || row.closedImport
? t("Transfer again")
: t("Start Transfer")}
</button>
)}
{entry?.expired &&
row.agreement &&
!row.isProviderSide && (
<button
type="button"
data-testid="restart-transfer"
disabled={initiating !== null}
onClick={() => void startTransfers([row.id])}
className="flex items-center gap-1 text-xs px-2 py-1 rounded bg-(--accent) text-white dark:text-gray-900 hover:bg-(--accent-hover) disabled:opacity-50"
>
<ArrowRightLeft size={12} />
{t("Start a new transfer")}
</button>
)}
{row.stage === "progress" && (
<span className="flex items-center gap-1 text-xs text-(--text-secondary)">
<Loader2 size={12} className="animate-spin" />
{t("Waiting for the provider")}
</span>
)}
{live && (
<>
<button
type="button"
onClick={() => void openFhirViewer(live["@id"])}
className="flex items-center gap-1 text-xs px-2 py-1 rounded bg-(--role-user-bg) text-(--role-user-text) hover:opacity-80"
>
{entry?.loading ? (
<Loader2 size={12} className="animate-spin" />
) : (
<FileJson2 size={12} />
)}
{viewing === live ? t("Close") : t("Open")}
</button>
<button
type="button"
onClick={() =>
void downloadLiveData(
live["@id"],
row.assetId || "data",
)
}
className="flex items-center gap-1 text-xs px-2 py-1 rounded border border-(--border) text-(--text-primary) hover:border-gray-500"
>
<Download size={12} />
{t("Download")}
</button>
</>
)}
{!live && row.transfers.length > 0 && (
<button
type="button"
onClick={() =>
void openFhirViewer(
(row.started ?? row.transfers[0])["@id"],
)
}
className={`flex items-center gap-1 text-xs px-2 py-1 rounded transition-colors ${
viewing
? "bg-layer2/20 text-teal-800 dark:text-teal-300"
: "bg-(--role-user-bg) text-(--role-user-text) hover:opacity-80"
}`}
>
<FileJson2 size={12} />
{viewing ? t("Hide FHIR") : t("View FHIR")}
</button>
)}
<button
type="button"
aria-expanded={detailsOpen.has(detailsKey)}
onClick={() => toggleDetails(detailsKey)}
className="flex items-center gap-1 text-xs px-2 py-1 rounded border border-(--border) text-(--text-primary) hover:border-gray-500"
>
{detailsOpen.has(detailsKey) ? (
<ChevronDown size={12} />
) : (
<ChevronRight size={12} />
)}
{t("Details")}
</button>
</div>
{live && (
<SpeImport
transferId={live["@id"]}
participantId={selectedCtx}
known={imports[live["@id"]] ?? NO_IMPORTS}
onImported={() => {
void loadImports();
// The import closed the transfer: follow it.
void refreshTransfers();
}}
onRestart={
row.agreement && !row.isProviderSide
? () => void startTransfers([row.id])
: undefined
}
/>
)}
{row.closedImport &&
(() => {
const closed = row.closedImport;
const spe =
closedAfterImportInto(
f(
closed as Record<string, unknown>,
"errorDetail",
),
) ?? "";
const records = (imports[closed["@id"]] ?? []).find(
(i) => i.speId === spe,
)?.records;
return (
<div
data-testid="closed-after-import"
className="mt-2 flex flex-wrap items-center gap-2 rounded border border-green-700/50 bg-green-900/20 px-2 py-1.5 text-xs text-(--text-primary)"
>
<CheckCircle2
size={14}
className="text-green-500"
/>
<span>
{records !== undefined
? t(
"{records} records are in {spe}. The transfer is closed: the access to the provider's data has ended. Start a new transfer to import it again.",
{ records: String(records), spe },
)
: t(
"The data is in {spe}. The transfer is closed: the access to the provider's data has ended. Start a new transfer to import it again.",
{ spe },
)}
</span>
<Link
href={importViewUrl(closed["@id"], spe)}
className="flex items-center gap-1 underline text-(--accent)"
>
<Box size={12} />
{t("Show in 3D")}
</Link>
</div>
);
})()}
{viewing && viewerFor(viewing)}
{detailsOpen.has(detailsKey) && (
<div className="mt-3 space-y-3">
{row.agreement && (
<TransferDetails
participantId={selectedCtx}
contractId={row.id}
openedAt={openedAtOf(row.id)}
providerDid={row.providerDid}
providerName={row.provider || "—"}
consumerName={
participants.find(
(p) => p["@id"] === selectedCtx,
)?.displayName ?? ""
}
sample={row.sample}
/>
)}
<div data-testid="contract-history">
<p className="text-[10px] uppercase tracking-wide text-(--text-secondary) mb-1">
{t("Transfers under this contract ({n})", {
n: row.transfers.length,
})}
</p>
{row.transfers.length === 0 ? (
<p className="text-xs text-(--text-secondary)">
{t("No transfer yet.")}
</p>
) : (
<div className="space-y-2">
{row.transfers.map((tr) => {
const state = f(
tr as Record<string, unknown>,
"state",
);
const transferType =
f(
tr as Record<string, unknown>,
"transferType",
) || "HttpData-PULL";
const timestamp =
(tr.stateTimestamp as number) ||
(tr["edc:stateTimestamp"] as number) ||
0;
// The hub ends a transfer once its data is
// in an SPE; that TERMINATED is the success.
const closedInto = closedAfterImportInto(
f(
tr as Record<string, unknown>,
"errorDetail",
),
);
return (
<div
key={tr["@id"]}
id={`transfer-${tr["@id"]}`}
className="p-2 rounded border border-(--border) space-y-2"
>
<div className="flex flex-wrap items-center gap-2 text-[11px] text-(--text-secondary)">
<span
data-testid="transfer-state"
title={
closedInto
? closedAfterImportReason(
closedInto,
)
: undefined
}
className={`text-xs px-2 py-0.5 rounded-full ${stateBg(
closedInto ? "COMPLETED" : state,
)}`}
>
{state || "UNKNOWN"}
</span>
<span className="px-1.5 py-0.5 rounded-sm bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200">
{transferType}
</span>
{timestamp > 0 && (
<span>
{t("Updated:")}{" "}
{new Date(timestamp).toLocaleString(
undefined,
{
dateStyle: "short",
timeStyle: "short",
},
)}
</span>
)}
<span>
{t("Process: {id}…", {
id: tr["@id"].slice(0, 8),
})}
</span>
{state
?.toUpperCase()
.includes("COMPLETED") &&
(tr.dataPayload as DataPayload)
?.total && (
<span className="text-green-800 dark:text-green-400">
{t("{n} resources transferred", {
n: (
tr.dataPayload as DataPayload
).total,
})}
</span>
)}
</div>
<DspPipeline
state={state}
closedInto={closedInto}
/>
</div>
);
})}
</div>
)}
</div>
</div>
)}
</div>
);
})}
</div>
{shownRows.length > PAGE_SIZE && (
<button
type="button"
onClick={() => setShowAll(!showAll)}
className="mt-3 flex items-center gap-1 text-xs text-teal-800 dark:text-teal-300 hover:text-teal-800/80 dark:hover:text-teal-300/80"
>
{showAll ? (
<>
<ChevronDown size={12} /> {t("Show less")}
</>
) : (
<>
<ChevronRight size={12} />{" "}
{t("Show {n} more", {
n: shownRows.length - PAGE_SIZE,
})}
</>
)}
</button>
)}
</>
)}
</section>
</div>
</div>
);
}
|