Skip to content

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

5.9 KB
Raw Blame
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { Icon } from "@editor/ui/Icon";
import { usePortalAccess } from "@editor/hooks/usePortalAccess";
import { useNavigationActions } from "@editor/contexts/NavigationContext";
import { PORTAL_BASENAME } from "@editor/routes/portalBasename";
import { HAS_PORTAL } from "@editor/routes/hasPortal";
import { saveEditorReturnPath } from "@editor/services/workbenchSession";
import { policyCategoryIcon } from "@editor/components/policies/policyCategoryIcon";
import {
  useEditorPipelines,
  type EditorPipeline,
  type EditorPipelines,
} from "@editor/components/policies/useEditorPipelines";
import { ActionIcon } from "@editor/ui/ActionIcon";
import "@editor/components/policies/EditorPipelinesPanel.css";

const PIPELINES_PATH = `${PORTAL_BASENAME}/pipelines`;
const ICON_SIZE = "1.05rem";
const TRIGGER_SIZE = "0.8125rem";

function rowState(pipeline: EditorPipeline): "running" | "failed" | "idle" {
  if (pipeline.running) return "running";
  return pipeline.failed ? "failed" : "idle";
}

function PipelineRow({
  pipeline,
  onOpen,
}: {
  pipeline: EditorPipeline;
  onOpen: (() => void) | null;
}) {
  const { t } = useTranslation();
  const state = rowState(pipeline);
  const label =
    state === "running"
      ? t("policies.editorPanel.rowRunning", "{{label}} is running now", {
          label: pipeline.label,
        })
      : state === "failed"
        ? t("policies.editorPanel.rowFailed", "{{label}} - last run failed", {
            label: pipeline.label,
          })
        : t("policies.editorPanel.rowIdle", "{{label}} runs automatically", {
            label: pipeline.label,
          });
  const trigger =
    pipeline.runOn === "export"
      ? t("policies.editorPanel.triggerExport", "Runs on export")
      : t("policies.editorPanel.triggerImport", "Runs on import");
  const triggerIcon = pipeline.runOn === "export" ? "log-out" : "log-in";

  const body = (
    <>
      <span className="editor-pipelines__row-icon" aria-hidden>
        {policyCategoryIcon(pipeline.policyKey, ICON_SIZE)}
      </span>
      <span className="editor-pipelines__row-label">{pipeline.label}</span>
      {onOpen && (
        <Icon
          name={triggerIcon}
          className="editor-pipelines__row-trigger"
          size={TRIGGER_SIZE}
          title={trigger}
        />
      )}
      {pipeline.runsToday > 0 && (
        <span
          className="editor-pipelines__row-count"
          title={t("policies.editorPanel.runsToday", "{{count}} runs today", {
            count: pipeline.runsToday,
          })}
        >
          {pipeline.runsToday}
        </span>
      )}
      <span className="editor-pipelines__dot" data-state={state} aria-hidden />
    </>
  );

  return (
    <li>
      {onOpen ? (
        <button
          type="button"
          className="editor-pipelines__row"
          onClick={onOpen}
          title={label}
        >
          {body}
        </button>
      ) : (
        <div className="editor-pipelines__row" title={label}>
          {body}
        </div>
      )}
    </li>
  );
}

export interface EditorPipelinesPanelViewProps extends EditorPipelines {
  onOpenProcessor: (() => void) | null;
}

export function EditorPipelinesPanelView({
  onImport,
  onExport,
  total,
  onOpenProcessor,
}: EditorPipelinesPanelViewProps) {
  const { t } = useTranslation();
  const [open, setOpen] = useState(false);

  return (
    <section className="editor-pipelines">
      <div className="editor-pipelines__head">
        <button
          type="button"
          className="editor-pipelines__toggle"
          onClick={() => setOpen((v) => !v)}
          aria-expanded={open}
          disabled={total === 0}
          title={
            open
              ? t("policies.editorPanel.collapse", "Hide pipelines")
              : t("policies.editorPanel.expand", "Show pipelines")
          }
        >
          <span className="editor-pipelines__title">
            {t("policies.editorPanel.title", "PDF Processor")}
          </span>
          {/* The span carries data-open and the class: <Icon> forwards no data-*
              attributes, and the css rotates on that hook. */}
          {total > 0 && (
            <span className="editor-pipelines__chevron" data-open={open}>
              <Icon name="chevron-down" size={"1.1rem"} />
            </span>
          )}
        </button>
        {onOpenProcessor && (
          <ActionIcon
            variant="tertiary"
            size="md"
            shape="circle"
            onClick={onOpenProcessor}
            aria-label={t(
              "policies.editorPanel.openProcessor",
              "Open the PDF Processor",
            )}
          >
            <Icon name="external-link" size={"1rem"} />
          </ActionIcon>
        )}
      </div>

      {total === 0 ? (
        <p className="editor-pipelines__empty">
          {t(
            "policies.editorPanel.empty",
            "Nothing runs on import or export yet",
          )}
        </p>
      ) : (
        open && (
          <ul className="editor-pipelines__list">
            {[...onImport, ...onExport].map((pipeline) => (
              <PipelineRow
                key={pipeline.policyKey}
                pipeline={pipeline}
                onOpen={onOpenProcessor}
              />
            ))}
          </ul>
        )
      )}
    </section>
  );
}

export function EditorPipelinesPanel() {
  const pipelines = useEditorPipelines();
  const portalAccess = usePortalAccess();
  const navigate = useNavigate();
  const { actions } = useNavigationActions();

  // Builds without the processor have no /processor route to land on.
  const openProcessor =
    HAS_PORTAL && portalAccess
      ? () =>
          actions.requestNavigation(() => {
            saveEditorReturnPath();
            navigate(PIPELINES_PATH);
          })
      : null;

  return (
    <EditorPipelinesPanelView {...pipelines} onOpenProcessor={openProcessor} />
  );
}