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,358 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Icon } from "./Icon";
|
||||
import { EmptyState, formatDateTime, Notice, numberFmt } from "./ui";
|
||||
import type { AnalysisBrief, AnalysisReport } from "@/lib/analysis/types";
|
||||
|
||||
type Goal = AnalysisBrief["goal"];
|
||||
type Status = "QUEUED" | "RUNNING" | "COMPLETED" | "FAILED";
|
||||
|
||||
export interface AnalysisItem {
|
||||
id: string;
|
||||
status: Status;
|
||||
brief: AnalysisBrief;
|
||||
postCount: number;
|
||||
warnings: string[];
|
||||
report: AnalysisReport | null;
|
||||
budget: { testBaht: number; scaleBaht: number; reserveBaht: number; daily: number[] } | null;
|
||||
error: string | null;
|
||||
createdAt: string;
|
||||
finishedAt: string | null;
|
||||
requestedBy: string | null;
|
||||
}
|
||||
|
||||
// Keep in sync with GOALS in src/lib/analysis/plan.mjs.
|
||||
const GOAL_LABEL: Record<Goal, string> = { MESSAGES: "เพิ่มข้อความสอบถาม", SALES: "เพิ่มยอดขาย", AWARENESS: "เพิ่มการรับรู้" };
|
||||
|
||||
const STATUS_META: Record<Status, { label: string; pill: string }> = {
|
||||
QUEUED: { label: "รอคิว", pill: "pill-neutral" },
|
||||
RUNNING: { label: "กำลังวิเคราะห์", pill: "pill-blue" },
|
||||
COMPLETED: { label: "เสร็จแล้ว", pill: "pill-green" },
|
||||
FAILED: { label: "ไม่สำเร็จ", pill: "pill-red" },
|
||||
};
|
||||
|
||||
const baht = (n: number) => `${numberFmt.format(n)} บาท`;
|
||||
|
||||
export function AnalysisView({
|
||||
pageId,
|
||||
pageActive,
|
||||
canAnalyze,
|
||||
canWrite,
|
||||
briefDefaults,
|
||||
items,
|
||||
}: {
|
||||
pageId: string;
|
||||
pageActive: boolean;
|
||||
canAnalyze: boolean;
|
||||
/** Can create posts on this page — shows "ใช้ไอเดียนี้". */
|
||||
canWrite: boolean;
|
||||
/** Prefilled from the shop profile. */
|
||||
briefDefaults: { business: string; audience: string };
|
||||
items: AnalysisItem[];
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const [selectedId, setSelectedId] = useState<string | null>(items[0]?.id ?? null);
|
||||
const selected = items.find((i) => i.id === selectedId) ?? items[0] ?? null;
|
||||
const pending = items.some((i) => i.status === "QUEUED" || i.status === "RUNNING");
|
||||
|
||||
// The worker updates rows out of band — poll while something is in flight.
|
||||
useEffect(() => {
|
||||
if (!pending) return;
|
||||
const t = setInterval(() => router.refresh(), 5000);
|
||||
return () => clearInterval(t);
|
||||
}, [pending, router]);
|
||||
|
||||
return (
|
||||
<div className="grid gap-5 grid-cols-[minmax(0,1fr)] xl:grid-cols-[380px_minmax(0,1fr)] items-start">
|
||||
<div className="flex flex-col gap-5 min-w-0">
|
||||
{canAnalyze ? (
|
||||
<BriefForm pageId={pageId} defaults={briefDefaults} disabled={!pageActive || pending} disabledReason={!pageActive ? "เพจนี้ยังเชื่อมไม่พร้อม — ไปแก้ที่หน้าเพจที่เชื่อมก่อน" : pending ? "รองานที่กำลังวิเคราะห์ให้เสร็จก่อน" : null} onQueued={(id) => { setSelectedId(id); router.refresh(); }} />
|
||||
) : (
|
||||
<Notice tone="blue" title="ดูผลได้อย่างเดียว">ผู้จัดการเพจเท่านั้นที่สั่งวิเคราะห์และวางงบได้</Notice>
|
||||
)}
|
||||
|
||||
<section className="card">
|
||||
<div className="card-head">
|
||||
<h2>ประวัติการวิเคราะห์</h2>
|
||||
</div>
|
||||
{items.length === 0 ? (
|
||||
<EmptyState icon="target" title="ยังไม่เคยวิเคราะห์เพจนี้" hint="กรอกข้อมูลธุรกิจกับงบด้านบน แล้วกดเริ่มวิเคราะห์" />
|
||||
) : (
|
||||
<ul>
|
||||
{items.map((i) => (
|
||||
<li key={i.id}>
|
||||
<button
|
||||
type="button"
|
||||
className="w-full text-left flex items-start gap-3 px-[18px] py-3 border-b border-line-2 hover:bg-board aria-pressed:bg-board"
|
||||
aria-pressed={selected?.id === i.id}
|
||||
onClick={() => setSelectedId(i.id)}
|
||||
>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<b className="num">{baht(i.brief.budgetBaht)}</b>
|
||||
<span className="text-ink-3 text-[13px] num">· {i.brief.days} วัน</span>
|
||||
<span className={`pill ${STATUS_META[i.status].pill}`}>{STATUS_META[i.status].label}</span>
|
||||
</div>
|
||||
<p className="text-[13px] text-ink-2 mt-1 truncate">{GOAL_LABEL[i.brief.goal]} — {i.brief.business}</p>
|
||||
<p className="text-[12px] text-ink-4 mt-0.5">{formatDateTime(i.createdAt)}{i.requestedBy ? ` · ${i.requestedBy}` : ""}</p>
|
||||
</div>
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div className="min-w-0">{selected ? <ResultPanel item={selected} canWrite={canWrite} /> : null}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function BriefForm({
|
||||
pageId,
|
||||
defaults,
|
||||
disabled,
|
||||
disabledReason,
|
||||
onQueued,
|
||||
}: {
|
||||
pageId: string;
|
||||
defaults: { business: string; audience: string };
|
||||
disabled: boolean;
|
||||
disabledReason: string | null;
|
||||
onQueued: (id: string) => void;
|
||||
}) {
|
||||
// Analysis caps these at 1,500 / 1,000 characters; the profile allows longer text.
|
||||
const [business, setBusiness] = useState(defaults.business.slice(0, 1500));
|
||||
const [audience, setAudience] = useState(defaults.audience.slice(0, 1000));
|
||||
const [goal, setGoal] = useState<Goal>("MESSAGES");
|
||||
const [budget, setBudget] = useState("");
|
||||
const [days, setDays] = useState("14");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
async function submit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
setBusy(true);
|
||||
const res = await fetch("/api/analysis", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ pageId, business, audience, goal, budgetBaht: Number(budget.replace(/,/g, "")), days: Number(days) }),
|
||||
}).catch(() => null);
|
||||
const body = res ? await res.json().catch(() => ({})) : {};
|
||||
setBusy(false);
|
||||
if (!res?.ok) return setError(body.error ?? "เริ่มวิเคราะห์ไม่สำเร็จ ลองใหม่อีกครั้ง");
|
||||
onQueued(body.id);
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="card" onSubmit={submit}>
|
||||
<div className="card-head">
|
||||
<h2>สั่งวิเคราะห์ใหม่</h2>
|
||||
</div>
|
||||
<div className="card-body flex flex-col gap-4">
|
||||
<label className="field">
|
||||
<span>ขายอะไร / ธุรกิจอะไร</span>
|
||||
<textarea className="textarea" rows={3} maxLength={1500} required value={business} onChange={(e) => setBusiness(e.target.value)} placeholder="เช่น ร้านเค้กโฮมเมด ส่งในกรุงเทพ ราคา 350–900 บาท" />
|
||||
</label>
|
||||
<label className="field">
|
||||
<span>กลุ่มลูกค้า (ถ้ารู้)</span>
|
||||
<textarea className="textarea" rows={2} maxLength={1000} value={audience} onChange={(e) => setAudience(e.target.value)} placeholder="เช่น ผู้หญิงวัยทำงาน 25–40 ซื้อเป็นของขวัญ" />
|
||||
</label>
|
||||
<div className="field">
|
||||
<span>เป้าหมายโฆษณา</span>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{(Object.keys(GOAL_LABEL) as Goal[]).map((g) => (
|
||||
<button key={g} type="button" className="chip" aria-pressed={goal === g} onClick={() => setGoal(g)}>
|
||||
{GOAL_LABEL[g]}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<label className="field">
|
||||
<span>งบรวมสูงสุด (บาท)</span>
|
||||
<input className="input num" inputMode="numeric" required value={budget} onChange={(e) => setBudget(e.target.value.replace(/[^\d,]/g, ""))} placeholder="5,000" />
|
||||
</label>
|
||||
<label className="field">
|
||||
<span>ระยะเวลา (วัน)</span>
|
||||
<input className="input num" type="number" min={1} max={90} required value={days} onChange={(e) => setDays(e.target.value)} />
|
||||
</label>
|
||||
</div>
|
||||
{error && <Notice tone="red">{error}</Notice>}
|
||||
{disabledReason && <p className="text-[13px] text-ink-3">{disabledReason}</p>}
|
||||
<button type="submit" className="btn btn-primary" disabled={disabled || busy}>
|
||||
<Icon name="target" size={16} /> {busy ? "กำลังดึงโพสต์จากเพจ…" : "เริ่มวิเคราะห์"}
|
||||
</button>
|
||||
<p className="text-[12px] text-ink-4">ใช้เวลาประมาณ 1–5 นาที ปิดหน้านี้ได้ ผลจะอยู่ในประวัติ</p>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
function ResultPanel({ item, canWrite }: { item: AnalysisItem; canWrite: boolean }) {
|
||||
if (item.status === "QUEUED" || item.status === "RUNNING") {
|
||||
return (
|
||||
<section className="card">
|
||||
<EmptyState icon="clock" title={item.status === "QUEUED" ? "รอคิววิเคราะห์" : "กำลังวิเคราะห์โพสต์และวางแผน…"} hint={`อ่านโพสต์ ${numberFmt.format(item.postCount)} โพสต์ หน้านี้จะอัปเดตเองเมื่อเสร็จ`} />
|
||||
</section>
|
||||
);
|
||||
}
|
||||
if (item.status === "FAILED" || !item.report || !item.budget) {
|
||||
return (
|
||||
<section className="card card-body">
|
||||
<Notice tone="red" title="วิเคราะห์ไม่สำเร็จ">{item.error ?? "ไม่มีผลลัพธ์"}</Notice>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
const { report, budget } = item;
|
||||
const spend = budget.testBaht + budget.scaleBaht;
|
||||
const minDaily = Math.min(...budget.daily);
|
||||
const maxDaily = Math.max(...budget.daily);
|
||||
const testDays = Math.max(1, Math.round((item.brief.days * report.budgetSplit.testPercent) / Math.max(1, report.budgetSplit.testPercent + report.budgetSplit.scalePercent)));
|
||||
const limitations = [...item.warnings, ...report.limitations];
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-5">
|
||||
<section className="card">
|
||||
<div className="card-head">
|
||||
<h2>สรุป</h2>
|
||||
<span className="text-[12px] text-ink-4 ml-auto">{item.finishedAt ? formatDateTime(item.finishedAt) : ""}</span>
|
||||
</div>
|
||||
<div className="card-body flex flex-col gap-3">
|
||||
<p className="whitespace-pre-wrap leading-relaxed">{report.summary}</p>
|
||||
<p className="text-[12px] text-ink-4">ข้อเสนอจาก AI จากโพสต์ {numberFmt.format(item.postCount)} โพสต์ — เป็นประมาณการ ตรวจก่อนนำไปใช้</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="card">
|
||||
<div className="card-head">
|
||||
<h2>แบ่งงบ {baht(item.brief.budgetBaht)}</h2>
|
||||
</div>
|
||||
<div className="card-body flex flex-col gap-4">
|
||||
<div className="flex h-3 rounded-full overflow-hidden bg-board" aria-hidden>
|
||||
<span style={{ width: `${report.budgetSplit.testPercent}%`, background: "var(--m-yellow)" }} />
|
||||
<span style={{ width: `${report.budgetSplit.scalePercent}%`, background: "var(--m-green)" }} />
|
||||
<span style={{ width: `${report.budgetSplit.reservePercent}%`, background: "var(--line)" }} />
|
||||
</div>
|
||||
<dl className="grid grid-cols-1 sm:grid-cols-3 gap-3">
|
||||
<BudgetCell label="ทดสอบ" percent={report.budgetSplit.testPercent} amount={budget.testBaht} />
|
||||
<BudgetCell label="ขยายตัวที่ได้ผล" percent={report.budgetSplit.scalePercent} amount={budget.scaleBaht} />
|
||||
<BudgetCell label="สำรอง (ยังไม่ใช้)" percent={report.budgetSplit.reservePercent} amount={budget.reserveBaht} />
|
||||
</dl>
|
||||
<p className="text-[13px] text-ink-2 num">
|
||||
ใช้จริง {baht(spend)} ใน {item.brief.days} วัน ≈ {minDaily === maxDaily ? baht(minDaily) : `${numberFmt.format(minDaily)}–${baht(maxDaily)}`} ต่อวัน · ช่วงทดสอบประมาณ {testDays} วันแรก
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="card">
|
||||
<div className="card-head">
|
||||
<h2>แผนยิง Ads</h2>
|
||||
</div>
|
||||
<dl className="card-body grid gap-4">
|
||||
<Row label="Objective">{report.adsPlan.objective}</Row>
|
||||
<Row label="กลุ่มเป้าหมาย">{report.adsPlan.audience}</Row>
|
||||
<Row label="ชิ้นงานที่ควรทดสอบ">
|
||||
<ul className="list-disc pl-5 flex flex-col gap-1">
|
||||
{report.adsPlan.creatives.map((c, i) => (
|
||||
<li key={i}>{c}</li>
|
||||
))}
|
||||
</ul>
|
||||
</Row>
|
||||
<Row label="วิธีทดสอบ">{report.adsPlan.testPlan}</Row>
|
||||
<Row label="เมื่อไหร่ควรเพิ่มงบ">{report.adsPlan.scaleRule}</Row>
|
||||
<Row label="เมื่อไหร่ควรหยุด">{report.adsPlan.stopRule}</Row>
|
||||
</dl>
|
||||
</section>
|
||||
|
||||
<section className="card">
|
||||
<div className="card-head">
|
||||
<h2>สิ่งที่เห็นจากคอนเทนต์</h2>
|
||||
</div>
|
||||
<ul>
|
||||
{report.contentFindings.map((f, i) => (
|
||||
<li key={i} className="px-[18px] py-3.5 border-b border-line-2 last:border-0">
|
||||
<b>{f.finding}</b>
|
||||
<p className="text-[13px] text-ink-3 mt-1">หลักฐาน: {f.evidence}</p>
|
||||
<p className="text-[13px] text-ink-2 mt-1">
|
||||
<Icon name="right" size={13} className="inline -mt-0.5" /> {f.action}
|
||||
</p>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section className="card">
|
||||
<div className="card-head">
|
||||
<h2>ไอเดียโพสต์ถัดไป</h2>
|
||||
</div>
|
||||
<div className="card-body grid gap-3 grid-cols-1 md:grid-cols-2">
|
||||
{report.contentIdeas.map((idea, i) => (
|
||||
<article key={i} className="rounded-lg border border-line p-3.5 flex flex-col gap-1.5">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<b>{idea.title}</b>
|
||||
<span className="pill pill-neutral">{idea.format}</span>
|
||||
</div>
|
||||
<p className="text-[13px] text-ink-2">Hook: {idea.hook}</p>
|
||||
<p className="text-[13px] text-ink-3">CTA: {idea.cta}</p>
|
||||
{canWrite && (
|
||||
<Link href={`/posts/new?analysis=${item.id}&idea=${i}`} className="btn btn-ghost btn-sm self-start mt-1">
|
||||
<Icon name="sparkle" size={14} /> ใช้ไอเดียนี้
|
||||
</Link>
|
||||
)}
|
||||
</article>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="card">
|
||||
<div className="card-head">
|
||||
<h2>ตัวเลขที่ต้องติดตาม</h2>
|
||||
</div>
|
||||
<div className="card-body flex flex-wrap gap-2">
|
||||
{report.metricsToTrack.map((m, i) => (
|
||||
<span key={i} className="pill pill-blue">
|
||||
{m}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{limitations.length > 0 && (
|
||||
<Notice tone="amber" title="ข้อจำกัดของผลนี้">
|
||||
<ul className="list-disc pl-5 mt-1 flex flex-col gap-0.5">
|
||||
{limitations.map((l, i) => (
|
||||
<li key={i}>{l}</li>
|
||||
))}
|
||||
</ul>
|
||||
</Notice>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function BudgetCell({ label, percent, amount }: { label: string; percent: number; amount: number }) {
|
||||
return (
|
||||
<div className="rounded-lg border border-line p-3">
|
||||
<dt className="text-[12px] text-ink-3">
|
||||
{label} <span className="num">{percent}%</span>
|
||||
</dt>
|
||||
<dd className="text-[18px] font-semibold num mt-0.5">{baht(amount)}</dd>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Row({ label, children }: { label: string; children: React.ReactNode }) {
|
||||
return (
|
||||
<div className="grid gap-1 sm:grid-cols-[150px_minmax(0,1fr)]">
|
||||
<dt className="text-[13px] text-ink-3">{label}</dt>
|
||||
<dd className="text-[14px] whitespace-pre-wrap">{children}</dd>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user