import Link from "next/link"; import { redirect } from "next/navigation"; import { getSession } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; import { ShellPage } from "@/components/page-templates"; import { TablePlaceholder } from "@/components/placeholders"; import type { ReactNode } from "react"; type SearchRow = [string, string, string, ReactNode]; function toLocale(date: Date | null) { if (!date) { return "-"; } const now = new Date(); const diff = now.getTime() - date.getTime(); if (diff < 60_000) { return "just now"; } if (diff < 3_600_000) { return `${Math.floor(diff / 60_000)}m ago`; } if (diff < 86_400_000) { return `${Math.floor(diff / 3_600_000)}h ago`; } return new Intl.DateTimeFormat("id-ID", { day: "2-digit", month: "short", year: "numeric" }).format(date); } export default async function SearchPage({ searchParams }: { searchParams?: Promise<{ q?: string }>; }) { const params = await (searchParams ?? Promise.resolve({ q: "" })); const q = params.q?.trim() ?? ""; const session = await getSession(); if (!session) { redirect("/login"); } const tenantFilter = session.role === "super_admin" ? {} : { tenantId: session.tenantId }; const contactScope = q ? { OR: [{ fullName: { contains: q } }, { phoneNumber: { contains: q } }] } : {}; const userScope = q ? { OR: [{ fullName: { contains: q } }, { email: { contains: q } }] } : {}; const convoScope = q ? { OR: [{ contact: { fullName: { contains: q } } }, { subject: { contains: q } }] } : {}; const [contacts, users, conversations] = await Promise.all([ prisma.contact.findMany({ where: q ? { ...tenantFilter, ...contactScope } : tenantFilter, orderBy: { lastInteractionAt: "desc" }, take: q ? 5 : 0 }), prisma.user.findMany({ where: q ? { ...tenantFilter, ...userScope } : tenantFilter, orderBy: { fullName: "asc" }, take: q ? 5 : 0 }), prisma.conversation.findMany({ where: q ? { ...tenantFilter, ...convoScope } : tenantFilter, include: { contact: true }, orderBy: { lastMessageAt: "desc" }, take: q ? 5 : 0 }) ]); const rows: SearchRow[] = q ? [ ...contacts.map( (contact) => [ "Contact", contact.fullName, `Last seen: ${toLocale(contact.lastInteractionAt)}`, View ] as SearchRow ), ...users.map( (user) => [ "User", user.fullName, `Email: ${user.email}`, View ] as SearchRow ), ...conversations.map( (conversation) => [ "Conversation", conversation.contact.fullName, `Last message: ${toLocale(conversation.lastMessageAt)}`, Open ] as SearchRow ) ] : []; const infoText = q ? `${rows.length} hasil untuk "${q}"` : "Masukkan keyword di query ?q=... untuk mencari conversation, contact, atau user."; return (

{infoText}

); }