Add review window and manual content generation

This commit is contained in:
Loma Bot Ops
2026-11-27 13:34:41 +07:00
parent 9d5bfe7d9d
commit 8086197f58
16 changed files with 357 additions and 62 deletions
+26 -9
View File
@@ -17,7 +17,7 @@ const DEFAULT_SETTINGS: Settings = { enabled: false, scheduleMode: "FIXED", week
export function ContentAutoForm({ pageId }: { pageId: string }) {
const [settings, setSettings] = useState<Settings>(DEFAULT_SETTINGS);
const [loaded, setLoaded] = useState(false);
const [busy, setBusy] = useState(false);
const [busy, setBusy] = useState<"save" | "run" | null>(null);
const [error, setError] = useState("");
const [message, setMessage] = useState("");
useEffect(() => {
@@ -43,21 +43,34 @@ export function ContentAutoForm({ pageId }: { pageId: string }) {
if (!loaded || busy) return;
if (mixTotal !== 100) { setError(`สัดส่วนเนื้อหารวม ${mixTotal}% ต้องปรับให้ครบ 100%`); return; }
if (settings.weekdays.length === 0 || settings.targets.length === 0) { setError("เลือกวันและช่องทางอย่างน้อย 1 รายการ"); return; }
setBusy(true); setError(""); setMessage("");
setBusy("save"); setError(""); setMessage("");
try {
const response = await fetch(withBase("/api/ai/content-auto"), { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pageId, ...settings }) });
const data = await response.json();
if (!response.ok) throw new Error(data.error || "บันทึก auto ไม่สำเร็จ");
setSettings(data.automation);
setMessage(data.automation.enabled ? "บันทึกแล้ว จะสร้างฉบับร่างตามวันและเวลาที่เลือก" : "ปิด auto แล้ว งานที่เริ่มสร้างไปแล้วจะทำจนจบ");
setMessage(data.automation.enabled ? "บันทึกแล้ว ระบบจะสร้างร่างเวลา 03:00 ล่วงหน้า 1 วัน และนำขึ้นตารางเวลา 08:00" : "ปิด auto แล้ว งานที่เริ่มสร้างไปแล้วจะทำจนจบ");
} catch (failure) { setError(failure instanceof Error ? failure.message : "เชื่อมต่อไม่ได้"); }
finally { setBusy(false); }
finally { setBusy(null); }
}
async function runNow() {
if (!loaded || busy || !settings.enabled) return;
setBusy("run"); setError(""); setMessage("");
try {
const response = await fetch(withBase("/api/ai/content-auto"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pageId }) });
const data = await response.json();
if (!response.ok) throw new Error(data.error || "สร้างเนื้อหาตอนนี้ไม่สำเร็จ");
setSettings((current) => ({ ...current, lastQueuedAt: new Date().toISOString() }));
setMessage(`เข้าคิวสร้างเนื้อหาแล้ว ${data.queued} งาน ดูความคืบหน้าได้ในงานล่าสุดด้านล่าง`);
} catch (failure) { setError(failure instanceof Error ? failure.message : "เชื่อมต่อไม่ได้"); }
finally { setBusy(null); }
}
return <section className="flex flex-col gap-4" aria-label="วางเนื้อหาอัตโนมัติ">
<div><h3 className="font-semibold text-[18px]">วางเนื้อหาอัตโนมัติ</h3><p className="text-[13px] text-ink-2 mt-1">AI จะสร้างโพสต์เป็นฉบับร่างตามแผน ทุกชิ้นยังต้องผ่านการอนุมัติก่อนเผยแพร่</p></div>
<div><h3 className="font-semibold text-[18px]">วางเนื้อหาอัตโนมัติ</h3><p className="text-[13px] text-ink-2 mt-1">ระบบสร้างฉบับร่างเวลา 03:00 ล่วงหน้า 1 วัน เพื่อให้ตรวจเนื้อหาก่อน และนำขึ้นตารางเวลา 08:00 โดยทุกชิ้นยังต้องผ่านการอนุมัติก่อนเผยแพร่</p></div>
<form onSubmit={save} noValidate className="flex flex-col gap-5">
<fieldset disabled={!loaded || busy} className="contents">
<fieldset disabled={!loaded || busy !== null} className="contents">
<label className="flex items-start gap-3 rounded-xl border border-line bg-blue-soft p-3 text-[14px] font-medium cursor-pointer">
<input className="mt-0.5" type="checkbox" checked={settings.enabled} onChange={(e) => setSettings({ ...settings, enabled: e.target.checked })} />
<span><b className="block">เปิดใช้สำหรับเพจนี้</b><span className="text-[12px] text-ink-2 font-normal">ระบบทำงานตามแผนได้แม้ปิดหน้าเว็บ</span></span>
@@ -67,7 +80,7 @@ export function ContentAutoForm({ pageId }: { pageId: string }) {
{DAYS.map((label, day) => <label key={day} className={`min-h-10 rounded-lg border grid place-items-center text-[13px] cursor-pointer ${settings.weekdays.includes(day) ? "border-blue bg-blue-soft text-blue-ink font-semibold" : "border-line bg-paper text-ink-2"}`}><input className="sr-only" type="checkbox" checked={settings.weekdays.includes(day)} onChange={(e) => setSettings({ ...settings, weekdays: e.target.checked ? [...settings.weekdays, day] : settings.weekdays.filter((d) => d !== day) })} />{label}</label>)}
</div></fieldset>
<fieldset><legend className="font-semibold text-[14px] mb-2">2. กำหนดเวลา</legend><div className="grid sm:grid-cols-2 gap-2">
<fieldset><legend className="font-semibold text-[14px] mb-2">2. กำหนดเวลาเผยแพร่ตามแผน</legend><p className="text-[12px] text-ink-2 mb-2">เวลานี้ใช้วางโพสต์บนตารางของวันถัดไป ไม่ใช่เวลาสร้างฉบับร่าง</p><div className="grid sm:grid-cols-2 gap-2">
{(["FIXED", "RANDOM"] as const).map((mode) => <label key={mode} className={`rounded-xl border p-3 flex gap-2 cursor-pointer ${settings.scheduleMode === mode ? "border-blue bg-blue-soft" : "border-line"}`}><input type="radio" name="scheduleMode" value={mode} checked={settings.scheduleMode === mode} onChange={() => setSettings({ ...settings, scheduleMode: mode })} /><span><b className="block text-[13px]">{mode === "FIXED" ? "ระบุเวลาเอง" : "สุ่มเวลา"}</b><span className="text-[12px] text-ink-2">{mode === "FIXED" ? "เลือกเวลาแต่ละรอบได้ตรง ๆ" : "สุ่มช่วง 08:00–20:00 น."}</span></span></label>)}
</div></fieldset>
@@ -82,10 +95,14 @@ export function ContentAutoForm({ pageId }: { pageId: string }) {
<label className="field"><span>คำแนะนำเพิ่มเติม (ไม่บังคับ)</span><textarea className="textarea" rows={2} value={settings.brief} onChange={(e) => setSettings({ ...settings, brief: e.target.value })} placeholder="เช่น เน้นสินค้ากลุ่มใหม่ ใช้น้ำเสียงเป็นกันเอง" /></label>
<fieldset className="text-[13px]"><legend className="font-semibold text-[14px] mb-2">4. ช่องทางของฉบับร่าง</legend><div className="flex flex-wrap gap-4">{(["FACEBOOK", "INSTAGRAM"] as const).map((target) => <label key={target} className="flex items-center gap-2 min-h-10 cursor-pointer"><input type="checkbox" checked={settings.targets.includes(target)} onChange={(e) => setSettings({ ...settings, targets: e.target.checked ? [...settings.targets, target] : settings.targets.filter((t) => t !== target) })} />{target === "FACEBOOK" ? "Facebook" : "Instagram"}</label>)}</div></fieldset>
<button type="submit" className="btn btn-primary self-start" disabled={!loaded || busy}>{busy ? "กำลังบันทึก…" : "บันทึกแผนอัตโนมัติ"}</button>
<div className="flex flex-wrap gap-2">
<button type="submit" className="btn btn-primary" disabled={!loaded || busy !== null}>{busy === "save" ? "กำลังบันทึก…" : "บันทึกแผนอัตโนมัติ"}</button>
<button type="button" className="btn btn-ghost" disabled={!loaded || busy !== null || !settings.enabled} onClick={runNow}>{busy === "run" ? "กำลังเข้าคิว…" : "สร้างเนื้อหาตอนนี้"}</button>
</div>
<p className="text-[12px] text-ink-2 -mt-3">ปุ่มสร้างตอนนี้ใช้ค่าที่บันทึกล่าสุด เหมาะสำหรับทดสอบโดยไม่ต้องรอเวลา 03:00</p>
</fieldset>
</form>
{settings.enabled && settings.nextRunAt && <p className="text-[12px] text-ink-2">รอบถัดไป: {formatTime(settings.nextRunAt)} · เวลาเริ่มเข้าคิว ไม่ใช่เวลาเผยแพร่</p>}
{settings.enabled && settings.nextRunAt && <p className="text-[12px] text-ink-2">สร้างร่างรอบถัดไป: {formatTime(settings.nextRunAt)} · ร่างจะขึ้นตารางตั้งแต่ 08:00 ของวันเดียวกัน</p>}
{settings.lastQueuedAt && <p className="text-[12px] text-ink-2">เข้าคิวล่าสุด: {formatTime(settings.lastQueuedAt)} · ดูผลในงานล่าสุดด้านล่าง</p>}
<p className="text-[12px] text-ink-2">ต้องเปิด worker และ login บัญชีสร้างภาพไว้ งานเริ่มตามคิว หาก worker หยุดจะรับเพียงรอบล่าสุดเมื่อกลับมา</p>
{settings.lastError && <Notice tone="red">{settings.lastError}</Notice>}
+20 -6
View File
@@ -11,6 +11,19 @@ import { IMAGE_PROMPT_MAX, IMAGE_SIZES, type ImageSizeKey } from "@/lib/ai/image
type OverlayPos = "top" | "center" | "bottom";
let sarabunFontPromise: Promise<FontFace> | null = null;
function loadSarabunFont() {
if (!sarabunFontPromise) {
const font = new FontFace("Sarabun", `url(${withBase("/fonts/sarabun/Sarabun-Bold.ttf")})`, { weight: "700" });
sarabunFontPromise = font.load().then((loaded) => {
document.fonts.add(loaded);
return loaded;
});
}
return sarabunFontPromise;
}
function wrapText(ctx: CanvasRenderingContext2D, text: string, maxWidth: number): string[] {
const lines: string[] = [];
const paragraphs = text.split("\n");
@@ -34,7 +47,8 @@ function wrapText(ctx: CanvasRenderingContext2D, text: string, maxWidth: number)
function buildComposite(imgUrl: string, text: string, pos: OverlayPos): Promise<string> {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
img.onload = async () => {
try { await loadSarabunFont(); } catch { /* Canvas will use the Thai fallback font. */ }
const canvas = document.createElement("canvas");
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
@@ -57,15 +71,15 @@ function buildComposite(imgUrl: string, text: string, pos: OverlayPos): Promise<
const grad = ctx.createLinearGradient(0, zoneY, 0, zoneY + zoneH);
if (pos === "top") {
grad.addColorStop(0, "rgba(0,0,0,0.75)");
grad.addColorStop(0, "rgba(0,0,0,0.48)");
grad.addColorStop(1, "rgba(0,0,0,0)");
} else if (pos === "bottom") {
grad.addColorStop(0, "rgba(0,0,0,0)");
grad.addColorStop(1, "rgba(0,0,0,0.75)");
grad.addColorStop(1, "rgba(0,0,0,0.48)");
} else {
grad.addColorStop(0, "rgba(0,0,0,0)");
grad.addColorStop(0.15, "rgba(0,0,0,0.65)");
grad.addColorStop(0.85, "rgba(0,0,0,0.65)");
grad.addColorStop(0.15, "rgba(0,0,0,0.42)");
grad.addColorStop(0.85, "rgba(0,0,0,0.42)");
grad.addColorStop(1, "rgba(0,0,0,0)");
}
ctx.fillStyle = grad;
@@ -73,7 +87,7 @@ function buildComposite(imgUrl: string, text: string, pos: OverlayPos): Promise<
ctx.fillStyle = "#ffffff";
ctx.textAlign = "center";
ctx.shadowColor = "rgba(0,0,0,0.55)";
ctx.shadowColor = "rgba(0,0,0,0.35)";
ctx.shadowBlur = fontSize * 0.28;
ctx.shadowOffsetY = 2;