"use client"; import { useState, useEffect } from "react"; import { ArrowLeft, Filter, X } from "lucide-react"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { fetchCredentialAuditLog, type AuditLogEntry } from "@/lib/api/credentials"; import { useWorkspaceId } from "@/lib/hooks"; const ACTIVITY_ACTIONS = [ { value: "CREDENTIAL_CREATED", label: "Created" }, { value: "CREDENTIAL_ACCESSED", label: "Accessed" }, { value: "CREDENTIAL_ROTATED", label: "Rotated" }, { value: "CREDENTIAL_REVOKED", label: "Revoked" }, { value: "UPDATED", label: "Updated" }, ]; interface FilterState { action?: string; startDate?: string; endDate?: string; } export default function CredentialAuditPage(): React.ReactElement { const [logs, setLogs] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [page, setPage] = useState(1); const [limit] = useState(20); const [totalPages, setTotalPages] = useState(0); const [filters, setFilters] = useState({}); const [hasFilters, setHasFilters] = useState(false); const workspaceId = useWorkspaceId(); useEffect(() => { if (!workspaceId) return; void loadLogs(workspaceId); }, [workspaceId, page, filters]); async function loadLogs(wsId: string): Promise { try { setIsLoading(true); const response = await fetchCredentialAuditLog(wsId, { ...filters, page, limit, }); setLogs(response.data); setTotalPages(response.meta.totalPages); setError(null); } catch (err) { setError(err instanceof Error ? err.message : "Failed to load audit logs"); setLogs([]); } finally { setIsLoading(false); } } function handleFilterChange(filterKey: keyof FilterState, value: string | undefined): void { const newFilters = { ...filters, [filterKey]: value }; if (!value) { const { [filterKey]: _, ...rest } = newFilters; setFilters(rest); setHasFilters(Object.keys(rest).length > 0); setPage(1); return; } setFilters(newFilters); setHasFilters(Object.keys(newFilters).length > 0); setPage(1); } function handleClearFilters(): void { setFilters({}); setHasFilters(false); setPage(1); } function formatTimestamp(dateString: string): string { const date = new Date(dateString); return new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric", year: "numeric", hour: "numeric", minute: "2-digit", second: "2-digit", hour12: true, }).format(date); } function getActionBadgeColor(action: string): string { const colors: Record = { CREDENTIAL_CREATED: "bg-green-100 text-green-800", CREDENTIAL_ACCESSED: "bg-blue-100 text-blue-800", CREDENTIAL_ROTATED: "bg-purple-100 text-purple-800", CREDENTIAL_REVOKED: "bg-red-100 text-red-800", UPDATED: "bg-yellow-100 text-yellow-800", }; return colors[action] ?? "bg-gray-100 text-gray-800"; } function getActionLabel(action: string): string { const label = ACTIVITY_ACTIONS.find((a) => a.value === action)?.label; return label ?? action; } return (

Credential Audit Log

View all activities related to your stored credentials

{/* Filters Card */}
Filter Logs
{hasFilters && ( )}
{/* Action Filter */}
{/* Start Date Filter */}
) => { handleFilterChange("startDate", e.target.value); }} />
{/* End Date Filter */}
) => { handleFilterChange("endDate", e.target.value); }} />
{/* Audit Logs List */} Activity History {logs.length > 0 ? `Showing ${String((page - 1) * limit + 1)}-${String(Math.min(page * limit, logs.length))} entries` : "No activities found"} {isLoading ? (

Loading audit logs...

) : error ? (

{error}

) : logs.length === 0 ? (

No audit logs found

{hasFilters ? "Try adjusting your filters" : "No credential activities yet"}

) : (
{/* Desktop view */}
{logs.map((log) => ( ))}
Timestamp Activity User Details
{formatTimestamp(log.createdAt)} {getActionLabel(log.action)}

{log.user.name ?? "Unknown"}

{log.user.email}

{(log.details.name as string) && (

Name:{" "} {log.details.name as string}

)} {(log.details.provider as string) && (

Provider:{" "} {log.details.provider as string}

)}
{/* Mobile view */}
{logs.map((log) => (
{getActionLabel(log.action)} {formatTimestamp(log.createdAt)}

{log.user.name ?? "Unknown"}

{log.user.email}

{((log.details.name as string) || (log.details.provider as string)) && (
{(log.details.name as string) && (

Name: {log.details.name as string}

)} {(log.details.provider as string) && (

Provider:{" "} {log.details.provider as string}

)}
)}
))}
)} {/* Pagination */} {totalPages > 1 && (
Page {page} of {totalPages}
)}
); }