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,293 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { Icon, type IconName } from "./Icon";
|
||||
import { PageAvatar } from "./PageAvatar";
|
||||
import { ToastProvider } from "./Toast";
|
||||
|
||||
interface ShellPage {
|
||||
id: string;
|
||||
name: string;
|
||||
pictureUrl: string | null;
|
||||
status: string;
|
||||
}
|
||||
|
||||
type CountKey = "pendingApprovals";
|
||||
|
||||
interface NavEntry {
|
||||
href: string;
|
||||
label: string;
|
||||
icon: IconName;
|
||||
superadmin?: boolean;
|
||||
count?: CountKey;
|
||||
/** Other sections that light this entry up. */
|
||||
also?: string[];
|
||||
}
|
||||
|
||||
const WORK: NavEntry[] = [
|
||||
{ href: "/", label: "แผนคอนเทนต์", icon: "calendar", also: ["/calendar"] },
|
||||
{ href: "/posts", label: "โพสต์ทั้งหมด", icon: "post", count: "pendingApprovals" },
|
||||
{ href: "/inbox", label: "Inbox", icon: "inbox" },
|
||||
{ href: "/auto-reply", label: "Auto-reply", icon: "bolt" },
|
||||
{ href: "/insights", label: "Insights", icon: "chart" },
|
||||
{ href: "/analysis", label: "วิเคราะห์ & Ads", icon: "target" },
|
||||
];
|
||||
const SETTINGS: NavEntry[] = [
|
||||
{ href: "/account", label: "บัญชีของฉัน", icon: "users" },
|
||||
{ href: "/pages", label: "เพจที่เชื่อม", icon: "pages" },
|
||||
{ href: "/profile", label: "โปรไฟล์ร้าน", icon: "store" },
|
||||
{ href: "/users", label: "ผู้ใช้", icon: "users", superadmin: true },
|
||||
];
|
||||
|
||||
// Thumb tabs on phones; everything else is in the "เมนู" drawer.
|
||||
const TABS: NavEntry[] = [WORK[0], WORK[1], WORK[2]];
|
||||
|
||||
function setSelectedPage(id: string | null) {
|
||||
document.cookie = id
|
||||
? `pm_page=${encodeURIComponent(id)}; path=/; max-age=31536000; samesite=lax`
|
||||
: "pm_page=; path=/; max-age=0; samesite=lax";
|
||||
}
|
||||
|
||||
function PageOptions({ pages, selectedId, onPick }: { pages: ShellPage[]; selectedId: string | null; onPick: (id: string | null) => void }) {
|
||||
return (
|
||||
<ul role="listbox" aria-label="เลือกเพจ" className="flex flex-col gap-0.5">
|
||||
<li>
|
||||
<button type="button" role="option" aria-selected={!selectedId} className="page-option" onClick={() => onPick(null)}>
|
||||
<span className="w-9 h-9 rounded-full bg-board border border-line grid place-items-center shrink-0 text-ink-2">
|
||||
<Icon name="pages" size={16} />
|
||||
</span>
|
||||
<span className="flex-1 text-left">ทุกเพจ</span>
|
||||
{!selectedId && <Icon name="check" size={18} />}
|
||||
</button>
|
||||
</li>
|
||||
{pages.map((p) => (
|
||||
<li key={p.id}>
|
||||
<button type="button" role="option" aria-selected={p.id === selectedId} className="page-option" onClick={() => onPick(p.id)}>
|
||||
<PageAvatar name={p.name} src={p.pictureUrl} size={36} />
|
||||
<span className="flex-1 min-w-0 text-left truncate">{p.name}</span>
|
||||
{p.status !== "ACTIVE" && <span className="pill pill-red">token มีปัญหา</span>}
|
||||
{p.id === selectedId && <Icon name="check" size={18} />}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
{pages.length === 0 && <li className="p-3 text-ink-3 text-[13px]">ยังไม่มีเพจที่คุณเข้าถึงได้</li>}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
export function Shell({
|
||||
user,
|
||||
pages,
|
||||
selectedId,
|
||||
counts,
|
||||
children,
|
||||
}: {
|
||||
user: { name: string; email: string; avatarUrl: string | null; superadmin: boolean };
|
||||
pages: ShellPage[];
|
||||
selectedId: string | null;
|
||||
counts: Record<CountKey, number>;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
const [frameOpen, setFrameOpen] = useState(false);
|
||||
const [switcherOpen, setSwitcherOpen] = useState(false);
|
||||
const [sheetOpen, setSheetOpen] = useState(false);
|
||||
const switcherRef = useRef<HTMLDivElement>(null);
|
||||
const selected = pages.find((p) => p.id === selectedId) ?? null;
|
||||
|
||||
useEffect(() => {
|
||||
if (!switcherOpen) return;
|
||||
const close = (e: MouseEvent) => {
|
||||
if (!switcherRef.current?.contains(e.target as Node)) setSwitcherOpen(false);
|
||||
};
|
||||
document.addEventListener("mousedown", close);
|
||||
return () => document.removeEventListener("mousedown", close);
|
||||
}, [switcherOpen]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!sheetOpen && !frameOpen) return;
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") {
|
||||
setSheetOpen(false);
|
||||
setFrameOpen(false);
|
||||
}
|
||||
};
|
||||
document.addEventListener("keydown", onKey);
|
||||
return () => document.removeEventListener("keydown", onKey);
|
||||
}, [sheetOpen, frameOpen]);
|
||||
|
||||
function pick(id: string | null) {
|
||||
setSelectedPage(id);
|
||||
setSwitcherOpen(false);
|
||||
setSheetOpen(false);
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
await fetch("/api/auth/logout", { method: "POST" });
|
||||
router.replace("/login");
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
const section = "/" + (pathname.split("/")[1] ?? "");
|
||||
const isActive = (n: NavEntry) => section === n.href || Boolean(n.also?.includes(section));
|
||||
const moreActive = !TABS.some(isActive);
|
||||
const sectionLabel = [...WORK, ...SETTINGS].find(isActive)?.label ?? "พื้นที่ทำงาน";
|
||||
|
||||
const navList = (entries: NavEntry[]) =>
|
||||
entries
|
||||
.filter((n) => !n.superadmin || user.superadmin)
|
||||
.map((n) => {
|
||||
const count = n.count ? counts[n.count] : 0;
|
||||
return (
|
||||
<Link key={n.href} href={n.href} className="nav-item" aria-current={isActive(n) ? "page" : undefined} onClick={() => setFrameOpen(false)}>
|
||||
<Icon name={n.icon} />
|
||||
{n.label}
|
||||
{count > 0 && (
|
||||
<span className="nav-count num" title="รออนุมัติ">
|
||||
{count}
|
||||
</span>
|
||||
)}
|
||||
</Link>
|
||||
);
|
||||
});
|
||||
|
||||
return (
|
||||
<ToastProvider>
|
||||
<div className="shell">
|
||||
<aside className="frame" data-open={frameOpen} aria-label="เมนูหลัก">
|
||||
<div className="flex items-center gap-3 px-1.5 pb-2">
|
||||
<span className="w-9 h-9 rounded-[10px] overflow-hidden shrink-0 shadow-sm">
|
||||
{/* eslint-disable-next-line @next/next/no-img-element -- generated app icon route */}
|
||||
<img src="/icons/192" alt="" width={36} height={36} />
|
||||
</span>
|
||||
<div className="leading-tight flex-1 min-w-0">
|
||||
<div className="font-bold">ระบบจัดการเพจ</div>
|
||||
<div className="text-ink-3 text-[11px] mt-1">Content workspace</div>
|
||||
</div>
|
||||
<button type="button" className="min-[960px]:hidden p-2 -mr-1 rounded-full hover:bg-white/60" onClick={() => setFrameOpen(false)} aria-label="ปิดเมนู">
|
||||
<Icon name="x" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="relative" ref={switcherRef}>
|
||||
<button type="button" className="page-tag" aria-haspopup="listbox" aria-expanded={switcherOpen} onClick={() => setSwitcherOpen((o) => !o)}>
|
||||
{selected ? (
|
||||
<PageAvatar name={selected.name} src={selected.pictureUrl} size={38} />
|
||||
) : (
|
||||
<span className="w-[38px] h-[38px] rounded-full bg-board border border-line grid place-items-center text-ink-2">
|
||||
<Icon name="pages" size={16} />
|
||||
</span>
|
||||
)}
|
||||
<span className="flex-1 min-w-0 leading-tight">
|
||||
<span className="block text-[11px] text-ink-3">กำลังดู</span>
|
||||
<span className="block truncate font-semibold">{selected?.name ?? "ทุกเพจ"}</span>
|
||||
</span>
|
||||
<Icon name="chevron" size={16} />
|
||||
</button>
|
||||
{switcherOpen && (
|
||||
<div className="absolute z-20 mt-2 w-full card p-1.5 max-h-80 overflow-auto">
|
||||
<PageOptions pages={pages} selectedId={selectedId} onPick={pick} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<nav className="flex flex-col gap-1" aria-label="งาน">
|
||||
<div className="frame-label mb-2">พื้นที่ทำงาน</div>
|
||||
{navList(WORK)}
|
||||
</nav>
|
||||
<div className="flex flex-col gap-1">
|
||||
<div className="frame-label">ตั้งค่า</div>
|
||||
<nav className="flex flex-col gap-1" aria-label="ตั้งค่า">
|
||||
{navList(SETTINGS)}
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
<div className="mt-auto flex items-center gap-3 px-2 pt-3 border-t border-[var(--frame-edge)]">
|
||||
<Link href="/account" aria-label="ตั้งค่าโปรไฟล์ของฉัน" onClick={() => setFrameOpen(false)}><PageAvatar name={user.name || user.email} src={user.avatarUrl} size={36} /></Link>
|
||||
<div className="flex-1 min-w-0 leading-tight">
|
||||
<div className="truncate font-medium">{user.name}</div>
|
||||
<div className="truncate text-ink-3 text-[12px]">{user.superadmin ? "ผู้ดูแลระบบ" : user.email}</div>
|
||||
</div>
|
||||
<button type="button" className="p-2.5 rounded-full hover:bg-white/60 text-ink-2" onClick={logout} aria-label="ออกจากระบบ" title="ออกจากระบบ">
|
||||
<Icon name="logout" />
|
||||
</button>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
{frameOpen && <div className="scrim min-[960px]:hidden" onClick={() => setFrameOpen(false)} />}
|
||||
|
||||
<div className="board">
|
||||
<header className="workspace-bar hidden min-[960px]:flex">
|
||||
<div className="flex items-center gap-2 text-[13px] text-ink-3"><Icon name="pages" size={16} /><span>พื้นที่ทำงาน</span><span className="text-ink-4">/</span><span className="text-ink font-medium">{sectionLabel}</span></div>
|
||||
<div className="flex items-center gap-3 ml-auto text-[12px] text-ink-3"><span>Facebook & Instagram</span><span className="w-1 h-1 rounded-full bg-line" /><span>{pages.length} เพจ</span><Link href="/account" aria-label="ตั้งค่าโปรไฟล์ของฉัน"><PageAvatar name={user.name || user.email} src={user.avatarUrl} size={32} /></Link></div>
|
||||
</header>
|
||||
{/* Phone top bar: the page chip is the context switch. */}
|
||||
<header className="topbar min-[960px]:hidden">
|
||||
<button type="button" className="page-chip" onClick={() => setSheetOpen(true)} aria-haspopup="dialog">
|
||||
{selected ? (
|
||||
<PageAvatar name={selected.name} src={selected.pictureUrl} size={30} />
|
||||
) : (
|
||||
<span className="w-[30px] h-[30px] rounded-full bg-board grid place-items-center text-ink-2">
|
||||
<Icon name="pages" size={14} />
|
||||
</span>
|
||||
)}
|
||||
<span className="truncate max-w-[56vw]">{selected?.name ?? "ทุกเพจ"}</span>
|
||||
<Icon name="chevron" size={15} />
|
||||
</button>
|
||||
</header>
|
||||
<main className="page-body">{children}</main>
|
||||
</div>
|
||||
|
||||
<nav className="tabbar min-[960px]:hidden" aria-label="เมนูลัด">
|
||||
{TABS.slice(0, 2).map((t) => (
|
||||
<TabLink key={t.href} entry={t} active={isActive(t)} count={t.count ? counts[t.count] : 0} />
|
||||
))}
|
||||
<Link href="/posts/new" className="tab-create" aria-label="สร้างโพสต์">
|
||||
<span className="tab-create-btn">
|
||||
<Icon name="plus" size={24} />
|
||||
</span>
|
||||
</Link>
|
||||
<TabLink entry={TABS[2]} active={isActive(TABS[2])} count={0} />
|
||||
<button type="button" className="tab" data-active={moreActive} onClick={() => setFrameOpen(true)}>
|
||||
<Icon name="menu" size={22} />
|
||||
<span>เมนู</span>
|
||||
</button>
|
||||
</nav>
|
||||
|
||||
{sheetOpen && (
|
||||
<div className="sheet-root min-[960px]:hidden" role="dialog" aria-modal="true" aria-label="เลือกเพจ">
|
||||
<div className="scrim" onClick={() => setSheetOpen(false)} />
|
||||
<div className="sheet">
|
||||
<div className="sheet-grip" aria-hidden="true" />
|
||||
<div className="flex items-center px-4 pb-2">
|
||||
<b className="text-[17px]">เลือกเพจ</b>
|
||||
<button type="button" className="ml-auto p-2 -mr-2 rounded-full text-ink-3" onClick={() => setSheetOpen(false)} aria-label="ปิด">
|
||||
<Icon name="x" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="px-2 pb-2 overflow-y-auto">
|
||||
<PageOptions pages={pages} selectedId={selectedId} onPick={pick} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</ToastProvider>
|
||||
);
|
||||
}
|
||||
|
||||
function TabLink({ entry, active, count }: { entry: NavEntry; active: boolean; count: number }) {
|
||||
return (
|
||||
<Link href={entry.href} className="tab" data-active={active} aria-current={active ? "page" : undefined}>
|
||||
<span className="relative">
|
||||
<Icon name={entry.icon} size={22} />
|
||||
{count > 0 && <span className="tab-badge num">{count > 99 ? "99+" : count}</span>}
|
||||
</span>
|
||||
<span>{entry.href === "/posts" ? "โพสต์" : entry.label}</span>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user