← back to Omega Watches 2

src/app/(dashboard)/audit/page.tsx

92 lines

"use client";

import { useEffect, useState } from "react";
import { api } from "@/lib/api-client";
import { formatDateTime } from "@/lib/utils";

export default function AuditLogPage() {
  const [logs, setLogs] = useState<any[]>([]);
  const [error, setError] = useState("");
  const [filter, setFilter] = useState({ table: "", operation: "" });

  useEffect(() => {
    api.getAuditLog(filter.table || filter.operation ? filter : undefined)
      .then(setLogs)
      .catch((e) => setError(e.message));
  }, [filter]);

  return (
    <div className="space-y-6">
      <h1 className="text-xl font-bold text-gray-100">Audit Log</h1>

      <div className="flex gap-3">
        <select
          className="input w-48"
          value={filter.table}
          onChange={(e) => setFilter((f) => ({ ...f, table: e.target.value }))}
        >
          <option value="">All Tables</option>
          <option value="market_event">market_event</option>
          <option value="msrp_snapshot">msrp_snapshot</option>
          <option value="watch_reference">watch_reference</option>
        </select>
        <select
          className="input w-36"
          value={filter.operation}
          onChange={(e) => setFilter((f) => ({ ...f, operation: e.target.value }))}
        >
          <option value="">All Ops</option>
          <option value="INSERT">INSERT</option>
          <option value="UPDATE">UPDATE</option>
          <option value="DELETE">DELETE</option>
        </select>
      </div>

      {error && <div className="rounded bg-red-900/30 p-3 text-red-300">{error}</div>}

      <div className="card border-navy-700 p-0">
        <div className="overflow-x-auto">
          <table className="w-full text-sm">
            <thead>
              <tr className="border-b border-navy-700 text-left text-xs uppercase text-gray-500">
                <th className="p-3">Time</th>
                <th className="p-3">Table</th>
                <th className="p-3">Op</th>
                <th className="p-3">Record ID</th>
                <th className="p-3">Changed By</th>
                <th className="p-3">Details</th>
              </tr>
            </thead>
            <tbody>
              {logs.length === 0 ? (
                <tr>
                  <td colSpan={6} className="p-6 text-center text-gray-500">
                    No audit entries yet. Changes to market_event and other tables are logged automatically.
                  </td>
                </tr>
              ) : (
                logs.map((l) => (
                  <tr key={l.id} className="border-b border-navy-800 hover:bg-navy-900">
                    <td className="p-3 text-xs text-gray-500">{formatDateTime(l.changed_at)}</td>
                    <td className="p-3 font-mono text-xs text-gray-300">{l.table_name}</td>
                    <td className="p-3">
                      <span className={`badge ${l.operation === "INSERT" ? "badge-success" : l.operation === "DELETE" ? "badge-error" : "badge-warning"}`}>
                        {l.operation}
                      </span>
                    </td>
                    <td className="p-3 font-mono text-xs text-gray-400">{l.record_id?.slice(0, 12)}...</td>
                    <td className="p-3 text-gray-400">{l.changed_by}</td>
                    <td className="p-3 text-xs text-gray-500">
                      {l.new_values ? `${Object.keys(l.new_values).length} fields` : "-"}
                    </td>
                  </tr>
                ))
              )}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  );
}