2024-11-30 23:38:32 +05:00
|
|
|
import {
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCaption,
|
|
|
|
TableCell,
|
|
|
|
TableFooter,
|
|
|
|
TableHead,
|
|
|
|
TableHeader,
|
|
|
|
TableRow,
|
|
|
|
} from "@/components/ui/table";
|
2024-12-22 21:34:57 +05:00
|
|
|
import { auth } from "@/lib/auth";
|
2024-11-30 23:38:32 +05:00
|
|
|
import prisma from "@/lib/db";
|
2024-12-22 21:34:57 +05:00
|
|
|
import { headers } from "next/headers";
|
2024-12-26 00:43:39 +05:00
|
|
|
import ClickableRow from "./clickable-row";
|
2024-12-25 17:21:04 +05:00
|
|
|
import DeviceCard from "./device-card";
|
2024-11-30 23:38:32 +05:00
|
|
|
import Pagination from "./pagination";
|
|
|
|
|
|
|
|
export async function DevicesTable({
|
|
|
|
searchParams,
|
2025-01-01 23:48:56 +05:00
|
|
|
parentalControl,
|
2024-11-30 23:38:32 +05:00
|
|
|
}: {
|
|
|
|
searchParams: Promise<{
|
|
|
|
query: string;
|
|
|
|
page: number;
|
|
|
|
sortBy: string;
|
|
|
|
}>;
|
2024-12-22 21:34:57 +05:00
|
|
|
parentalControl?: boolean;
|
2024-11-30 23:38:32 +05:00
|
|
|
}) {
|
2024-12-22 21:34:57 +05:00
|
|
|
const session = await auth.api.getSession({
|
|
|
|
headers: await headers()
|
|
|
|
})
|
2025-01-01 23:48:56 +05:00
|
|
|
const isAdmin = session?.user.role === "ADMIN"
|
2024-11-30 23:38:32 +05:00
|
|
|
const query = (await searchParams)?.query || "";
|
|
|
|
const page = (await searchParams)?.page;
|
|
|
|
const sortBy = (await searchParams)?.sortBy || "asc";
|
|
|
|
const totalDevices = await prisma.device.count({
|
|
|
|
where: {
|
2025-01-01 23:48:56 +05:00
|
|
|
userId: isAdmin ? undefined : session?.session.userId,
|
2024-11-30 23:38:32 +05:00
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
name: {
|
|
|
|
contains: query || "",
|
|
|
|
mode: "insensitive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mac: {
|
|
|
|
contains: query || "",
|
|
|
|
mode: "insensitive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2024-12-07 14:09:53 +05:00
|
|
|
NOT: {
|
2025-01-08 23:04:30 +05:00
|
|
|
payments: {
|
|
|
|
some: {
|
|
|
|
paid: false
|
|
|
|
}
|
2024-12-07 14:09:53 +05:00
|
|
|
}
|
|
|
|
},
|
2025-01-01 23:48:56 +05:00
|
|
|
isActive: isAdmin ? undefined : parentalControl,
|
|
|
|
blocked: isAdmin ? undefined : parentalControl !== undefined ? undefined : false,
|
2024-11-30 23:38:32 +05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const totalPages = Math.ceil(totalDevices / 10);
|
|
|
|
const limit = 10;
|
|
|
|
const offset = (Number(page) - 1) * limit || 0;
|
|
|
|
|
|
|
|
const devices = await prisma.device.findMany({
|
|
|
|
where: {
|
2025-01-06 12:49:13 +05:00
|
|
|
userId: session?.session.userId,
|
2024-11-30 23:38:32 +05:00
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
name: {
|
|
|
|
contains: query || "",
|
|
|
|
mode: "insensitive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mac: {
|
|
|
|
contains: query || "",
|
|
|
|
mode: "insensitive",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2024-12-07 14:09:53 +05:00
|
|
|
NOT: {
|
2025-01-08 23:04:30 +05:00
|
|
|
payments: {
|
|
|
|
some: {
|
|
|
|
paid: false
|
|
|
|
}
|
2025-01-06 12:49:13 +05:00
|
|
|
},
|
2024-12-07 14:09:53 +05:00
|
|
|
},
|
2025-01-06 12:49:13 +05:00
|
|
|
isActive: parentalControl,
|
|
|
|
blocked: parentalControl !== undefined ? undefined : false,
|
2024-11-30 23:38:32 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
skip: offset,
|
|
|
|
take: limit,
|
|
|
|
orderBy: {
|
|
|
|
name: `${sortBy}` as "asc" | "desc",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{devices.length === 0 ? (
|
|
|
|
<div className="h-[calc(100svh-400px)] flex flex-col items-center justify-center my-4">
|
|
|
|
<h3>No devices yet.</h3>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<>
|
2024-12-25 17:21:04 +05:00
|
|
|
<div className="hidden sm:block">
|
|
|
|
<Table className="overflow-scroll">
|
|
|
|
<TableCaption>Table of all devices.</TableCaption>
|
|
|
|
<TableHeader>
|
|
|
|
<TableRow>
|
|
|
|
<TableHead>Device Name</TableHead>
|
|
|
|
<TableHead>MAC Address</TableHead>
|
2024-12-26 00:43:39 +05:00
|
|
|
<TableHead>#</TableHead>
|
2024-12-25 17:21:04 +05:00
|
|
|
</TableRow>
|
|
|
|
</TableHeader>
|
|
|
|
<TableBody className="overflow-scroll">
|
|
|
|
{devices.map((device) => (
|
2024-12-26 00:43:39 +05:00
|
|
|
// <TableRow key={device.id}>
|
|
|
|
// <TableCell>
|
|
|
|
// <div className="flex flex-col items-start">
|
|
|
|
// <Link
|
|
|
|
// className="font-medium hover:underline"
|
|
|
|
// href={`/devices/${device.id}`}
|
|
|
|
// >
|
|
|
|
// {device.name}
|
|
|
|
// </Link>
|
|
|
|
// <span className="text-muted-foreground">
|
|
|
|
// Active until{" "}
|
|
|
|
// {new Date().toLocaleDateString("en-US", {
|
|
|
|
// month: "short",
|
|
|
|
// day: "2-digit",
|
|
|
|
// year: "numeric",
|
|
|
|
// })}
|
|
|
|
// </span>
|
|
|
|
// {parentalControl && (
|
|
|
|
// <div className="p-2 rounded border my-2">
|
|
|
|
// <span>Comment: </span>
|
|
|
|
// <p className="text-neutral-500">
|
|
|
|
// blocked because he was watching youtube
|
|
|
|
// </p>
|
|
|
|
// </div>
|
|
|
|
// )}
|
2024-12-25 17:21:04 +05:00
|
|
|
|
2024-12-26 00:43:39 +05:00
|
|
|
// </div>
|
|
|
|
// </TableCell>
|
|
|
|
// <TableCell className="font-medium">{device.mac}</TableCell>
|
|
|
|
// <TableCell>
|
|
|
|
// {!parentalControl ? (
|
|
|
|
// <AddDevicesToCartButton device={device} />
|
|
|
|
// ) : (
|
|
|
|
// <BlockDeviceButton device={device} />
|
|
|
|
// )}
|
|
|
|
// </TableCell>
|
|
|
|
// </TableRow>
|
2025-01-01 23:48:56 +05:00
|
|
|
<ClickableRow admin={isAdmin} key={device.id} device={device} parentalControl={parentalControl} />
|
2024-12-25 17:21:04 +05:00
|
|
|
))}
|
|
|
|
</TableBody>
|
|
|
|
<TableFooter>
|
|
|
|
<TableRow>
|
|
|
|
<TableCell colSpan={2}>
|
|
|
|
{query.length > 0 && (
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
Showing {devices.length} locations for "{query}
|
|
|
|
"
|
|
|
|
</p>
|
2024-12-22 21:34:57 +05:00
|
|
|
)}
|
2024-11-30 23:38:32 +05:00
|
|
|
</TableCell>
|
2024-12-25 17:21:04 +05:00
|
|
|
<TableCell className="text-muted-foreground">
|
|
|
|
{totalDevices} devices
|
|
|
|
</TableCell>
|
2024-11-30 23:38:32 +05:00
|
|
|
</TableRow>
|
2024-12-25 17:21:04 +05:00
|
|
|
</TableFooter>
|
|
|
|
</Table>
|
|
|
|
<Pagination totalPages={totalPages} currentPage={page} />
|
|
|
|
</div>
|
|
|
|
<div className="sm:hidden my-4">
|
|
|
|
{devices.map((device) => (
|
|
|
|
<DeviceCard parentalControl={parentalControl} key={device.id} device={device} />
|
|
|
|
))}
|
|
|
|
</div>
|
2024-11-30 23:38:32 +05:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|