"use server" import { prisma } from "@/lib/prisma" import { auth } from "@/auth.vendor" import { redirect } from "next/navigation" export default async function StorefrontChatInboxPage() { const session = await auth() if (!session?.user?.id) { redirect("/login") } const p = prisma as any const stores = await prisma.store.findMany({ where: { userId: session.user.id, onboardingComplete: true }, select: { id: true, businessName: true, shopifyDomain: true, isActive: true } }) const store = stores.find((s) => s.isActive) ?? stores[0] if (!store) { redirect("/onboarding") } const conversations = await p.storefrontChatConversation.findMany({ where: { storeId: store.id }, orderBy: { updatedAt: "desc" }, take: 25, select: { id: true, visitorId: true, customerEmail: true, updatedAt: true } }) const conversationIds = conversations.map((c: { id: string }) => c.id) const messages = await p.storefrontChatMessage.findMany({ where: conversationIds.length ? { conversationId: { in: conversationIds } } : undefined, orderBy: { createdAt: "desc" }, take: 200, select: { id: true, content: true, createdAt: true, senderType: true, conversation: { select: { visitorId: true, customerEmail: true } } } }) const sorted = [...messages].sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime()) return (
{store.businessName} · {store.shopifyDomain}
{conversations.length} visitors
{sorted.length} messages
No storefront chat messages yet.
When customers send a message from the widget, it will show here.
Recent activity
Read-only
{m.senderType === "CUSTOMER" ? "CUSTOMER" : m.senderType} {" · "} {m.conversation?.visitorId || "visitor"}
{m.conversation?.customerEmail ? ({m.conversation.customerEmail}
) : null}{new Date(m.createdAt).toLocaleString()}