import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import prisma from "@/lib/db"; import AddDevicesToCartButton from "./add-devices-to-cart-button"; import Pagination from "./pagination"; export async function DevicesTable({ searchParams, }: { searchParams: Promise<{ query: string; page: number; sortBy: string; }>; }) { const query = (await searchParams)?.query || ""; const page = (await searchParams)?.page; const sortBy = (await searchParams)?.sortBy || "asc"; const totalDevices = await prisma.device.count({ where: { OR: [ { name: { contains: query || "", mode: "insensitive", }, }, { mac: { contains: query || "", mode: "insensitive", }, }, ], }, }); const totalPages = Math.ceil(totalDevices / 10); const limit = 10; const offset = (Number(page) - 1) * limit || 0; const devices = await prisma.device.findMany({ where: { OR: [ { name: { contains: query || "", mode: "insensitive", }, }, { mac: { contains: query || "", mode: "insensitive", }, }, ], }, skip: offset, take: limit, orderBy: { name: `${sortBy}` as "asc" | "desc", }, }); return (
{devices.length === 0 ? (

No devices yet.

) : ( <> Table of all devices. Device Name MAC Address Actions {devices.map((device) => ( {device.name} {device.mac} ))} {query.length > 0 && (

Showing {devices.length} locations for "{query} "

)}
{totalDevices} devices
)}
); }