Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

18.6 KB
Raw Blame
"use client";

import { useState } from "react";
import { AnalystWorkload } from "./AnalystWorkload";
import useSWR from "swr";
import Link from "next/link";
import toast from "react-hot-toast";
import { casesApi, type Case, type CasesResponse } from "@/lib/api";
import { clsx } from "clsx";
import { format } from "date-fns";
import { EmptyState, EmptyStateIcons } from "@/components/ui/EmptyState";
import { SavedViewsBar } from "@/components/saved-views/SavedViewsBar";
import { demoFallback } from "@/lib/demoFallback";

// WS-F3 — the saved-views API stores an opaque filter blob per view, so we
// flatten the three filter slices Cases tracks today into a single shape the
// bar can snapshot and replay. Nothing else in the view consumes this — it's
// purely the wire format between the bar and the local state setters.
type CaseFilterSnapshot = {
  status: Case["status"] | "all";
  severity: Case["severity"] | "all";
  search: string;
};

// ─── Mock Data ────────────────────────────────────────────────────────────────

// Deterministic mock data — no Date.now() or Math.random() to avoid SSR hydration mismatches.
const MOCK_CASE_BASE = new Date("2026-05-06T12:00:00Z").getTime();
const MOCK_CASES: Case[] = Array.from({ length: 18 }, (_, i) => ({
  id: `case-${1000 + i}`,
  title: [
    "Ransomware incident on finance workstations",
    "Suspected APT lateral movement campaign",
    "Credential stuffing attack against portal",
    "Data exfiltration via cloud storage abuse",
    "Supply chain compromise investigation",
    "Insider threat: anomalous data access",
    "Phishing campaign targeting executives",
    "Cryptominer on dev server cluster",
    "Brute-force attack on VPN endpoints",
    "Unauthorized cloud resource provisioning",
  ][i % 10],
  status: (["open", "in_progress", "resolved", "closed"] as Case["status"][])[i % 4],
  severity: (["critical", "high", "medium", "low"] as Case["severity"][])[i % 4],
  assignee: ["[email protected]", "[email protected]", "[email protected]", undefined][i % 4],
  alertCount: ((i * 13 + 7) % 30) + 1,
  createdAt: new Date(MOCK_CASE_BASE - i * 7200000).toISOString(),
  updatedAt: new Date(MOCK_CASE_BASE - i * 1800000).toISOString(),
  tags: [
    ["ransomware", "finance"],
    ["apt", "lateral"],
    ["credential", "portal"],
    ["exfil", "cloud"],
  ][i % 4],
}));

// ─── Helpers ──────────────────────────────────────────────────────────────────

const SEVERITY_CONFIG = {
  critical: { label: "Critical", className: "text-red-400 bg-red-500/10 border-red-500/20" },
  high: { label: "High", className: "text-orange-400 bg-orange-500/10 border-orange-500/20" },
  medium: { label: "Medium", className: "text-yellow-400 bg-yellow-500/10 border-yellow-500/20" },
  low: { label: "Low", className: "text-blue-400 bg-blue-500/10 border-blue-500/20" },
};

// Keyed on the six states `aisoc_cases` permits. It used to name `open`,
// `in_progress` and `pending` -- all refused by the table's CHECK -- and omit
// the four a case actually passes through, so every real row fell through to
// the `?? STATUS_CONFIG.open` fallback below and the list showed every case
// as "Open" whatever it was.
const STATUS_CONFIG: Record<Case["status"], { label: string; className: string; dot: string }> = {
  new: {
    label: "New",
    className: "text-gray-300 bg-gray-700/50 border-gray-600/50",
    dot: "bg-gray-400",
  },
  triaged: {
    label: "Triaged",
    className: "text-sky-300 bg-sky-500/10 border-sky-500/20",
    dot: "bg-sky-400",
  },
  investigating: {
    label: "Investigating",
    className: "text-blue-300 bg-blue-500/10 border-blue-500/20",
    dot: "bg-blue-400 animate-pulse",
  },
  contained: {
    label: "Contained",
    className: "text-amber-300 bg-amber-500/10 border-amber-500/20",
    dot: "bg-amber-400",
  },
  resolved: {
    label: "Resolved",
    className: "text-green-300 bg-green-500/10 border-green-500/20",
    dot: "bg-green-400",
  },
  closed: {
    label: "Closed",
    className: "text-gray-500 bg-gray-800/50 border-gray-700/50",
    dot: "bg-gray-600",
  },
};

// ─── Case Card ────────────────────────────────────────────────────────────────

function CaseCard({ c }: { c: Case }) {
  const sev = SEVERITY_CONFIG[c.severity] ?? SEVERITY_CONFIG.medium;
  // Defensive: if normalization missed an unexpected status string, fall back
  // to "open" styling so the entire list never blanks the page.
  const sts = STATUS_CONFIG[c.status] ?? STATUS_CONFIG.new;
  const displayId = c.caseNumber ?? `${c.id ?? ""}`.slice(-6);
  const detailHref = `/cases/${encodeURIComponent(c.caseNumber ?? c.id)}`;

  return (
    <Link href={detailHref} className="block">
      <div className="bg-gray-900/60 border border-gray-800/60 rounded-xl p-5 hover:border-gray-700 hover:bg-gray-900/80 transition-all group">
        <div className="flex items-start justify-between gap-3">
          <div className="flex-1 min-w-0">
            <div className="flex items-center gap-2 mb-2">
              <span
                className={clsx("text-xs font-medium px-2 py-0.5 rounded border", sev.className)}
              >
                {sev.label}
              </span>
              <span
                className={clsx(
                  "flex items-center gap-1 text-xs font-medium px-2 py-0.5 rounded border",
                  sts.className
                )}
              >
                <span className={clsx("w-1.5 h-1.5 rounded-full", sts.dot)} />
                {sts.label}
              </span>
            </div>
            <h3 className="text-sm font-medium text-gray-200 group-hover:text-white truncate">
              {c.title}
            </h3>
            <div className="flex items-center gap-3 mt-2">
              <span className="text-xs text-gray-500">#{displayId}</span>
              <span className="text-xs text-gray-500">·</span>
              <span className="text-xs text-gray-500">{c.alertCount ?? 0} alerts</span>
              {c.assignee && (
                <>
                  <span className="text-xs text-gray-500">·</span>
                  <span className="text-xs text-gray-400">{c.assignee.split("@")[0]}</span>
                </>
              )}
            </div>
          </div>
          <div className="text-right shrink-0 flex flex-col items-end gap-2">
            <p className="text-xs text-gray-500" suppressHydrationWarning>
              {format(new Date(c.createdAt), "MMM dd, HH:mm")}
            </p>
            {c.tags && c.tags.length > 0 && (
              <div className="flex flex-wrap gap-1 justify-end">
                {c.tags.slice(0, 2).map((tag) => (
                  <span
                    key={tag}
                    className="text-xs text-gray-500 bg-gray-800 px-1.5 py-0.5 rounded"
                  >
                    {tag}
                  </span>
                ))}
              </div>
            )}
            <button
              onClick={(e) => {
                e.preventDefault();
                toast.success("Incident report generated — downloading PDF");
              }}
              className="flex items-center gap-1 text-xs text-gray-500 hover:text-blue-400 transition-colors opacity-0 group-hover:opacity-100"
            >
              <svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
                />
              </svg>
              Export Report
            </button>
          </div>
        </div>
      </div>
    </Link>
  );
}

// ─── Main View ────────────────────────────────────────────────────────────────

type FilterStatus = Case["status"] | "all";

export function CasesView() {
  const [statusFilter, setStatusFilter] = useState<FilterStatus>("all");
  const [severityFilter, setSeverityFilter] = useState<Case["severity"] | "all">("all");
  const [search, setSearch] = useState("");

  // This view took an `initialCases` prop fed by a server-side fetch in
  // `cases/page.tsx`. That fetch sent no credential and a build-time tenant
  // id, so it returned one fixed tenant's cases to whoever loaded the page,
  // and it only returned anything at all because an uncredentialed request
  // resolved to a demo administrator. A server render has no session to
  // borrow, so it was removed rather than repaired; the list now loads
  // through the credentialed client on mount, under the caller's own
  // identity.
  const sampleCases = demoFallback<CasesResponse>({
    cases: MOCK_CASES,
    total: MOCK_CASES.length,
    page: 1,
    pageSize: MOCK_CASES.length,
  });

  const {
    data: casesData,
    error,
    isLoading,
  } = useSWR(
    ["cases", statusFilter, severityFilter],
    () => casesApi.list({ status: statusFilter !== "all" ? statusFilter : undefined }),
    {
      fallbackData: sampleCases,
      revalidateOnMount: true,
    }
  );

  const cases = (casesData?.cases || []).filter((c) => {
    if (search && !c.title.toLowerCase().includes(search.toLowerCase())) return false;
    if (severityFilter !== "all" && c.severity !== severityFilter) return false;
    return true;
  });

  // The list above already falls back to `[]`; this counted MOCK_CASES, so a
  // failed request rendered an empty table under stat cards claiming 18 cases.
  const allCases = casesData?.cases ?? [];
  // Substituting `[]` for MOCK_CASES stopped the fabrication but moved the
  // defect: the grid sits above the `isLoading` branch, so five confident
  // zeros rendered on first paint and stayed there when the read failed.
  const countsUnknown = !casesData;
  const statCounts: Record<string, number | null> = countsUnknown
    ? { all: null, new: null, investigating: null, resolved: null, closed: null }
    : {
        all: allCases.length,
        new: allCases.filter((c) => c.status === "new").length,
        investigating: allCases.filter((c) => c.status === "investigating").length,
        resolved: allCases.filter((c) => c.status === "resolved").length,
        closed: allCases.filter((c) => c.status === "closed").length,
      };

  return (
    <div className="space-y-5">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-xl font-semibold text-gray-100">Cases</h1>
          <p className="text-sm text-gray-500 mt-0.5">
            Manage security investigations and incidents
          </p>
        </div>
        <div className="flex items-center gap-2">
          <button
            disabled
            title="Case creation wizard is planned for v1.1"
            className="flex items-center gap-2 bg-gray-700 text-gray-400 text-sm font-medium px-4 py-2 rounded-lg cursor-not-allowed select-none"
          >
            <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
              <path
                strokeLinecap="round"
                strokeLinejoin="round"
                strokeWidth={2}
                d="M12 4v16m8-8H4"
              />
            </svg>
            New Case
          </button>
          <span className="text-xs font-medium rounded-full bg-amber-500/15 text-amber-400 border border-amber-500/30 px-2 py-0.5">
            Planned for v1.1
          </span>
        </div>
      </div>

      {/* Depth 9.3 — who is carrying what. Its own fetch, so a workload
          failure does not blank the case list and a case-list failure does
          not blank the workload. */}
      <AnalystWorkload />

      {/* Stats */}
      <div className="grid grid-cols-5 gap-3">
        {/*
          Five tabs for a five-column grid, naming states that exist. These
          were `open` and `in_progress`, which `aisoc_cases` forbids, so both
          counters read zero on every deployment and neither filter returned
          anything. `triaged` and `contained` show on each row rather than as
          their own tab.
        */}
        {(["all", "new", "investigating", "resolved", "closed"] as const).map((s) => {
          return (
            <button
              key={s}
              onClick={() => setStatusFilter(s)}
              className={clsx(
                "bg-gray-900/60 border rounded-xl p-4 text-left transition-all",
                statusFilter === s
                  ? "border-blue-500/50 bg-blue-500/5"
                  : "border-gray-800/60 hover:border-gray-700"
              )}
            >
              <p
                className={clsx(
                  "text-2xl font-bold",
                  statCounts[s] === null ? "text-gray-600" : "text-gray-100"
                )}
              >
                {statCounts[s] === null ? "—" : statCounts[s]}
              </p>
              <p className="text-xs text-gray-500 mt-0.5 capitalize">
                {s === "all" ? "All Cases" : s.replace("_", " ")}
              </p>
            </button>
          );
        })}
      </div>

      {/*
        WS-F3 — saved views. Cases keeps three slices of filter state, so we
        synthesize a single object for the bar and unpack it on apply. We
        spread defaults to be defensive against older saved-view payloads
        that might be missing newer keys.
      */}
      <SavedViewsBar<CaseFilterSnapshot>
        viewType="cases"
        filters={{
          status: statusFilter,
          severity: severityFilter,
          search,
        }}
        onApply={(applied) => {
          setStatusFilter(applied.status ?? "all");
          setSeverityFilter(applied.severity ?? "all");
          setSearch(applied.search ?? "");
        }}
      />

      {/* Filters */}
      <div className="flex items-center gap-3">
        <div className="relative flex-1 max-w-sm">
          <svg
            className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-500"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
          >
            <path
              strokeLinecap="round"
              strokeLinejoin="round"
              strokeWidth={2}
              d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
            />
          </svg>
          <input
            type="text"
            value={search}
            onChange={(e) => setSearch(e.target.value)}
            placeholder="Search cases…"
            className="w-full pl-9 pr-4 py-2 bg-gray-900/60 border border-gray-800 rounded-lg text-sm text-gray-300 placeholder-gray-600 focus:outline-none focus:border-blue-500/50"
          />
        </div>

        {/* The visible text is inside the options, so a screen reader
            announces the current value with no idea what it selects.
            `aria-label` rather than a visible `<label>`: the control sits
            in a filter row whose meaning is carried by the options
            themselves for a sighted reader. */}
        <select
          aria-label="Filter cases by severity"
          value={severityFilter}
          onChange={(e) => setSeverityFilter(e.target.value as Case["severity"] | "all")}
          className="bg-gray-900/60 border border-gray-800 rounded-lg text-sm text-gray-400 px-3 py-2 focus:outline-none focus:border-blue-500/50"
        >
          <option value="all">All Severities</option>
          <option value="critical">Critical</option>
          <option value="high">High</option>
          <option value="medium">Medium</option>
          <option value="low">Low</option>
        </select>

        {/* The same `countsUnknown` the five stat cards above already honour.
            This one printed `0 cases` beside their five em-dashes, so the
            page gave two different answers about the same unread list — and
            the confident one was the wrong one, because a zero reads as
            measured-and-none rather than not-measured. */}
        <span
          className="text-xs text-gray-500"
          title={
            countsUnknown
              ? "The case service has not answered, so there is no count to show."
              : undefined
          }
        >
          {countsUnknown ? "— cases" : `${cases.length} cases`}
        </span>
      </div>

      {/* Cases List */}
      {isLoading ? (
        <div className="flex items-center justify-center h-48">
          <div className="w-6 h-6 border-2 border-blue-500/30 border-t-blue-500 rounded-full animate-spin" />
        </div>
      ) : error && !casesData ? (
        <EmptyState
          icon={EmptyStateIcons.case}
          title="Could not load cases"
          description="The case service did not answer. This is not a report that no cases exist — retry, or check the API is reachable."
        />
      ) : cases.length === 0 ? (
        // WS-F5 — distinguish "filter miss" from "no cases ever". The former
        // gets a "clear filters" CTA; the latter explains what cases are and
        // where they come from so a brand-new tenant doesn't think it's broken.
        statusFilter !== "all" || severityFilter !== "all" || search ? (
          <EmptyState
            icon={EmptyStateIcons.search}
            title="No cases match these filters"
            description="Try clearing the status, severity, or search filter to widen the view."
            action={
              <button
                onClick={() => {
                  setStatusFilter("all");
                  setSeverityFilter("all");
                  setSearch("");
                }}
                className="text-xs px-3 py-1.5 rounded-md border border-blue-500/40 bg-blue-500/10 text-blue-300 hover:bg-blue-500/20 transition-colors"
              >
                Clear filters
              </button>
            }
          />
        ) : (
          <EmptyState
            icon={EmptyStateIcons.case}
            title="No cases yet"
            description="Cases bundle related alerts, evidence, and analyst notes into a single investigation. Promote alerts to a case from the Alerts tab, or click ‘New Case’ to start a manual investigation."
          />
        )
      ) : (
        <div className="grid grid-cols-1 gap-3">
          {cases.map((c) => (
            <CaseCard key={c.id} c={c} />
          ))}
        </div>
      )}
    </div>
  );
}