import { auth } from '@/auth' import { db } from '@/lib/db' import { machines, groups } from '@/lib/db/schema' import { eq, desc, or, ilike, sql } from 'drizzle-orm' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import Link from 'next/link' import { Download, Laptop } from 'lucide-react' import { MachineCard } from '@/components/dashboard/machine-card' import { MachinesFilter } from '@/components/dashboard/machines-filter' interface PageProps { searchParams: Promise<{ q?: string; tag?: string; group?: string }> } export default async function MachinesPage({ searchParams }: PageProps) { const session = await auth() const { q, tag, group } = await searchParams // Build query conditions const conditions = [eq(machines.userId, session!.user.id)] if (group) conditions.push(eq(machines.groupId, group)) let machineList = await db .select() .from(machines) .where(conditions.length === 1 ? conditions[0] : sql`${conditions.reduce((a, b) => sql`${a} AND ${b}`)}`) .orderBy(desc(machines.isOnline), desc(machines.lastSeen)) // Text search (filter in JS — avoids complex SQL for small datasets) if (q) { const lower = q.toLowerCase() machineList = machineList.filter(m => m.name.toLowerCase().includes(lower) || (m.hostname ?? '').toLowerCase().includes(lower) || (m.os ?? '').toLowerCase().includes(lower) || (m.ipAddress ?? '').toLowerCase().includes(lower) ) } // Tag filter if (tag) { machineList = machineList.filter(m => m.tags?.includes(tag)) } // Collect all unique tags across machines const allTagsSet = new Set() machineList.forEach(m => (m.tags ?? []).forEach(t => allTagsSet.add(t))) const allTags = [...allTagsSet].sort() const onlineCount = machineList.filter(m => m.isOnline).length // Fetch groups for group picker and machine card selects const groupList = await db.select({ id: groups.id, name: groups.name }).from(groups).orderBy(groups.name) return (

Machines

{machineList.length > 0 ? (
{machineList.map((machine) => ( ))}
) : (

{q || tag || group ? 'No machines match your filter' : 'No machines yet'}

{q || tag || group ? 'Try adjusting your search or filters.' : 'Download and install the RemoteLink agent on the machines you want to control remotely.'}

{!(q || tag || group) && ( )}
)}
) }