"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { Card, Chip, Stack, TextField, Typography } from "@mui/material"; import SearchIcon from "@mui/icons-material/Search"; import { DataTable } from "@/src/components/ui/DataTable"; type EventRow = { id: number; created_at: string; user: string; summary: string; }; type Props = { events: EventRow[]; pagination: { total: number; page: number; perPage: number }; initialSearch: string; }; export default function AuditLogClient({ events, pagination, initialSearch }: Props) { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); const [searchTerm, setSearchTerm] = useState(initialSearch); useEffect(() => { setSearchTerm(initialSearch); }, [initialSearch]); const debounceRef = useRef | null>(null); const updateSearch = useCallback( (value: string) => { if (debounceRef.current) clearTimeout(debounceRef.current); debounceRef.current = setTimeout(() => { const params = new URLSearchParams(searchParams.toString()); if (value.trim()) { params.set("search", value.trim()); } else { params.delete("search"); } params.delete("page"); // reset to page 1 on new search router.push(`${pathname}?${params.toString()}`); }, 400); }, [router, pathname, searchParams] ); useEffect(() => { return () => { if (debounceRef.current) clearTimeout(debounceRef.current); }; }, []); const columns = [ { id: "created_at", label: "Time", width: 180, render: (r: EventRow) => ( {new Date(r.created_at).toLocaleString()} ), }, { id: "user", label: "User", width: 160, render: (r: EventRow) => ( ), }, { id: "summary", label: "Event", render: (r: EventRow) => ( {r.summary} ), }, ]; const mobileCard = (r: EventRow) => ( {new Date(r.created_at).toLocaleString()} {r.summary} ); return ( Audit Log Review configuration changes and user activity. { setSearchTerm(e.target.value); updateSearch(e.target.value); }} slotProps={{ input: { startAdornment: , }, }} size="small" sx={{ maxWidth: 400 }} /> ); }