import { authOptions } from "@/app/auth"; import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { getDevices } from "@/queries/devices"; import { tryCatch } from "@/utils/tryCatch"; import { getServerSession } from "next-auth"; import { redirect } from "next/navigation"; import ClickableRow from "./clickable-row"; import ClientErrorMessage from "./client-error-message"; import DeviceCard from "./device-card"; import Pagination from "./pagination"; export async function DevicesTable({ searchParams, parentalControl, }: { searchParams: Promise<{ [key: string]: unknown; }>; parentalControl?: boolean; }) { const resolvedParams = await searchParams; const session = await getServerSession(authOptions); const isAdmin = session?.user?.is_superuser; const page = Number.parseInt(resolvedParams.page as string) || 1; const limit = 10; const offset = (page - 1) * limit; // Build params object for getDevices const apiParams: Record = {}; for (const [key, value] of Object.entries(resolvedParams)) { if (value !== undefined && value !== "") { apiParams[key] = typeof value === "number" ? value : String(value); } } apiParams.limit = limit; apiParams.offset = offset; const [error, devices] = await tryCatch( getDevices(apiParams), ); if (error) { if (error.message === "UNAUTHORIZED") { redirect("/auth/signin"); } else { return ; } } const { meta, data } = devices; return (
{data?.length === 0 ? (

No devices.

) : ( <>
Table of all devices. Device Name MAC Address Vendor # {data?.map((device) => ( ))} {meta?.total === 1 ? (

Total {meta?.total} device.

) : (

Total {meta?.total} devices.

)}
{data?.map((device) => ( ))}
) }
); }