import React, { useCallback, useEffect, useState } from 'react'; import { Download, RefreshCw, Search, ShieldBan, ShieldCheck } from 'lucide-react'; import { downloadCsv, fetchLog, setBlocked } from './api'; import PurgeControl from './PurgeControl'; const RANGES = [ { value: '24h', label: 'Last 24 hours' }, { value: '7d', label: 'Last 7 days' }, { value: '30d', label: 'Last 30 days' }, { value: 'all', label: 'All time' }, ]; /** * One generic, paginated log table. * * The three monitoring tabs were previously three ~200-line copies of the same * fetch/search/paginate code. They now share this component and differ only in * their column definitions. */ export default function LogTable({ title, endpoint, exportType, columns, rowKey = (row) => row.id, emptyMessage = 'Nothing recorded yet.', emptyHint, // Renders a Block/Release control per row when the row exposes an IP. action = null, // 'block' | 'release' | null onChanged, }) { const [rows, setRows] = useState([]); const [total, setTotal] = useState(0); const [page, setPage] = useState(1); const [perPage, setPerPage] = useState(10); const [search, setSearch] = useState(''); const [range, setRange] = useState('all'); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); const [busyIp, setBusyIp] = useState(null); const load = useCallback(async () => { setLoading(true); setError(null); try { const result = await fetchLog(endpoint, { page, limit: perPage, search, range, }); setRows(result.rows); setTotal(result.total); } catch (err) { setError(err.message); } finally { setLoading(false); } }, [endpoint, page, perPage, search, range]); // Debounce typing so each keystroke is not a request. useEffect(() => { const timer = setTimeout(load, search ? 350 : 0); return () => clearTimeout(timer); }, [load, search]); const totalPages = Math.max(1, Math.ceil(total / perPage)); const runAction = async (ip, blocked) => { setBusyIp(ip); setNotice(null); try { const result = await setBlocked(ip, blocked); setNotice({ type: 'success', text: result.message }); await load(); if (onChanged) onChanged(); } catch (err) { setNotice({ type: 'error', text: err.message }); } finally { setBusyIp(null); } }; const runExport = async () => { setNotice(null); try { const count = await downloadCsv(exportType); setNotice({ type: 'success', text: `Exported ${count} rows.` }); } catch (err) { setNotice({ type: 'error', text: err.message }); } }; return (

{title} {total > 0 && {total}}

{notice && (
{notice.text}
)} {error ? (
{error}
) : ( <>
{columns.map((column) => ( ))} {action && } {loading && rows.length === 0 ? ( ) : rows.length === 0 ? ( ) : ( rows.map((row) => ( {columns.map((column) => ( ))} {action && ( )} )) )}
{column.label} Action
Loading…
{emptyMessage} {emptyHint && {emptyHint}}
{column.render ? column.render(row) : row[column.key]} {row.ip_address ? ( ) : ( — )}
{ setPage(1); load(); if (onChanged) onChanged(); }} /> {total > perPage && (
Page {page} of {totalPages}
)} )}
); }