Browse Source

refactor: make showConfirmation always resolve

no input: <boolean> i.e. true on Ok, false otherwise
input: <?string> i.e. string on Ok, null otherwise
tophf 3 years ago
parent
commit
2ff204d006

+ 2 - 3
src/common/router.js

@@ -51,12 +51,11 @@ export function setRoute(hash, replace, noConfirm) {
 
 export function getUnloadSentry(onConfirm, onCancel) {
   async function confirmPopState(hash) {
-    try {
+    if (await showConfirmation(i18n('confirmNotSaved'))) {
       // popstate cannot be prevented so we pin current `route` and display a confirmation
-      await showConfirmation(i18n('confirmNotSaved'));
       setRoute(hash, false, true);
       onConfirm?.();
-    } catch {
+    } else {
       onCancel?.();
     }
   }

+ 8 - 6
src/common/ui/index.js

@@ -37,19 +37,21 @@ export function showMessage(message) {
  * @param {string | false} [cfg.input=false] if not false, shows a text input with this string
  * @param {?Object|false} [cfg.ok] additional props for the Ok button or `false` to remove it
  * @param {?Object|false} [cfg.cancel] same for the Cancel button
- * @return {Promise<?string>} resolves on Ok to `false` or the entered string, rejects otherwise
+ * @return {Promise<?string|true>} resolves on Ok to `true` or the entered string, null otherwise
  */
 export function showConfirmation(text, { ok, cancel, input = false } = {}) {
-  return new Promise((resolve, reject) => {
+  return new Promise(resolve => {
+    const onCancel = () => resolve(null);
+    const onOk = val => resolve(input === false || val);
     showMessage({
       input,
       text,
       buttons: [
-        ok !== false && { text: i18n('buttonOK'), onClick: resolve, ...ok },
-        cancel !== false && { text: i18n('buttonCancel'), onClick: reject, ...cancel },
+        ok !== false && { text: i18n('buttonOK'), onClick: onOk, ...ok },
+        cancel !== false && { text: i18n('buttonCancel'), onClick: onCancel, ...cancel },
       ].filter(Boolean),
-      onBackdropClick: reject,
-      onDismiss: reject, // Esc key
+      onBackdropClick: onCancel,
+      onDismiss: onCancel, // Esc key
     });
   });
 }

+ 1 - 4
src/options/views/tab-installed.vue

@@ -491,12 +491,9 @@ export default {
       });
     },
     async emptyRecycleBin() {
-      try {
-        await showConfirmation(i18n('buttonEmptyRecycleBin'));
+      if (await showConfirmation(i18n('buttonEmptyRecycleBin'))) {
         sendCmdDirectly('CheckRemove', { force: true });
         store.scripts = store.scripts.filter(script => !script.config.removed);
-      } catch (e) {
-        // NOP
       }
     },
     adjustScriptWidth() {

+ 6 - 8
src/options/views/tab-settings/vm-import.vue

@@ -210,14 +210,12 @@ function initDragDrop(targetElement) {
   const onDrop = async evt => {
     evt.preventDefault();
     showAllowedState(false);
-    try {
-      // storing it now because `files` will be null after await
-      const file = evt.dataTransfer.files[0];
-      await showConfirmation(i18n('buttonImportData'));
-      targetElement.disabled = true;
-      await importBackup(file);
-      targetElement.disabled = false;
-    } catch (e) { /* NOP */ }
+    // storing it now because `files` will be null after await
+    const file = evt.dataTransfer.files[0];
+    if (!await showConfirmation(i18n('buttonImportData'))) return;
+    targetElement.disabled = true;
+    await importBackup(file);
+    targetElement.disabled = false;
   };
   return () => {
     const isSettingsTab = window.location.hash === '#settings';