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.

feat: always show History button, show enable prompt when audit log disabled

Sidwebworks3bb05903e6cf
diff --git a/apps/erp/app/components/AuditLog/AuditLogDrawer.tsx b/apps/erp/app/components/AuditLog/AuditLogDrawer.tsx
index 9bc0996a93..1fd54d91f0 100644
--- a/apps/erp/app/components/AuditLog/AuditLogDrawer.tsx
+++ b/apps/erp/app/components/AuditLog/AuditLogDrawer.tsx
@@ -2,6 +2,7 @@ import { getTableLabel } from "@carbon/database/audit.config";
import type { AuditLogEntry } from "@carbon/database/audit.types";
import {
Badge,
+ Button,
cn,
Drawer,
DrawerBody,
@@ -14,10 +15,18 @@ import {
} from "@carbon/react";
import { formatDateTime } from "@carbon/utils";
import { memo, useEffect, useRef } from "react";
-import { LuFilePen, LuFilePlus, LuFileX, LuHistory } from "react-icons/lu";
-import { useFetcher } from "react-router";
+import {
+ LuFilePen,
+ LuFilePlus,
+ LuFileX,
+ LuHistory,
+ LuSettings
+} from "react-icons/lu";
+import { Link, useFetcher } from "react-router";
import { EmployeeAvatar, Empty } from "~/components";
+import { useRouteData } from "~/hooks";
import AuditLogUpgradeOverlay from "~/modules/settings/ui/AuditLog/AuditLogUpgradeOverlay";
+import { path } from "~/utils/path";
type AuditLogDrawerProps = {
isOpen: boolean;
@@ -67,10 +76,16 @@ const AuditLogDrawer = memo(
const lastLoadedRef = useRef<string | null>(null);
const loadKey = `${entityType}:${entityId}:${companyId}`;
+ const rootRouteData = useRouteData<{ auditLogEnabled: boolean }>(
+ path.to.authenticatedRoot
+ );
+ const auditLogEnabled = rootRouteData?.auditLogEnabled ?? false;
+
// Load audit log data when drawer opens or entity changes
useEffect(() => {
if (
planRestricted ||
+ !auditLogEnabled ||
!isOpen ||
!entityType ||
!entityId ||
@@ -93,7 +108,8 @@ const AuditLogDrawer = memo(
companyId,
loadKey,
fetcher,
- planRestricted
+ planRestricted,
+ auditLogEnabled
]);
// Reset tracking when drawer closes so it re-fetches on next open
@@ -106,6 +122,41 @@ const AuditLogDrawer = memo(
const entries = fetcher.data?.entries ?? [];
const isLoading = fetcher.state === "loading";
+ const drawerBody = planRestricted ? (
+ <AuditLogUpgradeOverlay />
+ ) : !auditLogEnabled ? (
+ <div className="flex flex-col items-center justify-center h-full text-center gap-4 px-4">
+ <div className="rounded-full bg-muted p-3">
+ <LuHistory className="size-6 text-muted-foreground" />
+ </div>
+ <div className="space-y-2">
+ <h3 className="text-lg font-semibold">
+ Audit logging is not enabled
+ </h3>
+ <p className="text-sm text-muted-foreground text-balance">
+ Enable audit logging in settings to start tracking changes to your
+ data.
+ </p>
+ </div>
+ <Button variant="secondary" leftIcon={<LuSettings />} asChild>
+ <Link to={path.to.auditLog}>Go to Settings</Link>
+ </Button>
+ </div>
+ ) : isLoading ? (
+ <VStack spacing={3}>
+ <Skeleton className="w-full h-[151px]" />
+ <Skeleton className="w-full h-[151px]" />
+ </VStack>
+ ) : entries.length === 0 ? (
+ <Empty />
+ ) : (
+ <VStack spacing={3}>
+ {entries.map((entry) => (
+ <AuditLogEntryCard key={entry.id} entry={entry} />
+ ))}
+ </VStack>
+ );
+
return (
<Drawer
open={isOpen}
@@ -120,24 +171,7 @@ const AuditLogDrawer = memo(
History
</DrawerTitle>
</DrawerHeader>
- <DrawerBody>
- {planRestricted ? (
- <AuditLogUpgradeOverlay />
- ) : isLoading ? (
- <VStack spacing={3}>
- <Skeleton className="w-full h-[151px]" />
- <Skeleton className="w-full h-[151px]" />
- </VStack>
- ) : entries.length === 0 ? (
- <Empty />
- ) : (
- <VStack spacing={3}>
- {entries.map((entry) => (
- <AuditLogEntryCard key={entry.id} entry={entry} />
- ))}
- </VStack>
- )}
- </DrawerBody>
+ <DrawerBody>{drawerBody}</DrawerBody>
</DrawerContent>
</Drawer>
);
diff --git a/apps/erp/app/components/AuditLog/useAuditLog.tsx b/apps/erp/app/components/AuditLog/useAuditLog.tsx
index bf1fde69a2..3e0a6b187b 100644
--- a/apps/erp/app/components/AuditLog/useAuditLog.tsx
+++ b/apps/erp/app/components/AuditLog/useAuditLog.tsx
@@ -7,12 +7,8 @@ import {
} from "@carbon/react";
import { usePlan } from "@carbon/remix";
import { Plan } from "@carbon/utils";
-import { Suspense } from "react";
import { LuHistory } from "react-icons/lu";
-import { Await } from "react-router";
-import { useRouteData } from "~/hooks";
import { useFlags } from "~/hooks/useFlags";
-import { path } from "~/utils/path";
import AuditLogDrawer from "./AuditLogDrawer";
type UseAuditLogOptions = {
@@ -41,20 +37,25 @@ export function useAuditLog({
const plan = usePlan();
const { isCloud } = useFlags();
- const rootRouteData = useRouteData<{
- auditLogEnabled: Promise<boolean>;
- }>(path.to.authenticatedRoot);
-
const isStarterTeaser = isCloud && plan === Plan.Starter;
- const trigger = (
- <AuditLogTrigger
- variant={variant}
- onOpen={disclosure.onOpen}
- auditLogEnabledPromise={rootRouteData?.auditLogEnabled}
- isStarterTeaser={isStarterTeaser}
- />
- );
+ const trigger =
+ variant === "dropdown" ? (
+ <DropdownMenuItem onClick={disclosure.onOpen}>
+ <DropdownMenuIcon icon={<LuHistory />} />
+ History
+ </DropdownMenuItem>
+ ) : (
+ <CardAction>
+ <Button
+ variant="secondary"
+ leftIcon={<LuHistory />}
+ onClick={disclosure.onOpen}
+ >
+ History
+ </Button>
+ </CardAction>
+ );
const drawer = (
<AuditLogDrawer
@@ -69,60 +70,3 @@ export function useAuditLog({
return { trigger, drawer };
}
-
-// -- Internal components --
-
-type AuditLogTriggerProps = {
- variant: "dropdown" | "card-action";
- onOpen: () => void;
- auditLogEnabledPromise: Promise<boolean> | undefined;
- isStarterTeaser: boolean;
-};
-
-function HistoryTrigger({
- variant,
- onOpen
-}: {
- variant: "dropdown" | "card-action";
- onOpen: () => void;
-}) {
- if (variant === "dropdown") {
- return (
- <DropdownMenuItem onClick={onOpen}>
- <DropdownMenuIcon icon={<LuHistory />} />
- History
- </DropdownMenuItem>
- );
- }
-
- return (
- <CardAction>
- <Button variant="secondary" leftIcon={<LuHistory />} onClick={onOpen}>
- History
- </Button>
- </CardAction>
- );
-}
-
-function AuditLogTrigger({
- variant,
- onOpen,
- auditLogEnabledPromise,
- isStarterTeaser
-}: AuditLogTriggerProps) {
- // Starter teaser: always show the button (no need to resolve auditLogEnabled)
- if (isStarterTeaser) {
- return <HistoryTrigger variant={variant} onOpen={onOpen} />;
- }
-
- return (
- <Suspense fallback={null}>
- <Await resolve={auditLogEnabledPromise}>
- {(auditLogEnabled) => {
- if (!auditLogEnabled) return null;
- return <HistoryTrigger variant={variant} onOpen={onOpen} />;
- }}
- </Await>
- </Suspense>
- );
-}