A binary mobile-api call with the same error shape, export proxy routes that pass Content-Type and Content-Disposition through, and an Export button under BO_LEADS_EXPORT that downloads with the current search or filters and shows EXPORT_TOO_LARGE on the page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
131 lines
5.2 KiB
TypeScript
131 lines
5.2 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import Link from "next/link";
|
|
import { Download } from "lucide-react";
|
|
import type { ColumnDef } from "@tanstack/react-table";
|
|
import { FilterField, FilterPanel, FilterSearch } from "@/app/components/ui/filter-panel";
|
|
import { DataTable } from "@/app/components/ui/data-table";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { PageHeader } from "@/app/components/ui/page-header";
|
|
import { EmptyState, ErrorState } from "@/app/components/ui/states";
|
|
import { RequireModule } from "@/app/lib/RequireModule";
|
|
import { useAdminAuth } from "@/app/lib/AdminAuthContext";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
import { useListFilters } from "@/app/lib/hooks/useListFilters";
|
|
import { useFitPageSize, MIN_PAGE_SIZE } from "@/app/lib/hooks/useFitPageSize";
|
|
import { exportLeads, listLeads } from "@/app/lib/services/adminLeads";
|
|
import type { LeadRow } from "@/app/lib/types";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
const DEFAULTS = { q: "" };
|
|
|
|
function LeadsPageContent() {
|
|
const t = useT();
|
|
const { hasModule } = useAdminAuth();
|
|
const { tableRef, pageSize } = useFitPageSize();
|
|
const { filters, page, set, setPage, clear, activeCount } = useListFilters(DEFAULTS);
|
|
const [data, setData] = useState<{ items: LeadRow[]; total: number }>({ items: [], total: 0 });
|
|
const [loading, setLoading] = useState(true);
|
|
const [failed, setFailed] = useState(false);
|
|
const [reloadKey, setReloadKey] = useState(0);
|
|
|
|
const [exporting, setExporting] = useState(false);
|
|
const [exportError, setExportError] = useState<string | null>(null);
|
|
|
|
const runExport = () => {
|
|
setExporting(true);
|
|
setExportError(null);
|
|
exportLeads({ q: filters.q || undefined })
|
|
.catch((err: unknown) => {
|
|
const tooLarge = err instanceof ApiError && (err.data as { code?: string } | undefined)?.code === "EXPORT_TOO_LARGE";
|
|
setExportError(tooLarge ? t("LeadsExportTooLarge") : err instanceof Error ? err.message : t("LeadListLoadFailed"));
|
|
})
|
|
.finally(() => setExporting(false));
|
|
};
|
|
|
|
const refetch = useCallback(() => setReloadKey((k) => k + 1), []);
|
|
|
|
useEffect(() => {
|
|
if (pageSize == null) return;
|
|
let cancelled = false;
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- resets loading/error before the filter/page/reloadKey-triggered fetch below, which is the actual synchronization.
|
|
setLoading(true);
|
|
setFailed(false);
|
|
setExportError(null);
|
|
listLeads({ q: filters.q || undefined, page, pageSize })
|
|
.then((result) => { if (!cancelled) setData(result); })
|
|
.catch(() => { if (!cancelled) setFailed(true); })
|
|
.finally(() => { if (!cancelled) setLoading(false); });
|
|
return () => { cancelled = true; };
|
|
}, [filters, page, reloadKey, pageSize]);
|
|
|
|
const columns = useMemo<ColumnDef<LeadRow, unknown>[]>(
|
|
() => [
|
|
{
|
|
accessorKey: "name",
|
|
header: t("Name"),
|
|
enableSorting: false,
|
|
cell: ({ row }) => (
|
|
<Link href={`/leads/${row.original.leadUuid}`} className="font-medium text-primary underline-offset-4 hover:underline focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none">
|
|
{row.original.name}
|
|
</Link>
|
|
),
|
|
},
|
|
{ accessorKey: "mobile", header: t("LeadMobile"), enableSorting: false },
|
|
{ accessorKey: "email", header: t("Email"), enableSorting: false },
|
|
{ accessorKey: "companyName", header: t("LeadCompany"), enableSorting: false },
|
|
{ accessorKey: "eventCount", header: t("LeadsEvents"), enableSorting: false },
|
|
{ id: "lastEvent", header: t("LeadsLastEvent"), enableSorting: false, cell: ({ row }) => row.original.lastEvent?.name ?? "" },
|
|
{ id: "latestSalesperson", header: t("LeadsLatestSalesperson"), enableSorting: false, cell: ({ row }) => row.original.latestSalesperson?.name ?? "" },
|
|
],
|
|
[t],
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<PageHeader
|
|
title={t("BO_LEADS")}
|
|
actions={hasModule("BO_LEADS_EXPORT") && (
|
|
<Button variant="outline" disabled={exporting} onClick={runExport}>
|
|
<Download aria-hidden="true" className="size-4" />
|
|
{t("Export")}
|
|
</Button>
|
|
)}
|
|
/>
|
|
|
|
{exportError && <ErrorState message={exportError} className="mb-4" />}
|
|
|
|
{failed && <ErrorState message={t("LeadListLoadFailed")} onRetry={refetch} className="mb-4" />}
|
|
|
|
<FilterPanel activeCount={activeCount} onClear={clear}>
|
|
<FilterField label={t("LeadsSearch")}>
|
|
<FilterSearch className="w-full" aria-label={t("LeadsSearch")} placeholder={t("LeadsSearch")} value={filters.q} onChange={(q) => set({ q })} />
|
|
</FilterField>
|
|
</FilterPanel>
|
|
|
|
<DataTable
|
|
label={t("BO_LEADS")}
|
|
columns={columns}
|
|
data={data.items}
|
|
total={data.total}
|
|
page={page}
|
|
pageSize={pageSize ?? MIN_PAGE_SIZE}
|
|
tableRef={tableRef}
|
|
onPageChange={setPage}
|
|
isLoading={loading}
|
|
rowKey={(l) => l.leadUuid}
|
|
empty={<EmptyState title={activeCount > 0 ? t("LeadNoMatch") : t("LeadNoLeads")} />}
|
|
/>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default function LeadsPage() {
|
|
return (
|
|
<RequireModule pathname="/leads">
|
|
<LeadsPageContent />
|
|
</RequireModule>
|
|
);
|
|
}
|