"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 ( ); } 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; 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(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 ( setFrameOpen(false)}> {n.label} {count > 0 && ( {count} )} ); }); return (
{frameOpen &&
setFrameOpen(false)} />}
พื้นที่ทำงาน/{sectionLabel}
Facebook & Instagram{pages.length} เพจ
{/* Phone top bar: the page chip is the context switch. */}
{children}
{sheetOpen && (
setSheetOpen(false)} />
)}
); } function TabLink({ entry, active, count }: { entry: NavEntry; active: boolean; count: number }) { return ( {count > 0 && {count > 99 ? "99+" : count}} {entry.href === "/posts" ? "โพสต์" : entry.label} ); }