Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

3.4 KB
Raw Blame
import { useEffect, useState } from "react";
import { GROK_MOVE_POPUP } from "./GrokMovePopup";
import { NEW_LOOK_NOTICE } from "./NewLookNotice";
import { SIMPLE_MODE_PROMPT } from "./SimpleModePrompt";
import { UPDATES_CONSENT_POPUP } from "./UpdatesConsent";
import { TEAMS_POPUP } from "./whats-new/TeamsPopup";
import { WHATS_NEW_POPUP } from "./whats-new/WhatsNewPopup";

export { useDesktopNotices } from "./desktop-notices";

const OTHER_DIALOGS = '[role="dialog"], [role="alertdialog"], [aria-modal="true"], [data-tour-card], [data-cloud-setup-ready]';
const QUEUE_POPUPS = [TEAMS_POPUP, GROK_MOVE_POPUP, WHATS_NEW_POPUP, UPDATES_CONSENT_POPUP, SIMPLE_MODE_PROMPT, NEW_LOOK_NOTICE].join(", ");
/** A first run's own screens on a Cloud: its welcome, its first ask and a team's welcome to a new member. */
const FIRST_SCREENS = "[data-cloud-welcome], [data-cloud-intent], [data-team-welcome]";
const OBSERVED_ATTRIBUTES = [
  "role", "aria-modal", "hidden", "style", "class", "open",
  "data-tour-card", "data-cloud-setup-ready", "data-cloud-welcome", "data-cloud-intent", "data-team-welcome",
];

/** Any dialog on screen counts, modal or not, and the card that says a new Cloud is set up; never the queue's own popups.
 * `firstScreens`: a first run's own screens count too. */
export function otherDialogOpen(firstScreens = false): boolean {
  const selector = firstScreens ? `${OTHER_DIALOGS}, ${FIRST_SCREENS}` : OTHER_DIALOGS;
  return [...document.querySelectorAll<HTMLElement>(selector)].some(element => !element.closest(QUEUE_POPUPS) && element.checkVisibility());
}

const listeners = new Set<() => void>();
let observer: MutationObserver | null = null;
let scheduled = false;

function notifyDialogListeners(): void {
  if (scheduled) return;
  scheduled = true;
  queueMicrotask(() => {
    scheduled = false;
    for (const listener of listeners) listener();
  });
}

function handleVisibilityChange(): void {
  if (document.visibilityState === "visible") notifyDialogListeners();
}

function subscribeDialogChanges(listener: () => void): () => void {
  listeners.add(listener);
  if (!observer && typeof MutationObserver !== "undefined" && document.body) {
    observer = new MutationObserver(notifyDialogListeners);
    observer.observe(document.body, {
      childList: true,
      subtree: true,
      attributes: true,
      attributeFilter: OBSERVED_ATTRIBUTES,
    });
    document.addEventListener("visibilitychange", handleVisibilityChange);
  }
  return () => {
    listeners.delete(listener);
    if (listeners.size === 0 && observer) {
      observer.disconnect();
      observer = null;
      document.removeEventListener("visibilitychange", handleVisibilityChange);
    }
  };
}

export function useOtherDialogOpen(watch: boolean, firstScreens = false): boolean {
  const [open, setOpen] = useState(() => (watch ? otherDialogOpen(firstScreens) : false));
  const [tracked, setTracked] = useState({ watch, firstScreens });
  if (tracked.watch !== watch || tracked.firstScreens !== firstScreens) {
    setTracked({ watch, firstScreens });
    setOpen(watch ? otherDialogOpen(firstScreens) : false);
  }
  useEffect(() => {
    if (!watch) return;
    const sync = () => setOpen((current) => {
      const next = otherDialogOpen(firstScreens);
      return current === next ? current : next;
    });
    sync();
    return subscribeDialogChanges(sync);
  }, [watch, firstScreens]);
  return watch && open;
}