Skip to content

Commit 796f4e1

Browse files
committed
Paginate the contacts list in the dashboard
The contacts page loaded only the first 10 contacts and had no page controls. Add the shared list pagination, reset to page 1 when the filter changes, clear ticked rows on page change, and step back when deleting empties the last page. Also document offset/rowsPerPage on GET /contacts and let the MCP list_contacts tool set itemsPerPage (max 50), like the other list tools.
1 parent 83ab52b commit 796f4e1

5 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎apps/api/openapi.json‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎apps/api/src/mcp/tools/contacts.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ export const contactTools = (): SendLitMcpTool[] => [
150150
.min(1)
151151
.optional()
152152
.describe("Page number, 1-indexed (default: 1)"),
153+
itemsPerPage: z
154+
.number()
155+
.int()
156+
.min(1)
157+
.max(50)
158+
.optional()
159+
.describe("Contacts per page (default: 10)"),
153160
}),
154161
outputSchema: contactListSchema,
155162
annotations: { idempotentHint: true, openWorldHint: false },
@@ -177,6 +184,7 @@ export const contactTools = (): SendLitMcpTool[] => [
177184
filter,
178185
excludeFilter: args.excludeFilter as any,
179186
offset: args.offset,
187+
rowsPerPage: args.itemsPerPage,
180188
}),
181189
countContacts(teamId, {
182190
searchText: args.q,

‎apps/web/app/(dashboard)/contacts/page.tsx‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { useSetBreadcrumb } from "@/components/dashboard/breadcrumb-context";
1717
import { DeleteConfirmationDialog } from "@/components/dashboard/delete-confirmation-dialog";
1818
import { DeleteContactsDialog } from "@/components/dashboard/delete-contacts-dialog";
1919
import { ImportContactsDialog } from "@/components/dashboard/import-contacts-dialog";
20+
import { ListPagination } from "@/components/dashboard/list-pagination";
2021
import { Loading } from "@/components/dashboard/loading";
2122
import { PageHeader } from "@/components/dashboard/page-header";
2223
import { ScrollablePage } from "@/components/dashboard/scrollable-page";
@@ -53,6 +54,7 @@ import {
5354
listContacts,
5455
} from "@/lib/api";
5556
import { ApiError } from "@/lib/api-client";
57+
import { ITEMS_PER_PAGE } from "@/lib/pagination";
5658
import { useSegments } from "@/lib/use-segments";
5759

5860
const emptyFilter: ContactFilterWithAggregator = {
@@ -63,6 +65,8 @@ const emptyFilter: ContactFilterWithAggregator = {
6365
export default function ContactsPage() {
6466
const [contacts, setContacts] = useState<Contact[] | null>(null);
6567
const [total, setTotal] = useState(0);
68+
const [page, setPage] = useState(1);
69+
const [loadingPage, setLoadingPage] = useState(false);
6670
const [filter, setFilter] = useState(emptyFilter);
6771
const [error, setError] = useState<string | null>(null);
6872
const [open, setOpen] = useState(false);
@@ -102,15 +106,29 @@ export default function ContactsPage() {
102106
}
103107
}
104108

105-
async function load(nextFilter: ContactFilterWithAggregator = filter) {
109+
async function load(
110+
nextFilter: ContactFilterWithAggregator = filter,
111+
pageToLoad = page,
112+
): Promise<void> {
113+
setLoadingPage(true);
106114
try {
107115
const { items, total } = await listContacts({
108116
filter: nextFilter.filters.length > 0 ? nextFilter : undefined,
117+
offset: pageToLoad,
118+
rowsPerPage: ITEMS_PER_PAGE,
109119
});
120+
// Deleting the last rows of the last page leaves it empty: step back.
121+
const lastPage = Math.max(1, Math.ceil(total / ITEMS_PER_PAGE));
122+
if (items.length === 0 && pageToLoad > lastPage) {
123+
return load(nextFilter, lastPage);
124+
}
110125
setContacts(items);
111126
setTotal(total);
127+
setPage(pageToLoad);
112128
} catch (err) {
113129
setError(err instanceof ApiError ? err.message : "Failed to load contacts");
130+
} finally {
131+
setLoadingPage(false);
114132
}
115133
}
116134

@@ -122,7 +140,7 @@ export default function ContactsPage() {
122140
// biome-ignore lint/correctness/useExhaustiveDependencies: load is recreated every render; reload only when the filter changes
123141
useEffect(() => {
124142
resetSelection();
125-
load(filter);
143+
load(filter, 1);
126144
}, [filter]);
127145

128146
// Polls a background deletion until it finishes, then reloads the list.
@@ -400,6 +418,19 @@ export default function ContactsPage() {
400418
)}
401419
</CardContent>
402420
</Card>
421+
422+
{contacts !== null && (
423+
<ListPagination
424+
page={page}
425+
total={total}
426+
itemsPerPage={ITEMS_PER_PAGE}
427+
disabled={loadingPage}
428+
onPageChange={(nextPage) => {
429+
resetSelection();
430+
void load(filter, nextPage);
431+
}}
432+
/>
433+
)}
403434
</ScrollablePage>
404435
);
405436
}

‎apps/web/lib/api.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ export function listContacts(
221221
filter?: ContactFilterWithAggregator;
222222
/** Contacts matching this filter are left out. */
223223
excludeFilter?: ContactFilterWithAggregator;
224+
/** Page number, 1-indexed. */
224225
offset?: number;
226+
rowsPerPage?: number;
225227
} = {},
226228
) {
227229
const filter = toApiContactFilter(params.filter);
@@ -234,6 +236,7 @@ export function listContacts(
234236
filter: filter ? JSON.stringify(filter) : undefined,
235237
excludeFilter: excludeFilter ? JSON.stringify(excludeFilter) : undefined,
236238
offset: params.offset,
239+
rowsPerPage: params.rowsPerPage,
237240
},
238241
}),
239242
);

‎packages/api-contract/src/schemas/contacts.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,18 @@ export const listContactsQuerySchema = z.object({
5656
.describe(
5757
"Serialized ContactFilterWithAggregator JSON; contacts matching it are left out",
5858
),
59-
offset: z.coerce.number().int().min(1).optional(),
60-
rowsPerPage: z.coerce.number().int().min(1).optional(),
59+
offset: z.coerce
60+
.number()
61+
.int()
62+
.min(1)
63+
.optional()
64+
.describe("Page number, 1-indexed (default: 1)"),
65+
rowsPerPage: z.coerce
66+
.number()
67+
.int()
68+
.min(1)
69+
.optional()
70+
.describe("Contacts per page (default: 10)"),
6171
});
6272

6373
export const contactDeliverySchema = z.object({

0 commit comments

Comments
 (0)