Skip to content

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

2.0 KB
Raw Blame
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useLocalProcessingOnly } from "@editor/hooks/useLocalProcessingOnly";
import { connectionModeService } from "@editor/services/connectionModeService";
import type { ConnectionMode } from "@editor/services/connectionModeService";
import { useServerFolderBlock as useCoreServerFolderBlock } from "@editor-core/hooks/useServerFolderBlock";

/**
 * Desktop's blocker speaks in connection modes: local mode has no server at all, so
 * "storage isn't enabled" would send the user after a setting that does not exist.
 */
export function useServerFolderBlock(): string | null {
  const { t } = useTranslation();
  const coreReason = useCoreServerFolderBlock();
  const localOnly = useLocalProcessingOnly();
  // Seeded from the cache so a remount answers on its first frame; the effect covers
  // the first-ever load and later mode switches.
  const [mode, setMode] = useState<ConnectionMode | null>(() =>
    connectionModeService.getCachedMode(),
  );
  useEffect(() => {
    let mounted = true;
    void connectionModeService.getCurrentMode().then((current) => {
      if (mounted) setMode(current);
    });
    const unsubscribe = connectionModeService.subscribeToModeChanges((config) =>
      setMode(config.mode),
    );
    return () => {
      mounted = false;
      unsubscribe();
    };
  }, []);
  // While the mode is unknown (first-ever load), fail closed with the same message: in
  // any mode where the item would be blocked, signing in or connecting a server IS the
  // way out — unlike the core reasons, which in local mode point at a storage setting
  // that doesn't exist.
  if (localOnly)
    return t(
      "desktopPrivacy.blocked",
      "Your administrator requires documents to stay on this device. This feature is unavailable.",
    );
  if (mode === "local" || mode === null) {
    return t(
      "filesPage.serverFolderNeedsConnection",
      "Sign in to Stirling Cloud or connect a self-hosted server to use server folders.",
    );
  }
  return coreReason;
}