Skip to content

Commit c0502aa

Browse files
authored
Merge pull request #36 from LexLuthr/patch-1
fix: UI output formatting
2 parents 1b3fa4e + 1b2e2dc commit c0502aa

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

pages/index.js

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -964,10 +964,10 @@ export default function Home(props) {
964964
<div className="resultItem" key={resultIndex}>
965965
<dl>
966966
<dt>Peer Id:</dt>
967-
<dd>{dataResult.Provider.ID}</dd>
967+
<dd>{dataResult.PeerId}</dd>
968968

969969
<dt>MultiAddress:</dt>
970-
<dd>{dataResult.Provider.Addrs}</dd>
970+
<dd>{dataResult.MultiAddress}</dd>
971971

972972
{dataResult.Protocol && (
973973
<>
@@ -1422,58 +1422,47 @@ function onSearch(
14221422
const res = data.MultihashResults[0];
14231423
const provResults = res.ProviderResults;
14241424

1425-
let providers = {};
1426-
for (let i = 0; i < res.ProviderResults.length; i++) {
1427-
let provider = res.ProviderResults[i];
1428-
if (providers[provider.Provider.ID] == undefined) {
1429-
providers[provider.Provider.ID] = {};
1430-
}
1431-
providers[provider.Provider.ID][provider.ContextID] = provider;
1432-
}
1433-
const pids = Object.keys(providers);
1434-
14351425
let toDisplay = [];
14361426

1437-
for (let i = 0; i < pids.length; i++) {
1438-
let pd = providers[pids[i]];
1439-
let addrs = "";
1440-
let keys = {};
1441-
let contexts = Object.keys(pd);
1442-
for (let j = 0; j < contexts.length; j++) {
1443-
let mdBytes = base64ToBytesArr(pd[contexts[j]].Metadata);
1444-
while (mdBytes.length > 0) {
1445-
let next = popProtocol(mdBytes);
1446-
let name = next[0];
1447-
mdBytes = next[1];
1427+
// Process each ProviderResult individually
1428+
for (let i = 0; i < provResults.length; i++) {
1429+
let providerResult = provResults[i];
1430+
let mdBytes = base64ToBytesArr(providerResult.Metadata);
1431+
let protocols = [];
1432+
let deals = [];
1433+
1434+
// Extract protocols and deal information
1435+
while (mdBytes.length > 0) {
1436+
let next = popProtocol(mdBytes);
1437+
let name = next[0];
1438+
mdBytes = next[1];
1439+
1440+
if (name !== -1) {
14481441
let ctx = toContext(name, mdBytes);
1449-
if (keys[name] == undefined) {
1450-
keys[name] = [];
1451-
}
14521442
if (ctx[0] != "") {
1453-
keys[name].push(ctx[0]);
1443+
deals.push(ctx[0]);
14541444
}
14551445
mdBytes = ctx[1];
1456-
addrs = pd[contexts[j]].Provider.Addrs;
1457-
if (name == -1) {
1458-
break;
1459-
}
1446+
protocols.push(name);
1447+
} else {
1448+
break;
14601449
}
14611450
}
14621451

1463-
for (const [index, value] of Object.keys(keys).entries()) {
1464-
let displayEntry = { ...provResults[i] };
1465-
displayEntry["Protocol"] = value;
1452+
// Build the display entry with correct Provider.Addrs
1453+
let displayEntry = {
1454+
PeerId: providerResult.Provider.ID,
1455+
MultiAddress: providerResult.Provider.Addrs.join(", "),
1456+
Protocol: protocols.join(", "),
1457+
};
14661458

1467-
const deals = [];
1468-
for (const [dealIndex, dealValue] of keys[value].entries()) {
1469-
deals.push(dealValue);
1470-
}
1471-
if (deals.length) {
1472-
displayEntry["DealInfo"] = deals;
1473-
}
1474-
toDisplay.push(displayEntry);
1459+
if (deals.length > 0) {
1460+
displayEntry["DealInfo"] = deals;
14751461
}
1462+
1463+
toDisplay.push(displayEntry);
14761464
}
1465+
14771466
setDisplayData(toDisplay);
14781467
})
14791468
.catch((error) => {
@@ -1485,17 +1474,28 @@ function popProtocol(buf) {
14851474
try {
14861475
let [code, Vlen] = readVarint(buf, 0);
14871476
buf = buf.slice(Vlen);
1488-
if (code == 0x900) {
1489-
return ["Bitswap", buf];
1490-
} else if (code == 0x910 || code == 4128768) {
1491-
return ["Graphsync", buf];
1477+
1478+
// Map of protocol codes to protocol names
1479+
const protocolMap = {
1480+
0x0900: "Bitswap", // 2304
1481+
0x0910: "Graphsync", // 2320
1482+
0x0920: "HTTP", // 2336
1483+
};
1484+
1485+
// Convert code to hexadecimal for mapping
1486+
const hexCode = code.toString(16).toLowerCase();
1487+
const protocolName = protocolMap[`0x${hexCode}`];
1488+
1489+
if (protocolName) {
1490+
return [protocolName, buf];
14921491
} else {
14931492
return [code, buf];
14941493
}
14951494
} catch (e) {
14961495
return [-1, buf];
14971496
}
14981497
}
1498+
14991499
function toContext(code, buf) {
15001500
if (code == "Graphsync") {
15011501
try {

0 commit comments

Comments
 (0)