#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
import { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { Icon } from "@editor/ui/Icon";
import { Button, Input } from "@editor/ui";
import {
getSubcategoryLabel,
SUBCATEGORY_ORDER,
type SubcategoryId,
} from "@editor/data/toolsTaxonomy";
import { type ExecutableTool } from "@editor/hooks/tools/shared/toolAutomation";
import { toolAcceptsFormat } from "@editor/utils/toolIOCompat";
import { getToolFormatLabel } from "@editor/utils/toolIOLabels";
import { type ToolFormat } from "@editor/types/toolIO";
import {
searchOperations,
type StepOperation,
} from "@processor/components/policies/stepOperations";
import { BrandMark } from "@processor/components/BrandMarks";
interface ToolPickerProps {
tools: ExecutableTool[];
onPick: (tool: ExecutableTool) => void;
onClose: () => void;
/**
* Catalogue operations that hand the document to an outside system. Kept apart from the tool
* groups because they are a different species - a tool transforms the document in place, these
* call somebody else - and grouping them under a tool subcategory would bury that.
*/
operations?: StepOperation[];
onPickOperation?: (operation: StepOperation) => void;
/**
* What the step before this one produces, when known. Tools that cannot run on it are marked
* rather than hidden: the chain is still editable in any order, and the builder explains the
* problem once the step is added.
*/
precedingOutput?: ToolFormat;
/** Keeps an unavailable tool visible and explains why it cannot be added. */
unavailableReason?: (tool: ExecutableTool) => string | undefined;
}
/**
* Type-to-filter, category-grouped tool picker for adding a step to a pipeline. Replaces the flat
* wall of tool pills so the list stays usable as the tool count grows.
*/
export function ToolPicker({
tools,
onPick,
onClose,
operations = [],
onPickOperation,
precedingOutput,
unavailableReason,
}: ToolPickerProps) {
const { t } = useTranslation();
const [query, setQuery] = useState("");
// A format-routed tool (convert) can follow the previous step if ANY endpoint in its routing set
// accepts that output; a single-endpoint tool is judged on its one endpoint. Unknown when there
// is no preceding output yet.
const acceptsPreceding = (tool: ExecutableTool): boolean => {
if (!precedingOutput) return true;
const endpoints =
tool.endpoints && tool.endpoints.length > 0
? tool.endpoints
: [tool.endpoint];
return endpoints.some((endpoint) =>
toolAcceptsFormat(endpoint, precedingOutput),
);
};
const matchedOperations = useMemo(
() =>
onPickOperation
? searchOperations(operations, query, (key) => t(key))
: [],
[operations, onPickOperation, query, t],
);
const groups = useMemo(() => {
const q = query.trim().toLowerCase();
const matched = q
? tools.filter((tool) => tool.name.toLowerCase().includes(q))
: tools;
const byCategory = new Map<SubcategoryId, ExecutableTool[]>();
for (const tool of matched) {
const list = byCategory.get(tool.subcategoryId) ?? [];
list.push(tool);
byCategory.set(tool.subcategoryId, list);
}
return SUBCATEGORY_ORDER.filter((id) => byCategory.has(id)).map((id) => ({
id,
label: getSubcategoryLabel(t, id),
tools: byCategory.get(id) ?? [],
}));
}, [tools, query, t]);
return (
<div className="portal-pipelines__picker">
<div className="portal-pipelines__picker-search">
<Input
autoFocus
inputSize="sm"
value={query}
aria-label={t("portal.pipelines.builder.searchTools")}
placeholder={t("portal.pipelines.builder.searchTools")}
leadingIcon={<Icon name="search" size={"1.125rem"} />}
onChange={(e) => setQuery(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Escape") onClose();
}}
/>
</div>
<div className="portal-pipelines__picker-list">
{groups.length === 0 && matchedOperations.length === 0 ? (
<p className="portal-pipelines__picker-empty">
{t("portal.pipelines.builder.noToolMatches")}
</p>
) : (
groups.map((group) => (
<div key={group.id} className="portal-pipelines__picker-group">
<div className="portal-pipelines__picker-group-label">
{group.label}
</div>
{group.tools.map((tool) => {
const incompatible = Boolean(
precedingOutput && !acceptsPreceding(tool),
);
const unavailable = unavailableReason?.(tool);
return (
<Button
key={tool.toolId}
variant="quiet"
justify="start"
fullWidth
className={[
"portal-pipelines__picker-item",
incompatible ? "is-incompatible" : "",
]
.filter(Boolean)
.join(" ")}
disabled={Boolean(unavailable)}
onClick={() => onPick(tool)}
leftSection={
<span
className="portal-pipelines__picker-icon"
aria-hidden="true"
>
{tool.icon}
</span>
}
>
<span className="portal-pipelines__picker-text">
<span className="portal-pipelines__picker-name">
{tool.name}
</span>
{incompatible && precedingOutput && (
<span className="portal-pipelines__picker-note">
{t("portal.pipelines.builder.cannotFollow", {
produced: getToolFormatLabel(t, precedingOutput),
})}
</span>
)}
{unavailable && (
<span className="portal-pipelines__picker-note">
{unavailable}
</span>
)}
</span>
</Button>
);
})}
</div>
))
)}
{matchedOperations.length > 0 && onPickOperation ? (
<div className="portal-pipelines__picker-group">
<div className="portal-pipelines__picker-group-label">
{t("portal.pipelines.builder.sendToSystem")}
</div>
{matchedOperations.map((op) => (
<Button
key={op.id}
variant="quiet"
justify="start"
fullWidth
className="portal-pipelines__picker-item"
onClick={() => onPickOperation(op)}
leftSection={
<span
className="portal-pipelines__picker-icon"
aria-hidden="true"
>
<BrandMark
id={op.custom ? "api" : op.connectionTypeId}
size={18}
/>
</span>
}
>
<span className="portal-pipelines__picker-name">
{t(op.labelKey)}
</span>
</Button>
))}
</div>
) : null}
</div>
</div>
);
}