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.

2.2 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 {
  Button,
  CardAction,
  DropdownMenuIcon,
  DropdownMenuItem,
  useDisclosure
} from "@carbon/react";
import { LuHistory } from "react-icons/lu";
import { usePlanGate } from "~/hooks/usePlanGate";
import AuditLogDrawer from "./AuditLogDrawer";

type UseAuditLogOptions = {
  entityType: string;
  entityId: string;
  companyId: string;
  variant: "dropdown" | "card-action";
  triggerLabel?: React.ReactNode;
  drawerTitle?: React.ReactNode;
  /** Opt-in: offer a CSV download of the full history in the drawer. */
  downloadable?: boolean;
  /** Leads the downloaded file's name, e.g. the record's readable id. */
  downloadName?: string;
};

/**
 * Hook that returns audit log trigger and drawer elements.
 *
 * Place `trigger` inside the dropdown menu (or card header).
 * Place `drawer` at the component root level (outside any dropdown).
 *
 * This separation is necessary because Radix DropdownMenuContent unmounts
 * its children when the menu closes — the drawer must live outside it.
 */
export function useAuditLog({
  entityType,
  entityId,
  companyId,
  variant,
  triggerLabel = "History",
  drawerTitle,
  downloadable,
  downloadName
}: UseAuditLogOptions) {
  const disclosure = useDisclosure();
  const { isGated } = usePlanGate({ feature: "AUDIT_LOG" });

  const trigger =
    variant === "dropdown" ? (
      <DropdownMenuItem onClick={disclosure.onOpen}>
        <DropdownMenuIcon icon={<LuHistory />} />
        {triggerLabel}
      </DropdownMenuItem>
    ) : (
      <CardAction>
        <Button
          variant="secondary"
          leftIcon={<LuHistory />}
          onClick={disclosure.onOpen}
        >
          {triggerLabel}
        </Button>
      </CardAction>
    );

  const drawer = (
    <AuditLogDrawer
      isOpen={disclosure.isOpen}
      onClose={disclosure.onClose}
      entityType={entityType}
      entityId={entityId}
      companyId={companyId}
      title={drawerTitle}
      planRestricted={isGated}
      downloadable={downloadable}
      downloadName={downloadName}
    />
  );

  return { trigger, drawer };
}