9d32552457
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>
178 lines
8.6 KiB
TypeScript
178 lines
8.6 KiB
TypeScript
import Link from "next/link";
|
|
import { prisma } from "@/lib/db";
|
|
import { getConsoleContext, scopedPageIds } from "@/lib/console";
|
|
import { EmptyState, PageTitle, POST_STATUS_META } from "@/components/ui";
|
|
import { Icon } from "@/components/Icon";
|
|
import { postBoardDate } from "@/lib/board";
|
|
|
|
const TZ = "Asia/Bangkok";
|
|
const WEEKDAYS = ["จ.", "อ.", "พ.", "พฤ.", "ศ.", "ส.", "อา."];
|
|
const monthFmt = new Intl.DateTimeFormat("th-TH", { month: "long", year: "numeric", timeZone: TZ });
|
|
const timeFmt = new Intl.DateTimeFormat("th-TH", { hour: "2-digit", minute: "2-digit", timeZone: TZ });
|
|
const dayKeyFmt = new Intl.DateTimeFormat("en-CA", { year: "numeric", month: "2-digit", day: "2-digit", timeZone: TZ });
|
|
|
|
// Distinguishes pages when viewing "ทุกเพจ" — blues/greens/ambers from the palette only.
|
|
const PAGE_DOTS = ["var(--m-blue)", "var(--m-green)", "var(--m-yellow)", "var(--ink)", "var(--m-blue)", "var(--m-green-ink)"];
|
|
|
|
function parseMonth(m: string | undefined): { y: number; mo: number } {
|
|
const match = m?.match(/^(\d{4})-(\d{2})$/);
|
|
if (match) return { y: Number(match[1]), mo: Number(match[2]) - 1 };
|
|
const [y, mo] = dayKeyFmt.format(new Date()).split("-").map(Number);
|
|
return { y, mo: mo - 1 };
|
|
}
|
|
const monthParam = (y: number, mo: number) => {
|
|
const d = new Date(Date.UTC(y, mo, 1));
|
|
return `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, "0")}`;
|
|
};
|
|
|
|
export default async function CalendarPage(props: PageProps<"/calendar">) {
|
|
const ctx = await getConsoleContext();
|
|
const sp = await props.searchParams;
|
|
const { y, mo } = parseMonth(typeof sp.m === "string" ? sp.m : undefined);
|
|
|
|
// Bangkok is UTC+7 year-round: month bounds in UTC.
|
|
const start = new Date(Date.UTC(y, mo, 1, -7));
|
|
const end = new Date(Date.UTC(y, mo + 1, 1, -7));
|
|
const pageIds = scopedPageIds(ctx);
|
|
|
|
const posts = await prisma.post.findMany({
|
|
where: {
|
|
pageId: { in: pageIds },
|
|
status: { notIn: ["CANCELLED"] },
|
|
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 } },
|
|
],
|
|
},
|
|
select: { id: true, title: true, status: true, pageId: true, scheduledPublishAt: true, plannedAt: true, updatedAt: true },
|
|
orderBy: [{ scheduledPublishAt: "asc" }, { updatedAt: "asc" }],
|
|
});
|
|
|
|
const byDay = new Map<string, typeof posts>();
|
|
for (const p of posts) {
|
|
const key = dayKeyFmt.format(postBoardDate(p)!);
|
|
byDay.set(key, [...(byDay.get(key) ?? []), p]);
|
|
}
|
|
const dotOf = new Map(ctx.pages.map((p, i) => [p.id, PAGE_DOTS[i % PAGE_DOTS.length]]));
|
|
|
|
// Monday-first grid covering the month.
|
|
const first = new Date(Date.UTC(y, mo, 1));
|
|
const lead = (first.getUTCDay() + 6) % 7;
|
|
const daysInMonth = new Date(Date.UTC(y, mo + 1, 0)).getUTCDate();
|
|
const cells = Math.ceil((lead + daysInMonth) / 7) * 7;
|
|
const todayKey = dayKeyFmt.format(new Date());
|
|
const agenda = Array.from({ length: daysInMonth }, (_, i) => {
|
|
const day = i + 1;
|
|
const key = `${y}-${String(mo + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
|
|
return { key, day, items: byDay.get(key) ?? [] };
|
|
}).filter((d) => d.items.length > 0);
|
|
|
|
return (
|
|
<>
|
|
<PageTitle
|
|
title="ปฏิทินโพสต์"
|
|
subtitle={monthFmt.format(new Date(Date.UTC(y, mo, 15)))}
|
|
actions={
|
|
<>
|
|
<Link href={`/calendar?m=${monthParam(y, mo - 1)}`} className="btn btn-on-band" aria-label="เดือนก่อน">
|
|
<Icon name="left" />
|
|
</Link>
|
|
<Link href="/calendar" className="btn btn-on-band">
|
|
เดือนนี้
|
|
</Link>
|
|
<Link href={`/calendar?m=${monthParam(y, mo + 1)}`} className="btn btn-on-band" aria-label="เดือนถัดไป">
|
|
<Icon name="right" />
|
|
</Link>
|
|
</>
|
|
}
|
|
/>
|
|
|
|
{/* Phone: agenda — only the days that have posts, in date order. */}
|
|
<section className="card overflow-hidden md:hidden">
|
|
{agenda.length === 0 ? (
|
|
<EmptyState icon="calendar" title="เดือนนี้ยังไม่มีโพสต์" hint="โพสต์ที่ตั้งเวลาหรือเผยแพร่แล้วจะแสดงตรงนี้">
|
|
<Link href="/posts/new" className="btn btn-primary mt-2">
|
|
<Icon name="plus" /> สร้างโพสต์
|
|
</Link>
|
|
</EmptyState>
|
|
) : (
|
|
<ol>
|
|
{agenda.map(({ key, day, items }) => (
|
|
<li key={key} className="flex gap-3 px-4 py-3 border-b border-line-2 last:border-0">
|
|
<div className={`w-12 shrink-0 text-center rounded-[14px] py-1.5 ${key === todayKey ? "bg-blue text-white" : "bg-board text-blue-ink"}`}>
|
|
<div className="text-[11px] leading-none">{WEEKDAYS[(new Date(Date.UTC(y, mo, day)).getUTCDay() + 6) % 7]}</div>
|
|
<div className="num text-[20px] font-semibold leading-tight">{day}</div>
|
|
</div>
|
|
<ul className="flex-1 min-w-0 flex flex-col gap-1.5">
|
|
{items.map((p) => {
|
|
const meta = POST_STATUS_META[p.status];
|
|
return (
|
|
<li key={p.id}>
|
|
<Link href={`/posts/${p.id}`} className="flex items-center gap-2 min-h-[44px] rounded-[12px] px-3 py-2 bg-board active:bg-board">
|
|
{!ctx.selected && <span className="dot" style={{ background: dotOf.get(p.pageId) }} />}
|
|
<span className="num text-[13px] text-ink-2 shrink-0">{p.scheduledPublishAt || p.status === "PUBLISHED" ? timeFmt.format(postBoardDate(p)!) : "แผน"}</span>
|
|
<span className="flex-1 truncate">{p.title}</span>
|
|
<span className={`pill ${meta.pill}`}>{meta.label}</span>
|
|
</Link>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</li>
|
|
))}
|
|
</ol>
|
|
)}
|
|
</section>
|
|
|
|
<section className="card overflow-hidden hidden md:block">
|
|
<div className="grid grid-cols-7 border-b border-line-2 text-[12px] text-ink-3">
|
|
{WEEKDAYS.map((d) => (
|
|
<div key={d} className="px-3 py-3">
|
|
<span className="section-label">{d}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
<div className="grid grid-cols-7">
|
|
{Array.from({ length: cells }, (_, i) => {
|
|
const day = i - lead + 1;
|
|
const inMonth = day >= 1 && day <= daysInMonth;
|
|
const key = inMonth ? `${y}-${String(mo + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}` : "";
|
|
const items = inMonth ? byDay.get(key) ?? [] : [];
|
|
return (
|
|
<div key={i} className={`min-h-[118px] border-b border-r border-line-2 p-1.5 ${inMonth ? "" : "bg-board"}`}>
|
|
{inMonth && (
|
|
<div className={`num text-[12px] w-6 h-6 grid place-items-center rounded-full mb-1 ${key === todayKey ? "bg-blue text-white" : "text-ink-3"}`}>{day}</div>
|
|
)}
|
|
<div className="flex flex-col gap-1">
|
|
{items.slice(0, 4).map((p) => {
|
|
const meta = POST_STATUS_META[p.status];
|
|
return (
|
|
<Link key={p.id} href={`/posts/${p.id}`} className={`pill ${meta.pill} w-full justify-start overflow-hidden`} title={`${p.title} · ${meta.label}`}>
|
|
{!ctx.selected && <span className="dot" style={{ background: dotOf.get(p.pageId) }} />}
|
|
<span className="num shrink-0">{p.scheduledPublishAt || p.status === "PUBLISHED" ? timeFmt.format(postBoardDate(p)!) : "แผน"}</span>
|
|
<span className="truncate font-normal">{p.title}</span>
|
|
</Link>
|
|
);
|
|
})}
|
|
{items.length > 4 && <span className="text-[11px] text-ink-3 px-1">+{items.length - 4} โพสต์</span>}
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</section>
|
|
|
|
{!ctx.selected && ctx.pages.length > 1 && (
|
|
<div className="flex flex-wrap gap-3 text-[13px] text-ink-2">
|
|
{ctx.pages.map((p) => (
|
|
<span key={p.id} className="flex items-center gap-1.5">
|
|
<span className="dot" style={{ background: dotOf.get(p.id) }} /> {p.name}
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|