Skip to content

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

6.4 KB
Raw Blame
import { Suspense } from "react";
import { useTranslation } from "react-i18next";
import { Icon } from "@editor/ui/Icon";
import { Banner, Chip } from "@editor/ui";
import { SidebarProvider } from "@editor/contexts/SidebarContext";
import { type ToolRegistry } from "@editor/data/toolsTaxonomy";
import { type ErasedToolParams } from "@editor/hooks/tools/shared/toolOperationTypes";
import {
  activeFileFields,
  assetRefIds,
  extractStepFiles,
  type WorkingToolStep,
} from "@editor/hooks/tools/shared/toolAutomation";

import { PolicyExternalApiConfig } from "@processor/components/policies/PolicyExternalApiConfig";
import { isIntegrationStep } from "@processor/components/pipelines/integrationStep";
import { isIngestStep } from "@processor/components/pipelines/docparseStep";
import { IngestStepConfig } from "@processor/components/pipelines/IngestStepConfig";
import type { ExternalApiStepParams } from "@processor/components/policies/stepOperations";
import "@processor/components/pipelines/PipelineStepSettings.css";
import { PrivateContent } from "@editor/components/shared/PrivateContent";

/**
 * A params update: the next params outright, or a merge from the latest params. Settings UIs fire
 * several single-field changes synchronously (e.g. convert's source-format change also resets the
 * target and options); the merge form lets them accumulate against current state instead of each
 * rebuilding from the `step` snapshot captured at render, which would clobber the earlier fields.
 */
export type ParamsUpdate =
  | ErasedToolParams
  | ((prev: ErasedToolParams) => ErasedToolParams);

interface PipelineStepSettingsProps {
  step: WorkingToolStep;
  editorInput?: boolean;
  registry: Partial<ToolRegistry>;
  onChange: (update: ParamsUpdate) => void;
  /** Stored asset id -> file name, for labelling the supporting-file chips on a reopened pipeline. */
  assetNames: Record<string, string>;
  /** Drop a field's stored supporting-file binding (the user re-picks a file if the step still needs one). */
  onClearBinding: (field: string) => void;
}

/** One reopened supporting file shown as a chip: the field it binds and the stored file name(s). */
interface StoredFileChip {
  field: string;
  label: string;
}

/**
 * The supporting files this step is reusing from a previous save: an active binding whose field has
 * no fresh pick (a fresh pick shows in the tool's own file picker instead). Labelled by the resolved
 * asset name so the user sees "using cert.pfx" rather than an empty picker.
 */
function storedFileChips(
  step: WorkingToolStep,
  registry: Partial<ToolRegistry>,
  assetNames: Record<string, string>,
): StoredFileChip[] {
  const bindings = step.fileParameters;
  if (!bindings) return [];
  // A null active set means the tool couldn't be probed; show every stored binding rather than hide
  // the user's files (mirrors the save path, which keeps them too).
  const active = activeFileFields(step, registry);
  const activeSet = active === null ? null : new Set(active);
  const fresh = extractStepFiles(step, registry);
  return Object.entries(bindings)
    .filter(
      ([field]) =>
        (activeSet === null || activeSet.has(field)) && !fresh[field],
    )
    .map(([field, binding]) => ({
      field,
      label:
        assetRefIds(binding)
          .map((id) => assetNames[id] ?? id)
          .join(", ") || binding,
    }));
}

/**
 * Renders the parameter editor for one pipeline step, chosen by the tool's capability:
 * the tool's own settings UI when editable, an explanatory note when it has no parameters,
 * or a "not supported yet" fallback for tools not yet migrated to the mapper seam. Reopened
 * supporting files appear as removable chips above the tool's own settings.
 */
export function PipelineStepSettings({
  editorInput = false,
  step,
  registry,
  onChange,
  assetNames,
  onClearBinding,
}: PipelineStepSettingsProps) {
  // Hooks first: selecting a different step re-renders this same instance, so an early return
  // above useTranslation would change the hook count between renders and crash.
  const { t } = useTranslation();

  // Same reasoning as the integration branch below: a DocParse step has no registry entry, so
  // its settings come from its own component rather than a tool's.
  if (isIngestStep(step)) {
    return (
      <IngestStepConfig
        editorInput={editorInput}
        parameters={step.params}
        onChange={(params) => onChange(params as never)}
      />
    );
  }

  // An integration step is configured by the operations catalogue, not by a tool's settings UI:
  // it has no registry entry to look one up from.
  if (isIntegrationStep(step)) {
    return (
      <PolicyExternalApiConfig
        parameters={step.params as unknown as ExternalApiStepParams}
        onChange={(params) => onChange(params as never)}
      />
    );
  }

  const chips = storedFileChips(step, registry, assetNames);

  function toolBody() {
    if (step.support === "noSettings") {
      return (
        <Banner
          tone="info"
          description={t("portal.pipelines.composer.noToolSettings")}
        />
      );
    }
    const entry = step.toolId ? registry[step.toolId] : undefined;
    const Settings =
      step.support === "editable" ? entry?.automationSettings : null;
    if (!Settings) {
      return (
        <Banner
          tone="warning"
          description={t("portal.pipelines.composer.editingUnsupported")}
        />
      );
    }
    return (
      <SidebarProvider>
        <Suspense fallback={null}>
          <Settings
            parameters={step.params}
            onParameterChange={(key, value) =>
              onChange((prev) => ({ ...prev, [key]: value }))
            }
            disabled={false}
          />
        </Suspense>
      </SidebarProvider>
    );
  }

  return (
    <>
      {chips.length > 0 && (
        <div className="portal-step-settings__files">
          <span className="portal-step-settings__files-label">
            {t("portal.pipelines.builder.supportingFiles")}
          </span>
          <div className="portal-step-settings__files-chips">
            {chips.map((chip) => (
              <Chip
                key={chip.field}
                leadingIcon={<Icon name="file" size={"0.875rem"} />}
                onRemove={() => onClearBinding(chip.field)}
              >
                <PrivateContent>{chip.label}</PrivateContent>
              </Chip>
            ))}
          </div>
        </div>
      )}
      {toolBody()}
    </>
  );
}