Преглед на файлове

fix(ui): report errors and ignore repeat triggers when fetching content

Ignore new triggers while a request is in flight, show server, network
and parse errors in a toast, and always restore the button. Restore
only the button's child nodes so it no longer nests a copy of itself
on each fetch.
Fred преди 21 часа
родител
ревизия
5117f365dc
променени са 1 файла, в които са добавени 21 реда и са изтрити 14 реда
  1. 21 14
      internal/ui/static/js/app.js

+ 21 - 14
internal/ui/static/js/app.js

@@ -187,30 +187,28 @@ function setIconAndLabelElement(parentElement, iconName, labelText) {
 }
 }
 
 
 /**
 /**
- * Set the button to a loading state and return a clone of the original button element.
+ * Set the button to a loading state and return its original child nodes.
  *
  *
  * @param {Element} buttonElement - The button element to set to loading state.
  * @param {Element} buttonElement - The button element to set to loading state.
- * @return {Element} The original button element cloned before modification.
+ * @return {Node[]} The child nodes of the button before modification.
  */
  */
 function setButtonToLoadingState(buttonElement) {
 function setButtonToLoadingState(buttonElement) {
-    const originalButtonElement = buttonElement.cloneNode(true);
+    const originalChildNodes = Array.from(buttonElement.childNodes);
 
 
-    buttonElement.textContent = "";
-    buttonElement.appendChild(createIconLabelElement(buttonElement.dataset.labelLoading));
+    buttonElement.replaceChildren(createIconLabelElement(buttonElement.dataset.labelLoading));
 
 
-    return originalButtonElement;
+    return originalChildNodes;
 }
 }
 
 
 /**
 /**
  * Restore the button to its original state.
  * Restore the button to its original state.
  *
  *
  * @param {Element} buttonElement The button element to restore.
  * @param {Element} buttonElement The button element to restore.
- * @param {Element} originalButtonElement The original button element to restore from.
+ * @param {Node[]} originalChildNodes The child nodes returned by setButtonToLoadingState.
  * @returns {void}
  * @returns {void}
  */
  */
-function restoreButtonState(buttonElement, originalButtonElement) {
-    buttonElement.textContent = "";
-    buttonElement.appendChild(originalButtonElement);
+function restoreButtonState(buttonElement, originalChildNodes) {
+    buttonElement.replaceChildren(...originalChildNodes);
 }
 }
 
 
 /**
 /**
@@ -773,14 +771,17 @@ function handleFetchOriginalContentAction() {
     if (isListView()) return;
     if (isListView()) return;
 
 
     const buttonElement = document.querySelector(":is(a, button)[data-fetch-content-entry]");
     const buttonElement = document.querySelector(":is(a, button)[data-fetch-content-entry]");
-    if (!buttonElement) return;
+    if (!buttonElement || buttonElement.dataset.loading) return;
 
 
-    const originalButtonElement = setButtonToLoadingState(buttonElement);
+    buttonElement.dataset.loading = "true";
+    const originalChildNodes = setButtonToLoadingState(buttonElement);
 
 
     sendPOSTRequest(buttonElement.dataset.fetchContentUrl).then((response) => {
     sendPOSTRequest(buttonElement.dataset.fetchContentUrl).then((response) => {
-        restoreButtonState(buttonElement, originalButtonElement);
+        return response.json().then((data) => {
+            if (!response.ok) {
+                throw new Error(data.error_message || response.statusText);
+            }
 
 
-        response.json().then((data) => {
             if (data.content && data.reading_time) {
             if (data.content && data.reading_time) {
                 if (!articleHTMLPolicy) {
                 if (!articleHTMLPolicy) {
                     articleHTMLPolicy = trustedTypes.createPolicy('html', {createHTML: html => html});
                     articleHTMLPolicy = trustedTypes.createPolicy('html', {createHTML: html => html});
@@ -792,6 +793,12 @@ function handleFetchOriginalContentAction() {
                 }
                 }
             }
             }
         });
         });
+    }).catch((error) => {
+        console.error("Unable to fetch original content:", error);
+        showToastNotification("", error.message);
+    }).finally(() => {
+        restoreButtonState(buttonElement, originalChildNodes);
+        delete buttonElement.dataset.loading;
     });
     });
 }
 }