A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.
"use client";
import { useCallback } from "react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import type { PaginationProps } from "@mantine/core";
import { Box, Group, Pagination } from "@mantine/core";
import { useI18n } from "@homarr/translation/client";
import { Link } from "@homarr/ui";
interface TablePaginationProps {
total: number;
}
export const TablePagination = ({ total }: TablePaginationProps) => {
// eslint-disable-next-line @typescript-eslint/unbound-method
const { replace } = useRouter();
const pathName = usePathname();
const searchParams = useSearchParams();
const t = useI18n("common.pagination");
const current = parsePage(searchParams.getAll("page"));
const getItemProps = useCallback(
(page: number) => {
const params = new URLSearchParams(searchParams);
params.set("page", page.toString());
return {
component: Link,
href: `?${params.toString()}`,
};
},
[searchParams],
);
const getControlProps = useCallback(
(control: ControlType) => {
return getItemProps(calculatePageFor(control, current, total));
},
[current, getItemProps, total],
);
const handleChange = useCallback(
(page: number) => {
const params = new URLSearchParams(searchParams);
params.set("page", page.toString());
replace(`${pathName}?${params.toString()}`);
},
[pathName, replace, searchParams],
);
if (total <= 0) return null;
return (
<Pagination.Root
total={total}
value={current}
layout="responsive"
w="100%"
getItemProps={getItemProps}
onChange={handleChange}
>
<Group gap={8} justify="end" wrap="nowrap">
<Pagination.Previous {...getControlProps("previous")} />
<Box className={Pagination.classes.items}>
<Pagination.Items />
</Box>
<Pagination.Label formatLabel={({ page, totalPages }) => t("pageOf", { page, totalPages })} />
<Pagination.Next {...getControlProps("next")} />
</Group>
</Pagination.Root>
);
};
const parsePage = (pageValues: string[]) => {
if (pageValues.length !== 1) return 1;
const [pageValue] = pageValues;
if (!pageValue || !/^[1-9]\d*$/u.test(pageValue)) return 1;
const page = Number(pageValue);
if (!Number.isSafeInteger(page)) return 1;
return page;
};
type ControlType = Parameters<Exclude<PaginationProps["getControlProps"], undefined>>[0];
const calculatePageFor = (type: ControlType, current: number, total: number) => {
switch (type) {
case "first":
return 1;
case "previous":
return Math.max(current - 1, 1);
case "next":
return current + 1;
case "last":
return total;
default:
console.error(`Unknown pagination control type: ${type as string}`);
return 1;
}
};