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(); 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 ( <> เดือนนี้ } /> {/* Phone: agenda — only the days that have posts, in date order. */}
{agenda.length === 0 ? ( สร้างโพสต์ ) : (
    {agenda.map(({ key, day, items }) => (
  1. {WEEKDAYS[(new Date(Date.UTC(y, mo, day)).getUTCDay() + 6) % 7]}
    {day}
      {items.map((p) => { const meta = POST_STATUS_META[p.status]; return (
    • {!ctx.selected && } {p.scheduledPublishAt || p.status === "PUBLISHED" ? timeFmt.format(postBoardDate(p)!) : "แผน"} {p.title} {meta.label}
    • ); })}
  2. ))}
)}
{WEEKDAYS.map((d) => (
{d}
))}
{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 (
{inMonth && (
{day}
)}
{items.slice(0, 4).map((p) => { const meta = POST_STATUS_META[p.status]; return ( {!ctx.selected && } {p.scheduledPublishAt || p.status === "PUBLISHED" ? timeFmt.format(postBoardDate(p)!) : "แผน"} {p.title} ); })} {items.length > 4 && +{items.length - 4} โพสต์}
); })}
{!ctx.selected && ctx.pages.length > 1 && (
{ctx.pages.map((p) => ( {p.name} ))}
)} ); }