Skip to content

Commit

Permalink
Handle cases where the toggle status notification keeps popping up. O…
Browse files Browse the repository at this point in the history
…nly allow toast notifications within a second after a command
  • Loading branch information
david-tejada committed Apr 25, 2023
1 parent f68dffd commit 6ec4475
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/background/commands/dispatchCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const backgroundCommands = new Set([
export async function dispatchCommand(
command: RangoAction
): Promise<ResponseToTalon> {
await sendRequestToContent({ type: "allowToastNotification" });
const result = (await (backgroundCommands.has(command.type)
? runBackgroundCommand(command)
: sendRequestToContent(command))) as string | TalonAction[] | undefined;
Expand Down
10 changes: 9 additions & 1 deletion src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { runRangoActionWithTarget } from "./actions/runRangoActionWithTarget";
import { runRangoActionWithoutTarget } from "./actions/runRangoActionWithoutTarget";
import { reclaimHints } from "./wrappers/wrappers";
import { reclaimHintsFromCache } from "./hints/hintsCache";
import { notify, notifyTogglesStatus } from "./notify/notify";
import {
allowToastNotification,
notify,
notifyTogglesStatus,
} from "./notify/notify";
import { initContentScript } from "./setup/initContentScript";
import { setNavigationToggle } from "./settings/toggles";
import { updateHintsEnabled } from "./observe";
Expand Down Expand Up @@ -79,6 +83,10 @@ browser.runtime.onMessage.addListener(
await notifyTogglesStatus();
break;

case "allowToastNotification":
allowToastNotification();
break;

default: {
const result = await runRangoActionWithoutTarget(request);
return result;
Expand Down
15 changes: 15 additions & 0 deletions src/content/notify/notify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ import { ToastMessage } from "./ToastMessage";
import { TogglesStatusMessage } from "./ToastTogglesMessage";
import { ToastIcon } from "./ToastIcon";

let notificationAllowed = false;

// This is to avoid the notification showing up if the user hasn't issued any
// command. For example, we reset hintsToggleTabs every time the extension
// starts
export function allowToastNotification() {
console.log("Toast notifications allowed");
notificationAllowed = true;

setTimeout(() => {
notificationAllowed = false;
}, 1000);
}

function renderToast() {
let toastContainer = document.querySelector("#rango-toast");

Expand All @@ -21,6 +35,7 @@ function renderToast() {

async function shouldNotify() {
if (
!notificationAllowed ||
document.visibilityState !== "visible" ||
!getCachedSetting("enableNotifications") ||
!isMainframe() ||
Expand Down
5 changes: 5 additions & 0 deletions src/typings/RequestFromBackground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ interface DisplayToastNotification {
options?: ToastOptions;
}

interface AllowToastNotification {
type: "allowToastNotification";
}

interface UpdateNavigationToggle {
type: "updateNavigationToggle";
enable: boolean | undefined;
Expand All @@ -38,4 +42,5 @@ export type RequestFromBackground = { frameId?: number } & (
| ReclaimHints
| DisplayToastNotification
| UpdateNavigationToggle
| AllowToastNotification
);

0 comments on commit 6ec4475

Please sign in to comment.