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:
chaloemphong
2026-11-22 11:52:17 +07:00
commit 9d32552457
155 changed files with 22174 additions and 0 deletions
+190
View File
@@ -0,0 +1,190 @@
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { Icon } from "./Icon";
import { EmptyState, Notice, numberFmt } from "./ui";
type Kind = "DM" | "COMMENT";
interface Rule {
id: string;
name: string;
kind: Kind;
keywords: string[];
reply: string;
imageUrl: string | null;
publicAck: string | null;
isActive: boolean;
priority: number;
sent: number;
}
const EMPTY: Omit<Rule, "id" | "sent"> = { name: "", kind: "COMMENT", keywords: [], reply: "", imageUrl: null, publicAck: "ส่งรายละเอียดให้ทาง inbox แล้วนะคะ", isActive: true, priority: 0 };
const KIND_LABEL: Record<Kind, string> = { COMMENT: "คอมเมนต์ → ตอบทาง inbox", DM: "ข้อความ inbox → ตอบในแชท" };
export function RulesManager({ pageId, canEdit, rules }: { pageId: string; canEdit: boolean; rules: Rule[] }) {
const router = useRouter();
const [editing, setEditing] = useState<(Omit<Rule, "id" | "sent"> & { id?: string }) | null>(null);
const [keywordsText, setKeywordsText] = useState("");
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
function open(rule: Rule | null) {
setError(null);
setEditing(rule ? { ...rule } : { ...EMPTY });
setKeywordsText((rule?.keywords ?? []).join(", "));
}
async function save() {
if (!editing) return;
setBusy(true);
setError(null);
const res = await fetch("/api/reply-rules", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...editing, pageId, keywords: keywordsText }),
});
const body = await res.json().catch(() => ({}));
setBusy(false);
if (!res.ok) return setError(body.error ?? "บันทึกไม่สำเร็จ");
setEditing(null);
router.refresh();
}
async function toggle(rule: Rule) {
await fetch("/api/reply-rules", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...rule, pageId, isActive: !rule.isActive }),
});
router.refresh();
}
async function remove(rule: Rule) {
if (!window.confirm(`ลบกฎ "${rule.name}"?`)) return;
await fetch(`/api/reply-rules?id=${rule.id}`, { method: "DELETE" });
router.refresh();
}
return (
<div className="grid gap-5 grid-cols-[minmax(0,1fr)] xl:grid-cols-[minmax(0,1fr)_420px] items-start">
<section className="card">
<div className="card-head">
<h2>กฎทั้งหมด</h2>
{canEdit && (
<button type="button" className="btn btn-primary btn-sm ml-auto" onClick={() => open(null)}>
<Icon name="plus" size={14} /> เพิ่มกฎ
</button>
)}
</div>
{rules.length === 0 ? (
<EmptyState icon="bolt" title="ยังไม่มีกฎ" hint="เช่น คอมเมนต์ว่า “สนใจ” → ส่งรายละเอียดทาง inbox อัตโนมัติ" />
) : (
<ul>
{rules.map((r) => (
<li key={r.id} className="flex items-start gap-3 px-[18px] py-3.5 border-b border-line-2 last:border-0">
<button type="button" role="switch" aria-checked={r.isActive} aria-label={`เปิด/ปิดกฎ ${r.name}`} className="toggle mt-0.5" disabled={!canEdit} onClick={() => toggle(r)} />
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<b>{r.name}</b>
<span className={`pill ${r.kind === "COMMENT" ? "pill-blue" : "pill-green"}`}>{r.kind === "COMMENT" ? "คอมเมนต์" : "Inbox"}</span>
{r.priority !== 0 && <span className="pill pill-neutral num">ลำดับ {r.priority}</span>}
</div>
<div className="flex flex-wrap gap-1 mt-1.5">
{r.keywords.map((k) => (
<span key={k} className="pill pill-neutral">
{k}
</span>
))}
</div>
<p className="text-[13px] text-ink-2 mt-1.5 line-clamp-2 whitespace-pre-wrap">{r.reply}</p>
<p className="text-[12px] text-ink-4 mt-1 num">ส่งแล้ว {numberFmt.format(r.sent)} คน</p>
</div>
{canEdit && (
<div className="flex gap-1">
<button type="button" className="btn btn-ghost btn-sm" onClick={() => open(r)}>
แก้ไข
</button>
<button type="button" className="btn btn-danger btn-sm" onClick={() => remove(r)} aria-label="ลบ">
<Icon name="trash" size={14} />
</button>
</div>
)}
</li>
))}
</ul>
)}
</section>
{editing ? (
<section className="card">
<div className="card-head">
<h2>{editing.id ? "แก้ไขกฎ" : "กฎใหม่"}</h2>
</div>
<div className="card-body flex flex-col gap-4">
<label className="field">
<span>ชื่อกฎ</span>
<input className="input" value={editing.name} onChange={(e) => setEditing({ ...editing, name: e.target.value })} placeholder="เช่น ส่งราคาเมื่อคอมเมนต์สนใจ" />
</label>
<div className="field">
<span>ทำงานเมื่อ</span>
<div className="flex flex-wrap gap-2">
{(["COMMENT", "DM"] as Kind[]).map((k) => (
<button key={k} type="button" className="chip" aria-pressed={editing.kind === k} onClick={() => setEditing({ ...editing, kind: k })}>
{KIND_LABEL[k]}
</button>
))}
</div>
</div>
<label className="field">
<span>Keyword (คั่นด้วยจุลภาค — มีคำใดคำหนึ่งก็ทำงาน)</span>
<input className="input" value={keywordsText} onChange={(e) => setKeywordsText(e.target.value)} placeholder="สนใจ, ราคา, inbox" />
</label>
<label className="field">
<span>ข้อความตอบกลับ{editing.kind === "COMMENT" ? " (ส่งทาง inbox)" : ""}</span>
<textarea className="textarea" rows={5} maxLength={2000} value={editing.reply} onChange={(e) => setEditing({ ...editing, reply: e.target.value })} />
</label>
{editing.kind === "COMMENT" && (
<label className="field">
<span>ตอบใต้คอมเมนต์ (ไม่บังคับ — คนอื่นเห็น)</span>
<input className="input" value={editing.publicAck ?? ""} onChange={(e) => setEditing({ ...editing, publicAck: e.target.value || null })} />
</label>
)}
{editing.kind === "DM" && (
<label className="field">
<span>รูปแนบ URL (ไม่บังคับ)</span>
<input className="input" value={editing.imageUrl ?? ""} onChange={(e) => setEditing({ ...editing, imageUrl: e.target.value || null })} placeholder="https://…" />
</label>
)}
<label className="field">
<span>ลำดับความสำคัญ (เลขมากถูกเช็กก่อน)</span>
<input className="input num" type="number" value={editing.priority} onChange={(e) => setEditing({ ...editing, priority: Number(e.target.value) })} />
</label>
{editing.kind === "COMMENT" && (
<Notice tone="blue">ตอบทาง inbox ได้เฉพาะคอมเมนต์บนโพสต์ของเพจ ภายใน 7 วัน — คอมเมนต์ในกลุ่มตอบไม่ได้</Notice>
)}
{error && <Notice tone="red">{error}</Notice>}
<div className="flex gap-2">
<button type="button" className="btn btn-primary" disabled={busy} onClick={save}>
{busy ? "กำลังบันทึก…" : "บันทึกกฎ"}
</button>
<button type="button" className="btn btn-ghost" onClick={() => setEditing(null)}>
ยกเลิก
</button>
</div>
</div>
</section>
) : (
<section className="card card-body text-ink-2 flex flex-col gap-2">
<b className="text-ink">ทำงานยังไง</b>
<p>ลูกค้าคอมเมนต์หรือทักมา → ระบบหา keyword ตามลำดับความสำคัญ → ตอบตามกฎแรกที่ตรง</p>
<p>คนเดิมได้คำตอบจากกฎเดิมครั้งเดียว กันสแปม</p>
<p>แชทที่สลับเป็น “คนตอบ” ใน Inbox บอทจะไม่ตอบ</p>
{!canEdit && <p className="text-ink-3 text-[13px]">ต้องเป็นผู้อนุมัติขึ้นไปถึงจะแก้กฎได้</p>}
</section>
)}
</div>
);
}