Files
ARTEX-chixuweihu/web/src/components/table-pagination.tsx
T
ji ruo ea49ba0a3a fix(intercept): avoid full-history polling and full-table counts
- Add partial covering index on intercept_pending(task_id, created_at DESC) INCLUDE (agent_name) WHERE status = 'pending'.
- Add GET /api/intercept/pending/summary (global count or per-task count + agent_names) and ?task_id= filtering on GET /api/intercept/pending.
- Cap unpaginated GET /api/intercept/task/{taskID} at 200 rows and remove unused tasks JOIN from non-search approval queries.
- Replace unbounded SELECT COUNT(*) in paginated approval queries with end-of-list shortcut, pg_class/pg_stat catalog estimate for large unfiltered tables, and bounded counting (10,000 cap) for large filtered sets.
- Deduplicate task detail and sessions-tab polling via single pending summary poll, switch sidebar badge to summary count, and guard approval polling against background tabs, overlapping requests, and deep-page/search churn.

Fixes #186
2026-11-26 00:54:26 +08:00

136 lines
4.6 KiB
TypeScript

"use client";
import { useEffect, useRef } from "react";
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/button";
import { Pagination, PaginationContent, PaginationEllipsis, PaginationItem } from "@/components/ui/pagination";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
interface TablePaginationProps {
page: number; // 1-based
pageSize: number;
total: number; // total (after filtering)
totalApproximate?: boolean;
totalCapped?: boolean;
hasMore?: boolean;
onPageChange: (p: number) => void;
onPageSizeChange: (s: number) => void;
pageSizeOptions?: number[];
}
// pageWindows returns the sequence of page numbers / ellipsis to render.
function pageWindows(page: number, total: number): (number | "...")[] {
if (total <= 7) return Array.from({ length: total }, (_, i) => i + 1);
const out: (number | "...")[] = [1];
if (page > 3) out.push("...");
const lo = Math.max(2, page - 1);
const hi = Math.min(total - 1, page + 1);
for (let i = lo; i <= hi; i++) out.push(i);
if (page < total - 2) out.push("...");
out.push(total);
return out;
}
export function TablePagination({
page,
pageSize,
total,
totalApproximate = false,
totalCapped = false,
hasMore = false,
onPageChange,
onPageSizeChange,
pageSizeOptions = [10, 20, 50],
}: TablePaginationProps) {
const { t } = useTranslation();
const totalPages = Math.max(1, Math.ceil(total / pageSize), hasMore ? page + 1 : 0);
const safePage = Math.min(Math.max(1, page), totalPages);
const hasItems = total > 0 || hasMore;
const from = hasItems ? (safePage - 1) * pageSize + 1 : 0;
const to = hasMore ? safePage * pageSize : Math.min(safePage * pageSize, total);
let totalLabel: string | number = total;
if (totalCapped) totalLabel = `${total}+`;
else if (totalApproximate) totalLabel = `~${total}`;
const onPageChangeRef = useRef(onPageChange);
onPageChangeRef.current = onPageChange;
useEffect(() => {
if (page !== safePage) onPageChangeRef.current(safePage);
}, [page, safePage]);
return (
<div className="flex flex-wrap items-center justify-between gap-2 border-t px-4 py-2 lg:px-6">
<div className="text-muted-foreground flex items-center gap-2 text-xs">
<Select value={String(pageSize)} onValueChange={(v) => onPageSizeChange(Number(v))}>
<SelectTrigger size="sm" className="h-7 w-16">
<SelectValue />
</SelectTrigger>
<SelectContent>
{pageSizeOptions.map((s) => (
<SelectItem key={s} value={String(s)}>
{s}
</SelectItem>
))}
</SelectContent>
</Select>
<span>{t("pagination.perPage")}</span>
{hasItems ? (
<span className="tabular-nums">{t("pagination.range", { from, to, total: totalLabel, count: total })}</span>
) : (
<span>{t("pagination.empty")}</span>
)}
</div>
{totalPages > 1 && (
<Pagination className="mx-0 w-auto justify-end">
<PaginationContent>
<PaginationItem>
<Button
variant="ghost"
size="icon-sm"
disabled={safePage === 1}
onClick={() => onPageChange(safePage - 1)}
aria-label={t("common.previous")}
>
<ChevronLeftIcon className="size-4" />
</Button>
</PaginationItem>
{pageWindows(safePage, totalPages).map((p, i) =>
p === "..." ? (
<PaginationItem key={`el-${i}`}>
<PaginationEllipsis />
</PaginationItem>
) : (
<PaginationItem key={p}>
<Button
variant={p === safePage ? "outline" : "ghost"}
size="icon-sm"
onClick={() => onPageChange(p)}
aria-current={p === safePage ? "page" : undefined}
>
{p}
</Button>
</PaginationItem>
),
)}
<PaginationItem>
<Button
variant="ghost"
size="icon-sm"
disabled={safePage === totalPages}
onClick={() => onPageChange(safePage + 1)}
aria-label={t("common.next")}
>
<ChevronRightIcon className="size-4" />
</Button>
</PaginationItem>
</PaginationContent>
</Pagination>
)}
</div>
);
}