Refactor to fix circular imports (#4700)

# Description of Changes
Refactors code to avoid circular imports everywhere and adds linting for
circular imports to ensure it doesn't happen again. Most changes are
around the tool registry, making it a provider, and splitting into tool
types to make it easier for things like Automate to only have access to
tools excluding itself.
This commit is contained in:
James Brunton
2025-10-21 14:53:18 +01:00
committed by GitHub
parent 3e23dc59b6
commit c9eee00d66
35 changed files with 2272 additions and 352 deletions
+5 -2
View File
@@ -3,7 +3,7 @@ import React from 'react';
import { ToolOperationConfig } from '../hooks/tools/shared/useToolOperation';
import { BaseToolProps } from '../types/tool';
import { WorkbenchType } from '../types/workbench';
import { ToolId } from '../types/toolId';
import { LinkToolId, RegularToolId, SuperToolId, ToolId, ToolKind } from '../types/toolId';
import DrawRoundedIcon from '@mui/icons-material/DrawRounded';
import SecurityRoundedIcon from '@mui/icons-material/SecurityRounded';
import VerifiedUserRoundedIcon from '@mui/icons-material/VerifiedUserRounded';
@@ -47,7 +47,7 @@ export type ToolRegistryEntry = {
supportedFormats?: string[];
endpoints?: string[];
link?: string;
type?: string;
kind?: ToolKind;
// Workbench type for navigation
workbench?: WorkbenchType;
// Operation configuration for automation
@@ -60,6 +60,9 @@ export type ToolRegistryEntry = {
synonyms?: string[];
}
export type RegularToolRegistry = Record<RegularToolId, ToolRegistryEntry>;
export type SuperToolRegistry = Record<SuperToolId, ToolRegistryEntry>;
export type LinkToolRegistry = Record<LinkToolId, ToolRegistryEntry>;
export type ToolRegistry = Record<ToolId, ToolRegistryEntry>;
export const SUBCATEGORY_ORDER: SubcategoryId[] = [
+40 -17
View File
@@ -13,7 +13,15 @@ import RemovePages from "../tools/RemovePages";
import ReorganizePages from "../tools/ReorganizePages";
import { reorganizePagesOperationConfig } from "../hooks/tools/reorganizePages/useReorganizePagesOperation";
import RemovePassword from "../tools/RemovePassword";
import { SubcategoryId, ToolCategoryId, ToolRegistry } from "./toolsTaxonomy";
import {
SubcategoryId,
ToolCategoryId,
ToolRegistry,
RegularToolRegistry,
SuperToolRegistry,
LinkToolRegistry,
} from "./toolsTaxonomy";
import { isSuperToolId, isLinkToolId } from '../types/toolId';
import AdjustContrast from "../tools/AdjustContrast";
import AdjustContrastSingleStepSettings from "../components/tools/adjustContrast/AdjustContrastSingleStepSettings";
import { adjustContrastOperationConfig } from "../hooks/tools/adjustContrast/useAdjustContrastOperation";
@@ -109,14 +117,18 @@ import RemoveBlanksSettings from "../components/tools/removeBlanks/RemoveBlanksS
import AddPageNumbersAutomationSettings from "../components/tools/addPageNumbers/AddPageNumbersAutomationSettings";
import OverlayPdfsSettings from "../components/tools/overlayPdfs/OverlayPdfsSettings";
import ValidateSignature from "../tools/ValidateSignature";
const showPlaceholderTools = true; // Show all tools; grey out unavailable ones in UI
// Convert tool supported file formats
import Automate from "../tools/Automate";
import { CONVERT_SUPPORTED_FORMATS } from "../constants/convertSupportedFornats";
export interface TranslatedToolCatalog {
allTools: ToolRegistry;
regularTools: RegularToolRegistry;
superTools: SuperToolRegistry;
linkTools: LinkToolRegistry;
}
// Hook to get the translated tool registry
export function useFlatToolRegistry(): ToolRegistry {
export function useTranslatedToolCatalog(): TranslatedToolCatalog {
const { t } = useTranslation();
return useMemo(() => {
@@ -564,7 +576,7 @@ export function useFlatToolRegistry(): ToolRegistry {
automate: {
icon: <LocalIcon icon="automation-outline" width="1.5rem" height="1.5rem" />,
name: t("home.automate.title", "Automate"),
component: React.lazy(() => import("../tools/Automate")),
component: Automate,
description: t(
"home.automate.desc",
"Build multi-step workflows by chaining together PDF actions. Ideal for recurring tasks."
@@ -829,15 +841,26 @@ export function useFlatToolRegistry(): ToolRegistry {
},
};
if (showPlaceholderTools) {
return allTools;
}
const filteredTools = Object.keys(allTools)
.filter((key) => allTools[key as ToolId].component !== null || allTools[key as ToolId].link)
.reduce((obj, key) => {
obj[key as ToolId] = allTools[key as ToolId];
return obj;
}, {} as ToolRegistry);
return filteredTools;
const regularTools = {} as RegularToolRegistry;
const superTools = {} as SuperToolRegistry;
const linkTools = {} as LinkToolRegistry;
Object.entries(allTools).forEach(([key, entry]) => {
const toolId = key as ToolId;
if (isSuperToolId(toolId)) {
superTools[toolId] = entry;
} else if (isLinkToolId(toolId)) {
linkTools[toolId] = entry;
} else {
regularTools[toolId] = entry;
}
});
return {
allTools,
regularTools,
superTools,
linkTools,
};
}, [t]); // Only re-compute when translations change
}