Jelajahi Sumber

fix: toast duration being 1000x too high

Sv443 8 bulan lalu
induk
melakukan
493d46a9b2
4 mengubah file dengan 11 tambahan dan 11 penghapusan
  1. 9 7
      src/components/toast.ts
  2. 0 1
      src/features/index.ts
  3. 1 1
      src/index.ts
  4. 1 2
      src/utils/logging.ts

+ 9 - 7
src/components/toast.ts

@@ -7,7 +7,7 @@ import "./toast.css";
 type ToastPos = "tl" | "tr" | "bl" | "br";
 
 type ToastProps = {
-  /** Duration in seconds */
+  /** Duration in milliseconds */
   duration?: number;
   position?: ToastPos;
 } & (
@@ -40,10 +40,12 @@ let timeout: ReturnType<typeof setTimeout> | undefined;
 
 /** Shows a toast message with an icon */
 export async function showIconToast({
-  duration = getFeature("toastDuration"),
+  duration,
   position = "tr",
   ...rest
 }: IconToastProps) {
+  if(typeof duration !== "number" || isNaN(duration))
+    duration = getFeature("toastDuration") * 1000;
   if(duration <= 0)
     return info("Toast duration is <= 0, so it won't be shown");
   const toastWrapper = document.createElement("div");
@@ -95,12 +97,12 @@ export async function showToast(arg: string | ToastProps): Promise<HTMLDivElemen
     : arg;
 
   const {
-    duration = getFeature("toastDuration"),
+    duration: durationMs = getFeature("toastDuration") * 1000,
     position = "tr",
     ...rest
   } = props;
 
-  if(duration <= 0)
+  if(durationMs <= 0)
     return info("Toast duration is <= 0, so it won't be shown");
   const toastEl = document.querySelector("#bytm-toast");
   if(toastEl)
@@ -122,12 +124,12 @@ export async function showToast(arg: string | ToastProps): Promise<HTMLDivElemen
   }
 
   document.body.appendChild(toastElem);
-
+  
   pauseFor(100).then(async () => {
     toastElem.classList.add("visible", `pos-${position.toLowerCase()}`);
 
-    if(duration < Number.POSITIVE_INFINITY)
-      timeout = setTimeout(async () => await closeToast(), duration * 1000);
+    if(durationMs < Number.POSITIVE_INFINITY)
+      timeout = setTimeout(async () => await closeToast(), durationMs);
   });
 
   return toastElem;

+ 0 - 1
src/features/index.ts

@@ -700,7 +700,6 @@ export const featInfo = {
     textAdornment: adornments.advanced,
     enable: noop,
     change: () => showIconToast({
-      duration: getFeature("toastDuration") * 1000,
       message: "Example",
       iconSrc: getResourceUrl(`img-logo${mode === "development" ? "_dev" : ""}`),
     }),

+ 1 - 1
src/index.ts

@@ -447,7 +447,7 @@ function registerDevCommands() {
     }
   });
 
-  GM.registerMenuCommand("Throw Error", () => error("Test error thrown by user command:", new Error("Test error")));
+  GM.registerMenuCommand("Throw Error", () => error("Test error thrown by user command:", new SyntaxError("Test error")));
 
   log("Registered dev menu commands");
 }

+ 1 - 2
src/utils/logging.ts

@@ -61,10 +61,9 @@ export function error(...args: unknown[]): void {
 
   getFeature("showToastOnGenericError")
     && showIconToast({
+      message: t("generic_error_toast", args.find(e => e instanceof Error)?.name ?? t("error")),
       icon: "icon-error",
       iconFill: "var(--bytm-error-col)",
-      message: t("generic_error_toast", args.find(e => e instanceof Error)?.name ?? t("error")),
-      duration: Math.max(getFeature("toastDuration"), 6) * 1000,
     });
 }