Skip to content

Commit 51d5c1c

Browse files
committed
refactor: streamline error handling, hide td desc when error, simlified code
1 parent b08b2a5 commit 51d5c1c

1 file changed

Lines changed: 12 additions & 32 deletions

File tree

examples/browser/index.js

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@
1313
**/
1414

1515
function get_td(addr) {
16-
hideError();
17-
18-
// Clear all loaded content before loading new TD
19-
const interactions = document.getElementById("interactions");
20-
if (interactions) {
21-
interactions.style.display = "none";
22-
}
16+
clearAllInteractions();
2317

2418
servient
2519
.start()
@@ -30,25 +24,18 @@ function get_td(addr) {
3024
thingFactory
3125
.consume(td)
3226
.then((thing) => {
33-
removeInteractions();
3427
showInteractions(thing);
3528
updateTabDescription(addr, td);
3629
})
3730
.catch((err) => {
38-
showError("Failed to consume TD: " + err);
39-
clearAllInteractions();
4031
updateTabDescription(addr, null, "Failed to consume TD: " + err);
4132
});
4233
})
4334
.catch((err) => {
44-
showError("Failed to fetch TD: " + err);
45-
clearAllInteractions();
4635
updateTabDescription(addr, null, "Failed to fetch TD: " + err);
4736
});
4837
})
4938
.catch((err) => {
50-
showError("Failed to start servient: " + err);
51-
clearAllInteractions();
5239
updateTabDescription(addr, null, "Failed to start servient: " + err);
5340
});
5441
}
@@ -135,15 +122,6 @@ function showInteractions(thing) {
135122
}
136123
}
137124

138-
function removeInteractions() {
139-
for (id of ["properties", "actions", "events"]) {
140-
let placeholder = document.getElementById(id);
141-
while (placeholder.firstChild) {
142-
placeholder.removeChild(placeholder.firstChild);
143-
}
144-
}
145-
}
146-
147125
function showSchemaEditor(action, thing) {
148126
let td = thing.getThingDescription();
149127
// Remove old editor
@@ -220,23 +198,25 @@ function updateTabDescription(url, td, error) {
220198

221199
// Update description
222200
const descriptionElement = activeTab.querySelector(".td-description");
223-
if (!descriptionElement) return;
201+
const descriptionParent = descriptionElement ? descriptionElement.closest("p") : null;
224202

225203
if (error) {
226-
descriptionElement.textContent = error;
227-
descriptionElement.style.color = "red";
228-
} else if (td && td.description) {
229-
descriptionElement.textContent = td.description;
230-
descriptionElement.style.color = "";
204+
// Hide description, show error banner
205+
if (descriptionParent) descriptionParent.style.display = "none";
206+
showError(error);
231207
} else {
232-
descriptionElement.textContent = "No description available";
233-
descriptionElement.style.color = "";
208+
// Show description, hide error banner
209+
if (descriptionParent) descriptionParent.style.display = "";
210+
if (descriptionElement) {
211+
descriptionElement.textContent = td?.description || "No description available";
212+
descriptionElement.style.color = "";
213+
}
214+
hideError();
234215
}
235216
}
236217

237218
// Clear all interactions and editor
238219
function clearAllInteractions() {
239-
console.log("clearAllInteractions function called");
240220
hideError();
241221
const interactions = document.getElementById("interactions");
242222
if (interactions) {

0 commit comments

Comments
 (0)