Skip to content

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

2.5 KB
Raw Blame
import { memo, useState, useEffect } from "react";
import CoreToolButton from "@editor-core/components/tools/toolPicker/ToolButton";
import { getToolDisabledReason } from "@editor/components/tools/fullscreen/shared";
import {
  useToolWorkflowActions,
  useToolWorkflowData,
} from "@editor/contexts/ToolWorkflowContext";
import { useAppConfig } from "@editor/contexts/AppConfigContext";
import { useLocalProcessingOnly } from "@editor/hooks/useLocalProcessingOnly";
import {
  connectionModeService,
  type ConnectionMode,
} from "@editor/services/connectionModeService";

type CoreToolButtonProps = React.ComponentProps<typeof CoreToolButton>;

/**
 * Desktop override of ToolButton.
 * In local mode, unavailable tools (except comingSoon/selfHostedOffline) navigate directly
 * to the tool UI — the execute button there shows the disabled state with a "click to sign in"
 * tooltip, keeping the tool's settings visible and letting the user explore before committing.
 * In selfhosted/saas mode the tool renders as visually unavailable (dimmed, no badge).
 */
const ToolButton: React.FC<CoreToolButtonProps> = (props) => {
  const { toolAvailability } = useToolWorkflowData();
  const { handleToolSelectForced } = useToolWorkflowActions();
  const { config } = useAppConfig();
  const localOnly = useLocalProcessingOnly();
  const premiumEnabled = config?.premiumEnabled;
  const [connectionMode, setConnectionMode] = useState<ConnectionMode | null>(
    null,
  );

  useEffect(() => {
    void connectionModeService.getCurrentMode().then(setConnectionMode);
    return connectionModeService.subscribeToModeChanges((cfg) =>
      setConnectionMode(cfg.mode),
    );
  }, []);

  const disabledReason = getToolDisabledReason(
    props.id,
    props.tool,
    toolAvailability,
    premiumEnabled,
  );

  // In local mode, pass a handler so CoreToolButton renders the tool as "cloud-available"
  // (full opacity, cloud badge, clickable). Clicking navigates to the tool normally so the
  // user can see the settings; the disabled execute button handles the sign-in prompt.
  // comingSoon and selfHostedOffline tools remain dimmed — they have no usable UI to show.
  const handleUnavailableClick =
    !localOnly &&
    connectionMode === "local" &&
    disabledReason !== "comingSoon" &&
    disabledReason !== "selfHostedOffline"
      ? () => handleToolSelectForced(props.id)
      : undefined;

  if (localOnly && props.tool.hiddenFromToolList) return null;
  return (
    <CoreToolButton {...props} onUnavailableClick={handleUnavailableClick} />
  );
};

export default memo(ToolButton);