Skip to content

Open-source status page, incident response and uptime monitoring

6.4 KB
Raw Blame
"use client";

import type { RouterOutputs } from "@openstatus/api";
import { AuditLog, Lock } from "@openstatus/icons";
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";

import { Link } from "@/components/common/link";
import {
  BillingOverlay,
  BillingOverlayButton,
  BillingOverlayContainer,
  BillingOverlayDescription,
} from "@/components/content/billing-overlay";
import {
  EmptyStateDescription,
  EmptyStateMedia,
  EmptyStateTitle,
} from "@/components/content/empty-state";
import {
  Section,
  SectionDescription,
  SectionGroup,
  SectionHeader,
  SectionTitle,
} from "@/components/content/section";
import { columns } from "@/components/data-table/audit-logs-workspace/columns";
import { DataTableRowDetails } from "@/components/data-table/audit-logs-workspace/data-table-row-details";
import { AuditLogsDataTableToolbar } from "@/components/data-table/audit-logs-workspace/data-table-toolbar";
import { UpgradeDialog } from "@/components/dialogs/upgrade";
import { DataTable } from "@/components/ui/data-table/data-table";
import { DataTableEmpty } from "@/components/ui/data-table/data-table-empty";
import { DataTablePaginationSimple } from "@/components/ui/data-table/data-table-pagination";
import { useTRPC } from "@/lib/trpc/client";

type AuditLog = RouterOutputs["auditLog"]["list"]["items"][number];

const EXAMPLES = [
  {
    id: 1,
    workspaceId: 1,
    actorType: "user",
    actorId: "42",
    actorUserId: 42,
    action: "monitor.update",
    entityType: "monitor",
    entityId: "101",
    before: null,
    after: null,
    metadata: null,
    changedFields: ["name", "url"],
    createdAt: new Date("2026-04-24T10:00:00Z"),
    user: {
      id: 42,
      name: "Maximilian Kaske",
      email: "[email protected]",
      photoUrl: null,
    },
  },
  {
    id: 2,
    workspaceId: 1,
    actorType: "user",
    actorId: "42",
    actorUserId: 42,
    action: "page.create",
    entityType: "page",
    entityId: "7",
    before: null,
    after: null,
    metadata: null,
    changedFields: null,
    createdAt: new Date("2026-04-23T16:30:00Z"),
    user: {
      id: 42,
      name: "Thibault Le Ouay Ducasse",
      email: "[email protected]",
      photoUrl: null,
    },
  },
  {
    id: 3,
    workspaceId: 1,
    actorType: "slack",
    actorId: "k_abc123",
    actorUserId: null,
    action: "status_report.create",
    entityType: "status_report",
    entityId: "12",
    before: null,
    after: null,
    metadata: null,
    changedFields: null,
    createdAt: new Date("2026-04-22T09:15:00Z"),
    user: null,
  },
] satisfies AuditLog[];

export function Client() {
  const trpc = useTRPC();
  const [openDialog, setOpenDialog] = useState(false);
  const { data: workspace } = useQuery(trpc.workspace.get.queryOptions());
  const {
    data: auditLogs,
    isPending: auditLogsPending,
    isError: auditLogsError,
  } = useQuery(trpc.auditLog.list.queryOptions({}));

  if (!workspace) return null;

  const isLimited = workspace.limits["audit-log"] === false;

  // a failed list still renders the page with an empty table (see below)
  if (!isLimited && auditLogsPending) return null;

  return (
    <SectionGroup>
      <SectionHeader>
        <SectionTitle>Audit Logs</SectionTitle>
        <SectionDescription>
          Track mutating actions taken against your workspace from the last 14
          days.
        </SectionDescription>
      </SectionHeader>
      <Section>
        {isLimited ? (
          <BillingOverlayContainer>
            <DataTable
              paginationComponent={DataTablePaginationSimple}
              columns={columns}
              data={[...EXAMPLES, ...EXAMPLES, ...EXAMPLES]}
              defaultColumnVisibility={{
                actorType: false,
                entityType: false,
              }}
              toolbarComponent={AuditLogsDataTableToolbar}
              rowComponent={({ row }) => (
                <DataTableRowDetails row={row.original} />
              )}
            />
            <BillingOverlay>
              <BillingOverlayButton
                data-track="paywall_viewed"
                data-limit="audit-log"
                onClick={() => setOpenDialog(true)}
              >
                <Lock />
                Upgrade
              </BillingOverlayButton>
              <BillingOverlayDescription>
                Keep a verifiable history of every change in your workspace.{" "}
                <Link
                  href="https://www.openstatus.dev/docs/"
                  rel="noreferrer"
                  target="_blank"
                >
                  Learn more
                </Link>
                .
              </BillingOverlayDescription>
            </BillingOverlay>
            <UpgradeDialog
              open={openDialog}
              onOpenChange={setOpenDialog}
              limit="audit-log"
            />
          </BillingOverlayContainer>
        ) : (
          <DataTable
            columns={columns}
            data={auditLogs?.items ?? []}
            defaultColumnVisibility={{
              actorType: false,
              entityType: false,
            }}
            toolbarComponent={AuditLogsDataTableToolbar}
            onRowClick={(row) =>
              row.getCanExpand() ? row.toggleExpanded() : undefined
            }
            rowComponent={({ row }) => (
              <DataTableRowDetails row={row.original} />
            )}
            paginationComponent={DataTablePaginationSimple}
            emptyState={
              <DataTableEmpty>
                <EmptyStateMedia>
                  <AuditLog />
                </EmptyStateMedia>
                {auditLogsError ? (
                  <>
                    <EmptyStateTitle>
                      Audit logs could not be loaded
                    </EmptyStateTitle>
                    <EmptyStateDescription>
                      Try again in a moment.
                    </EmptyStateDescription>
                  </>
                ) : (
                  <>
                    <EmptyStateTitle>No audit logs yet</EmptyStateTitle>
                    <EmptyStateDescription>
                      Every create, update and delete in this workspace is
                      recorded here with who did it and when.
                    </EmptyStateDescription>
                  </>
                )}
              </DataTableEmpty>
            }
          />
        )}
      </Section>
    </SectionGroup>
  );
}