Skip to content

Open-source status page, incident response and uptime monitoring

7.0 KB
Raw Blame
"use client";

import type { RouterOutputs } from "@openstatus/api";
import {
  ApiKey as KeyIcon,
  Email,
  Server as ServerIcon,
} from "@openstatus/icons";
import { ModelContextProtocolIcon, SlackIcon } from "@openstatus/icons/brand";
import {
  Avatar,
  AvatarFallback,
  AvatarImage,
} from "@openstatus/ui/components/ui/avatar";
import { Badge } from "@openstatus/ui/components/ui/badge";
import type { ColumnDef } from "@tanstack/react-table";
import type { ComponentType } from "react";

import { cn } from "@/lib/utils";

import { TableCellDate } from "../table-cell-date";
import { TableCellEmpty } from "../table-cell-empty";

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

/**
 * djb2 — tiny deterministic string hash. Used to derive a stable avatar
 * seed from the user's email without shipping the email itself to
 * dicebear's URL. Not cryptographic (nothing about the avatar needs to
 * be secret), just non-leaking: the third-party service receives an
 * opaque slug, not a workspace member's address.
 */
function hashEmailSeed(email: string): string {
  let hash = 5381;
  for (let i = 0; i < email.length; i++) {
    hash = (hash * 33) ^ email.charCodeAt(i);
  }
  return (hash >>> 0).toString(36);
}

function getActionBadgeColor(action: string) {
  if (action.endsWith(".create"))
    return "bg-success/10 text-success border-success/20";
  if (action.endsWith(".update")) return "bg-info/10 text-info border-info/20";
  if (action.endsWith(".delete"))
    return "bg-destructive/10 text-destructive border-destructive/20";
  return "bg-muted/10 text-muted-foreground border-muted/20";
}

// Channel marker next to a resolved user; `actorType` is otherwise hidden.
const viaSource: Record<
  string,
  { Icon: ComponentType<{ className?: string }>; label: string }
> = {
  slack: { Icon: SlackIcon, label: "Slack" },
  apiKey: { Icon: KeyIcon, label: "API Key" },
  mcp: { Icon: ModelContextProtocolIcon, label: "MCP Server" },
};

export const columns: ColumnDef<AuditLog>[] = [
  {
    accessorKey: "user",
    header: "Actor",
    enableSorting: false,
    enableHiding: false,
    cell: ({ row }) => {
      const user = row.original.user;
      const type = row.original.actorType;
      if (!user) {
        if (type === "apiKey") {
          return (
            <div className="flex items-center gap-2">
              <div className="bg-muted-foreground/10 flex size-6 items-center justify-center rounded-md">
                <KeyIcon className="text-muted-foreground size-4" />
              </div>
              <span className="text-muted-foreground">API Key</span>
            </div>
          );
        }
        if (type === "slack") {
          return (
            <div className="flex items-center gap-2">
              <div className="bg-muted-foreground/10 flex size-6 items-center justify-center rounded-md">
                <SlackIcon className="text-muted-foreground size-4" />
              </div>
              <span className="text-muted-foreground">Slack</span>
            </div>
          );
        }
        if (type === "mcp") {
          return (
            <div className="flex items-center gap-2">
              <div className="bg-muted-foreground/10 flex size-6 items-center justify-center rounded-md">
                <ModelContextProtocolIcon className="text-muted-foreground size-4" />
              </div>
              <span className="text-muted-foreground">MCP Server</span>
            </div>
          );
        }
        if (type === "system") {
          return (
            <div className="flex items-center gap-2">
              <div className="bg-muted-foreground/10 flex size-6 items-center justify-center rounded-md">
                <ServerIcon className="text-muted-foreground size-4" />
              </div>
              <span className="text-muted-foreground">System</span>
            </div>
          );
        }
        if (type === "subscriber") {
          return (
            <div className="flex items-center gap-2">
              <div className="bg-muted-foreground/10 flex size-6 items-center justify-center rounded-md">
                <Email className="text-muted-foreground size-4" />
              </div>
              <span className="text-muted-foreground">Subscriber</span>
            </div>
          );
        }
        return <TableCellEmpty>Unknown</TableCellEmpty>;
      }
      const via = viaSource[type];
      const imgSrc =
        user.photoUrl ||
        // Seed with a hash of the email — dicebear logs request
        // URLs, so shipping raw emails as query params would
        // leak workspace member PII to a third party on every
        // audit-log render. Hashing keeps the avatar stable per
        // user without exposing the address.
        `https://api.dicebear.com/9.x/glass/svg?seed=${user.email ? hashEmailSeed(user.email) : user.id}`;
      return (
        <div className="flex items-center gap-2">
          <Avatar className="size-6 rounded-md">
            <AvatarImage src={imgSrc} />
            <AvatarFallback className="rounded-lg uppercase">
              {user?.name?.slice(0, 2)}
            </AvatarFallback>
          </Avatar>
          <span className="font-commit-mono truncate text-xs tracking-tight">
            {user.name ?? user.email ?? "—"}
          </span>
          {via && (
            <span
              className="text-muted-foreground flex shrink-0"
              title={`via ${via.label}`}
              aria-label={`via ${via.label}`}
            >
              <via.Icon className="size-3.5" />
            </span>
          )}
        </div>
      );
    },
    meta: {
      cellClassName: "max-w-[200px] truncate max-w-auto",
    },
  },
  {
    accessorKey: "action",
    header: "Action",
    enableSorting: false,
    enableHiding: false,
    filterFn: (row, id, filterValue: string[]) => {
      if (!filterValue?.length) return true;
      return filterValue.includes(row.getValue(id) as string);
    },
    cell: ({ row }) => {
      const value = row.getValue("action") as string;
      return (
        <Badge
          variant="outline"
          className={cn("font-mono text-xs", getActionBadgeColor(value))}
        >
          {value}
        </Badge>
      );
    },
  },
  {
    accessorKey: "actorType",
    header: "",
    cell: () => null,
    enableHiding: true,
    filterFn: (row, id, filterValue: string[]) => {
      if (!filterValue?.length) return true;
      return filterValue.includes(row.getValue(id) as string);
    },
  },
  {
    accessorKey: "entityType",
    header: "",
    cell: () => null,
    enableHiding: true,
    filterFn: (row, id, filterValue: string[]) => {
      if (!filterValue?.length) return true;
      return filterValue.includes(row.getValue(id) as string);
    },
  },
  {
    accessorKey: "createdAt",
    header: "Timestamp",
    enableSorting: false,
    enableHiding: false,
    cell: ({ row }) => {
      const value = row.getValue("createdAt");
      return <TableCellDate value={value} className="font-mono" />;
    },
    meta: {
      headerClassName: "text-right",
      cellClassName: "text-right",
    },
  },
];