import { HandCoins } from "lucide-react"; import Link from "next/link"; import { redirect } from "next/navigation"; import { getServerSession } from "next-auth"; import { authOptions } from "@/app/auth"; import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { cn } from "@/lib/utils"; import { getDevices } from "@/queries/devices"; import { tryCatch } from "@/utils/tryCatch"; import AddDevicesToCartButton from "../add-devices-to-cart-button"; import BlockDeviceDialog from "../block-device-dialog"; import ClientErrorMessage from "../client-error-message"; import DeviceCard from "../device-card"; import Pagination from "../pagination"; export async function AdminDevicesTable({ 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, true), ); 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 User MAC Address Vendor # {data?.map((device) => (
{device.name} {device.is_active ? (
Active until{" "} {new Date(device.expiry_date || "").toLocaleDateString( "en-US", { month: "short", day: "2-digit", year: "numeric", }, )}
) : (

Device Inactive

)} {device.has_a_pending_payment && ( Payment Pending{" "} )} {device.blocked_by === "ADMIN" && device.blocked && (
Comment

{device?.reason_for_blocking}

)}
{device.user} {device.mac} {device?.vendor}
))}
{meta?.total === 1 ? (

Total {meta?.total} device.

) : (

Total {meta?.total} devices.

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