import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { auth } from "@/lib/auth"; import prisma from "@/lib/db"; import { headers } from "next/headers"; import Link from "next/link"; import BlockDeviceDialog from "../block-device-dialog"; import ClickableRow from "../clickable-row"; import DeviceCard from "../device-card"; import Pagination from "../pagination"; export async function AdminDevicesTable({ searchParams, parentalControl, }: { searchParams: Promise<{ query: string; page: number; sortBy: string; }>; parentalControl?: boolean; }) { const session = await auth.api.getSession({ headers: await headers() }) const isAdmin = session?.user.role === "ADMIN" 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", }, }, ], }, include: { User: true, payments: true, }, skip: offset, take: limit, orderBy: { name: `${sortBy}` as "asc" | "desc", }, }); return (
{devices.length === 0 ? (

No devices yet.

) : ( <>
Table of all devices. Device Name User MAC Address isActive blocked blockedBy expiryDate {devices.map((device) => (
{device.name} {device.isActive && ( Active until{" "} {new Date().toLocaleDateString("en-US", { month: "short", day: "2-digit", year: "numeric", })} )} {device.blocked && (
Comment:

blocked because he was watching youtube

)}
{device.User?.name} {device.mac} {device.isActive ? "Active" : "Inactive"} {device.blocked ? "Blocked" : "Not Blocked"} {device.blockedBy ? device.blockedBy : "Not Blocked"} {new Date().toLocaleDateString("en-US", { month: "short", day: "2-digit", year: "numeric", })}
))}
{query.length > 0 && (

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

)}
{totalDevices} devices
{devices.map((device) => ( ))}
)}
); }