Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

7.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { useFeatureFlags } from "@carbon/react";
import { useLingui } from "@lingui/react/macro";
import { useMemo } from "react";
import {
  LuBlocks,
  LuBox,
  LuCalendarClock,
  LuCircleCheck,
  LuClipboardCheck,
  LuCreditCard,
  LuCrown,
  LuDatabase,
  LuFactory,
  LuFileText,
  LuFlaskConical,
  LuHistory,
  LuImage,
  LuKey,
  LuLandmark,
  LuLayoutDashboard,
  LuNetwork,
  LuPrinter,
  LuScanBarcode,
  LuSheet,
  LuShieldCheck,
  LuShoppingCart,
  LuUsers,
  LuWebhook,
  LuWorkflow,
  LuWrench
} from "react-icons/lu";
import { usePermissions } from "~/hooks";
import { useFlags } from "~/hooks/useFlags";
import { usePlanGate } from "~/hooks/usePlanGate";
import type { AuthenticatedRouteGroup, Role } from "~/types";
import { path } from "~/utils/path";

export default function useSettingsSubmodules() {
  const { t } = useLingui();
  const permissions = usePermissions();
  const { isCloud, isControlledEnvironment, isInternal, isLocalDev } =
    useFlags();
  const { feature } = useFeatureFlags();
  const canManageCompanies = isInternal || feature("multi-company-management");
  // Enterprise: backups are a Business plan feature, but internal staff and
  // local dev keep access (mirrors the server `canManageBackups`).
  const { isGated: backupsGated } = usePlanGate({ feature: "BACKUPS" });

  const settingsRoutes: AuthenticatedRouteGroup<{
    requiresOwnership?: boolean;
    requiresCloudEnvironment?: boolean;
    requiresControlledEnvironment?: boolean;
  }>[] = useMemo(
    () => [
      {
        name: t`Company`,
        routes: [
          {
            name: t`Billing`,
            to: path.to.billing,
            role: "employee",
            icon: <LuCreditCard />,
            requiresOwnership: true,
            requiresCloudEnvironment: true
          },
          {
            name: t`Company`,
            to: path.to.company,
            role: "employee",
            icon: <LuFactory />
          },
          {
            name: t`Companies`,
            to: path.to.companies,
            role: "employee",
            icon: <LuNetwork />
          },
          {
            name: t`Document Templates`,
            to: path.to.documentTemplates,
            role: "employee",
            icon: <LuFileText />
          },
          {
            name: t`Logos`,
            to: path.to.logos,
            role: "employee",
            icon: <LuImage />
          },
          {
            name: t`Printing`,
            to: path.to.printingSettings,
            role: "employee",
            icon: <LuPrinter />
          }
        ]
      },
      {
        name: t`Modules`,
        routes: [
          {
            name: t`Accounting`,
            to: path.to.accountingSettings,
            role: "employee",
            icon: <LuLandmark />
          },
          {
            name: t`Inventory`,
            to: path.to.inventorySettings,
            role: "employee",
            icon: <LuBox />
          },
          {
            name: t`Invoicing`,
            to: path.to.invoicingSettings,
            role: "employee",
            icon: <LuFileText />
          },
          {
            name: t`Items`,
            to: path.to.itemsSettings,
            role: "employee",
            icon: <LuBlocks />
          },
          {
            name: t`People`,
            to: path.to.peopleSettings,
            role: "employee",
            icon: <LuUsers />
          },
          {
            name: t`Planning`,
            to: path.to.planningSettings,
            role: "employee",
            icon: <LuCalendarClock />
          },
          {
            name: t`Purchasing`,
            to: path.to.purchasingSettings,
            role: "employee",
            icon: <LuShoppingCart />
          },
          {
            name: t`Production`,
            to: path.to.productionSettings,
            role: "employee",
            icon: <LuFactory />
          },
          {
            name: t`Quality`,
            to: path.to.qualitySettings,
            role: "employee",
            icon: <LuClipboardCheck />
          },
          {
            name: t`Sales`,
            to: path.to.salesSettings,
            role: "employee",
            icon: <LuCrown />
          },
          {
            name: t`Resources`,
            to: path.to.resourcesSettings,
            role: "employee",
            icon: <LuWrench />
          }
        ]
      },
      {
        name: t`System`,
        routes: [
          {
            name: t`API Keys`,
            to: path.to.apiKeys,
            role: "employee",
            icon: <LuKey />
          },
          {
            name: t`Approval Rules`,
            to: path.to.approvalRules,
            role: "employee",
            icon: <LuCircleCheck />
          },
          {
            name: t`Audit Logs`,
            to: path.to.auditLog,
            role: "employee",
            icon: <LuHistory />
          },
          {
            name: t`Backups`,
            to: path.to.backups,
            role: "employee",
            icon: <LuDatabase />
          },
          {
            name: t`Custom Fields`,
            to: path.to.customFields,
            role: "employee",
            icon: <LuLayoutDashboard />
          },
          {
            name: t`Demo Data`,
            to: path.to.demoData,
            role: "employee",
            icon: <LuFlaskConical />
          },
          {
            name: t`Integrations`,
            to: path.to.integrations,
            role: "employee",
            icon: <LuWorkflow />
          },
          {
            name: t`ITAR Certifications`,
            to: path.to.itarCertifications,
            role: "employee",
            icon: <LuClipboardCheck />,
            requiresControlledEnvironment: true
          },
          {
            name: t`Security`,
            to: path.to.security,
            role: "employee",
            icon: <LuShieldCheck />
          },
          {
            name: t`Sequences`,
            to: path.to.sequences,
            role: "employee",
            icon: <LuSheet />
          },
          {
            name: t`Serial Numbers`,
            to: path.to.serialNumberSequences,
            role: "employee",
            icon: <LuScanBarcode />
          },
          {
            name: t`Webhooks`,
            to: path.to.webhooks,
            role: "employee",
            icon: <LuWebhook />
          }
        ]
      }
    ],
    [t]
  );

  const isRouteVisible = (route: {
    to: string;
    role?: string;
    requiresOwnership?: boolean;
    requiresCloudEnvironment?: boolean;
    requiresControlledEnvironment?: boolean;
  }) => {
    if (route.role && !permissions.is(route.role as Role)) return false;
    if (route.requiresOwnership && !permissions.isOwner()) return false;
    if (route.requiresCloudEnvironment && !isCloud) return false;
    if (route.requiresControlledEnvironment && !isControlledEnvironment)
      return false;
    if (route.to === path.to.companies && !canManageCompanies) return false;
    // Backups: visible to Business/Enterprise, internal staff, or local dev.
    if (
      route.to === path.to.backups &&
      backupsGated &&
      !isInternal &&
      !isLocalDev
    )
      return false;
    return true;
  };

  return {
    groups: settingsRoutes
      .filter((group) => group.routes.some(isRouteVisible))
      .map((group) => ({
        ...group,
        routes: group.routes.filter(isRouteVisible)
      }))
  };
}