Skip to content

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

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

/** Hides off-device features until the native policy is known, including on read errors. */
export function useLocalProcessingOnly(): boolean {
  const [restricted, setRestricted] = useState(
    () => connectionModeService.getCachedLocalProcessingOnly() ?? true,
  );
  useEffect(() => {
    let mounted = true;
    void connectionModeService
      .getCurrentConfig()
      .then((config) => {
        if (mounted) setRestricted(Boolean(config.local_processing_only));
      })
      .catch(() => {
        if (mounted) setRestricted(true);
      });
    const unsubscribe = connectionModeService.subscribeToModeChanges((config) =>
      setRestricted(Boolean(config.local_processing_only)),
    );
    return () => {
      mounted = false;
      unsubscribe();
    };
  }, []);
  return restricted;
}