Initial commit: page-manager Next.js app with Coolify deploy support
Replace Vercel-specific infra (Cron, Blob) with self-hosted equivalents for deployment to Coolify: in-container cron scheduler (scripts/cron-runner.mjs) and disk-based upload storage served via /api/uploads/[...path]. Add Dockerfile, .dockerignore, and /api/health for container health checks. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
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";
|
||||
|
||||
export default async function OverviewPage(props: PageProps<"/">) {
|
||||
const ctx = await getConsoleContext();
|
||||
if (ctx.pages.length === 0) return <FirstRun name={ctx.user.name} superadmin={ctx.user.globalRole === "SUPERADMIN"} />;
|
||||
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 <>
|
||||
<PageTitle title="แผนคอนเทนต์" subtitle={ctx.selected ? `จัดคิวโพสต์ของ ${ctx.selected.name}` : "ทุกไอเดีย ทุกเพจ ในแผนเดียว · เวลาไทย"} actions={editable.length > 0 ? <Link href="/posts/new" className="btn btn-primary"><Icon name="plus" /> สร้างโพสต์</Link> : undefined} />
|
||||
<div className="flex flex-wrap items-center gap-3 text-[12px] text-ink-3">
|
||||
<span>{posts.length} โพสต์ในสัปดาห์นี้</span><span className="w-1 h-1 bg-line rounded-full" /><span>{tray.length}{tray.length === 50 ? "+" : ""} โพสต์ยังไม่วางวัน</span>
|
||||
{pending > 0 && <Link href="/posts?status=PENDING_APPROVAL" className="pill pill-amber">รออนุมัติ {pending}</Link>}
|
||||
{failed > 0 && <Link href="/posts?status=FAILED" className="pill pill-red">ต้องแก้ไข {failed}</Link>}
|
||||
</div>
|
||||
{unhealthy.length > 0 && <Notice tone="red" title="เชื่อม Facebook ใหม่เพื่อให้เพจทำงานต่อ">{unhealthy.map(p => p.name).join(", ")} · <Link href="/pages" className="underline">ไปที่เพจที่เชื่อม</Link></Notice>}
|
||||
{planFor && <WeekPlanAssistant key={`${planFor.id}-${week}`} pageId={planFor.id} pageName={planFor.name} week={week} openDays={openDays} />}
|
||||
<WeekBoard week={week} today={today} posts={posts.map(serialize)} tray={tray.map(serialize)} editablePageIds={editable} nextId={next?.id ?? null} />
|
||||
</>;
|
||||
}
|
||||
|
||||
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 (
|
||||
<>
|
||||
<PageTitle title={`สวัสดี ${name}`} subtitle="เริ่มวันทำงานของเพจคุณได้ที่นี่" />
|
||||
{superadmin ? <>
|
||||
<section className="welcome-panel">
|
||||
<div className="welcome-copy">
|
||||
<span className="welcome-eyebrow"><span className="w-1.5 h-1.5 rounded-full bg-blue" /> พื้นที่ทำงานของทุกเพจ</span>
|
||||
<h2>คอนเทนต์ที่ดี<br />เริ่มที่<span className="text-blue">แผนที่ชัดเจน</span></h2>
|
||||
<p>รวมไอเดีย จัดคิวโพสต์ และตอบลูกค้าในที่เดียว<br className="hidden sm:block" /> เชื่อมเพจของคุณ แล้วเริ่มวางแผนสัปดาห์แรก</p>
|
||||
<Link href="/pages" className="btn btn-primary btn-lg mt-2"><Icon name="link" size={18} /> เชื่อมเพจ Facebook <Icon name="right" size={16} /></Link>
|
||||
<span className="text-[11px] text-ink-3">รองรับ Facebook และ Instagram ที่ผูกกับเพจ</span>
|
||||
</div>
|
||||
<div className="workflow-preview" aria-label="ขั้นตอนวางแผนโพสต์">
|
||||
<div className="flex items-center gap-2 pb-4 border-b border-line"><span className="w-8 h-8 rounded-lg grid place-items-center bg-blue-soft text-blue"><Icon name="calendar" size={16} /></span><span className="text-[13px] font-semibold">จากไอเดีย สู่โพสต์บนเพจ</span></div>
|
||||
{[
|
||||
{ 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) => <div key={s.title} className="workflow-step"><span className={`workflow-icon ${s.tone}`}><Icon name={s.icon} size={18} /></span><div className="flex-1"><h3>{s.title}</h3><p>{s.hint}</p></div><span className="text-ink-4 text-[11px] num">0{i + 1}</span></div>)}
|
||||
<div className="flex items-center gap-2 pt-3 text-[11px] text-green-ink"><Icon name="shield" size={14} /> ทุกโพสต์ผ่านการอนุมัติก่อนเผยแพร่</div>
|
||||
</div>
|
||||
</section>
|
||||
<div className="flex items-center justify-between gap-3 mt-2"><h2 className="font-semibold">เตรียมพื้นที่ทำงานของคุณ</h2><span className="text-[12px] text-ink-3">เริ่มต้น 3 ขั้นตอน</span></div>
|
||||
<ol className="setup-steps">
|
||||
{FIRST_RUN_STEPS.map((s, i) => <li key={s.title} className="setup-step"><div className="flex items-center justify-between mb-5"><span className="setup-icon"><Icon name={s.icon} size={21} /></span><span className="text-[11px] text-ink-4">ขั้นตอน {i + 1}</span></div><h3>{s.title}</h3><p>{s.body}</p>{i === 0 && <Link href="/pages" className="text-[12px] text-blue font-medium mt-4 inline-flex items-center gap-1">เริ่มเชื่อมต่อ <Icon name="right" size={13} /></Link>}</li>)}
|
||||
</ol>
|
||||
</> : <section className="card"><EmptyState icon="pages" title="ยังไม่มีเพจให้คุณดูแล" hint="ขอให้ผู้ดูแลระบบเพิ่มคุณเป็นสมาชิกเพจ แล้วกลับมาที่หน้านี้" /></section>}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user