Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

1.4 KB
Raw Blame
import { useEffect, useState } from "react";
import { connectionModeService } from "@editor/services/connectionModeService";

/**
 * Like {@link useSaaSMode}, but starts pessimistically FALSE: it returns true
 * only once the connection mode has been CONFIRMED to be "saas".
 *
 * Use this to gate mounting components that fire a network call on mount against
 * Stirling Cloud specifically - the cloud team context. useSaaSMode()
 * starts optimistically true (right for tool-availability UX, where a flash of
 * "unavailable" is worse than a flash of "available"), but for a network-
 * triggering gate that optimism leaks a request: the gated surface mounts on
 * cold start, fires its mount fetch against the local/self-hosted backend, and
 * only then unmounts when the real mode resolves. Starting false closes that
 * window — nothing mounts (and nothing fetches) until we KNOW we're in SaaS.
 */
export function useConfirmedSaaSMode(): boolean {
  const [isSaaSMode, setIsSaaSMode] = useState(false);

  useEffect(() => {
    let current = true;
    void connectionModeService.getCurrentMode().then((mode) => {
      if (current) setIsSaaSMode(mode === "saas");
    });
    const unsubscribe = connectionModeService.subscribeToModeChanges((cfg) => {
      current = false;
      setIsSaaSMode(cfg.mode === "saas");
    });
    return () => {
      current = false;
      unsubscribe();
    };
  }, []);

  return isSaaSMode;
}