import Link from "next/link"; import { prisma } from "@/lib/db"; import { getConsoleContext, scopedPageIds } from "@/lib/console"; import { addDays, bkkDayKey, bkkMidnight, parseWeekParam, postBoardDate } from "@/lib/board"; import { EmptyState, Notice, PageTitle } from "@/components/ui"; import { Icon } from "@/components/Icon"; import { WeekBoard } from "@/components/WeekBoard"; import { WeekPlanAssistant } from "@/components/WeekPlanAssistant"; import { roleAtLeast } from "@/lib/auth/roles"; import { ContentAutoLauncher } from "@/components/ContentAutoLauncher"; export default async function OverviewPage(props: PageProps<"/">) { const ctx = await getConsoleContext(); if (ctx.pages.length === 0) return ; const sp = await props.searchParams; const week = parseWeekParam(typeof sp.w === "string" ? sp.w : undefined); const start = bkkMidnight(week); const end = bkkMidnight(addDays(week, 7)); const ids = scopedPageIds(ctx); const scope = { pageId: { in: ids } }; const [posts, tray, next, pending, failed] = await Promise.all([ prisma.post.findMany({ where: { ...scope, OR: [ { scheduledPublishAt: { gte: start, lt: end } }, { scheduledPublishAt: null, status: "PUBLISHED", updatedAt: { gte: start, lt: end } }, { scheduledPublishAt: null, status: { not: "PUBLISHED" }, plannedAt: { gte: start, lt: end } }, ] }, include: { page: { select: { name: true } } }, orderBy: [{ scheduledPublishAt: "asc" }, { plannedAt: "asc" }, { createdAt: "asc" }], }), prisma.post.findMany({ where: { ...scope, plannedAt: null, scheduledPublishAt: null, status: { notIn: ["PUBLISHED", "SCHEDULED"] } }, include: { page: { select: { name: true } } }, orderBy: { updatedAt: "desc" }, take: 50 }), prisma.post.findFirst({ where: { ...scope, status: "SCHEDULED", scheduledPublishAt: { gte: new Date() } }, orderBy: { scheduledPublishAt: "asc" }, select: { id: true } }), prisma.post.count({ where: { ...scope, status: "PENDING_APPROVAL" } }), prisma.post.count({ where: { ...scope, status: "FAILED" } }), ]); const editable = ctx.pages.filter(p => ids.includes(p.id) && roleAtLeast(p.role, "EDITOR")).map(p => p.id); const serialize = (p: (typeof posts)[number]) => ({ id: p.id, pageId: p.pageId, pageName: p.page.name, title: p.title, caption: p.caption, status: p.status, targets: p.targets, day: postBoardDate(p) ? bkkDayKey(postBoardDate(p)!) : null, scheduledAt: p.scheduledPublishAt?.toISOString() ?? null }); const unhealthy = ctx.pages.filter(p => ids.includes(p.id) && p.status !== "ACTIVE"); const today = bkkDayKey(new Date()); const openDays = Array.from({ length: 7 }, (_, i) => addDays(week, i)).filter(d => d >= today).length; const planFor = ctx.selected && editable.includes(ctx.selected.id) && openDays > 0 ? ctx.selected : null; return <> 0 ? <> editable.includes(page.id)).map(({ id, name }) => ({ id, name }))} initialPageId={ctx.selected?.id} /> สร้างโพสต์ : undefined} />
{posts.length} โพสต์ในสัปดาห์นี้{tray.length}{tray.length === 50 ? "+" : ""} โพสต์ยังไม่วางวัน {pending > 0 && รออนุมัติ {pending}} {failed > 0 && ต้องแก้ไข {failed}}
{unhealthy.length > 0 && {unhealthy.map(p => p.name).join(", ")} · ไปที่เพจที่เชื่อม} {planFor && } ; } const FIRST_RUN_STEPS = [ { title: "เชื่อมบัญชี Facebook", body: "ใช้บัญชีที่เป็นแอดมินของเพจ เพื่อเริ่มจัดการคอนเทนต์", icon: "link" as const }, { title: "เลือกเพจของคุณ", body: "รวมเพจที่ดูแลไว้ในที่เดียว สลับเพจได้ทุกเมื่อ", icon: "pages" as const }, { title: "วางแผนโพสต์แรก", body: "เขียน ส่งอนุมัติ แล้วเลือกเวลาให้ระบบเผยแพร่", icon: "calendar" as const }, ]; /** Shown until the first page is connected — the console is empty without one. */ function FirstRun({ name, superadmin }: { name: string; superadmin: boolean }) { return ( <> {superadmin ? <>
พื้นที่ทำงานของทุกเพจ

คอนเทนต์ที่ดี
เริ่มที่แผนที่ชัดเจน

รวมไอเดีย จัดคิวโพสต์ และตอบลูกค้าในที่เดียว
เชื่อมเพจของคุณ แล้วเริ่มวางแผนสัปดาห์แรก

เชื่อมเพจ Facebook รองรับ Facebook และ Instagram ที่ผูกกับเพจ
จากไอเดีย สู่โพสต์บนเพจ
{[ { icon: "post" as const, title: "เขียนให้พร้อม", hint: "เก็บไอเดียและรูปไว้ในฉบับร่าง", tone: "bg-blue-soft text-blue-ink" }, { icon: "check" as const, title: "ตรวจให้มั่นใจ", hint: "ส่งให้ทีมอนุมัติก่อนเผยแพร่", tone: "bg-yellow-soft text-yellow-ink" }, { icon: "send" as const, title: "โพสต์ให้ตรงเวลา", hint: "เลือกวัน แล้วให้ระบบดูแลคิว", tone: "bg-green-soft text-green-ink" }, ].map((s, i) =>

{s.title}

{s.hint}

0{i + 1}
)}
ทุกโพสต์ผ่านการอนุมัติก่อนเผยแพร่

เตรียมพื้นที่ทำงานของคุณ

เริ่มต้น 3 ขั้นตอน
    {FIRST_RUN_STEPS.map((s, i) =>
  1. ขั้นตอน {i + 1}

    {s.title}

    {s.body}

    {i === 0 && เริ่มเชื่อมต่อ }
  2. )}
:
} ); }