Add scheduled automatic content drafts with Codex and image automation
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { withBase } from "@/lib/base-path";
|
||||
import { Notice } from "./ui";
|
||||
|
||||
type Settings = {
|
||||
enabled: boolean; weekdays: number[]; times: string[]; brief: string;
|
||||
targets: ("FACEBOOK" | "INSTAGRAM")[];
|
||||
nextRunAt: string | null; lastQueuedAt: string | null; lastError: string | null;
|
||||
};
|
||||
const DAYS = ["อา.", "จ.", "อ.", "พ.", "พฤ.", "ศ.", "ส."];
|
||||
const formatTime = (value: string) => new Date(value).toLocaleString("th-TH", { timeZone: "Asia/Bangkok", dateStyle: "medium", timeStyle: "short" });
|
||||
|
||||
export function ContentAutoForm({ pageId }: { pageId: string }) {
|
||||
const [settings, setSettings] = useState<Settings>({ enabled: false, weekdays: [1, 2, 3, 4, 5], times: ["09:00"], brief: "", targets: ["FACEBOOK"], nextRunAt: null, lastQueuedAt: null, lastError: null });
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const [message, setMessage] = useState("");
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
void (async () => {
|
||||
try {
|
||||
const response = await fetch(withBase(`/api/ai/content-auto?pageId=${encodeURIComponent(pageId)}`), { signal: controller.signal, cache: "no-store" });
|
||||
const data = await response.json();
|
||||
if (!response.ok) throw new Error(data.error || "โหลดการตั้งค่า auto ไม่สำเร็จ");
|
||||
if (data.automation) setSettings(data.automation);
|
||||
setLoaded(true);
|
||||
} catch (failure) {
|
||||
if (!controller.signal.aborted) setError(failure instanceof Error ? failure.message : "เชื่อมต่อไม่ได้");
|
||||
}
|
||||
})();
|
||||
return () => controller.abort();
|
||||
}, [pageId]);
|
||||
|
||||
async function save() {
|
||||
if (!loaded || busy) return;
|
||||
setBusy(true); 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 แล้ว งานที่เริ่มสร้างไปแล้วจะทำจนจบ");
|
||||
} catch (failure) { setError(failure instanceof Error ? failure.message : "เชื่อมต่อไม่ได้"); }
|
||||
finally { setBusy(false); }
|
||||
}
|
||||
|
||||
return <section className="border border-line rounded-xl p-4 flex flex-col gap-3 bg-paper" aria-label="สร้างเนื้อหาอัตโนมัติ">
|
||||
<h3 className="font-semibold text-[15px]">โหมด auto สร้างเนื้อหา</h3>
|
||||
<p className="text-[13px] text-ink-2">เลือกวันและเวลา ให้ AI คิดหัวข้อจากข้อมูลร้าน เขียนเนื้อหาและสร้างภาพ บันทึกเป็นฉบับร่างรออนุมัติ ปิดหน้าเว็บได้</p>
|
||||
<fieldset disabled={!loaded || busy} className="flex flex-col gap-3">
|
||||
<label className="flex items-center gap-2 text-[14px] font-medium">
|
||||
<input type="checkbox" role="switch" checked={settings.enabled} onChange={(e) => setSettings({ ...settings, enabled: e.target.checked })} /> เปิดสร้างอัตโนมัติสำหรับเพจนี้
|
||||
</label>
|
||||
<fieldset className="flex flex-wrap gap-3">
|
||||
<legend className="text-[13px] mb-2">วันสร้างเนื้อหา (เวลาไทย)</legend>
|
||||
{DAYS.map((label, day) => <label key={day} className="flex items-center gap-1.5 text-[13px] min-h-10">
|
||||
<input 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>)}
|
||||
</fieldset>
|
||||
<div className="flex flex-col gap-2">
|
||||
{settings.times.map((time, index) => <div key={index} className="flex flex-wrap items-end gap-2">
|
||||
<label className="field"><span>เวลาสร้างรอบที่ {index + 1}</span><input className="input" type="time" required value={time} onChange={(e) => setSettings({ ...settings, times: settings.times.map((t, i) => i === index ? e.target.value : t) })} /></label>
|
||||
{settings.times.length > 1 && <button type="button" className="btn btn-ghost btn-sm" aria-label={`ลบเวลารอบที่ ${index + 1}`} onClick={() => setSettings({ ...settings, times: settings.times.filter((_, i) => i !== index) })}>ลบเวลา</button>}
|
||||
</div>)}
|
||||
{settings.times.length < 3 && <button type="button" className="btn btn-ghost btn-sm self-start" onClick={() => setSettings({ ...settings, times: [...settings.times, "12:00"] })}>เพิ่มเวลา (สูงสุด 3 รอบต่อวัน)</button>}
|
||||
</div>
|
||||
<label className="field"><span>แนวเนื้อหา (เว้นว่างให้ AI เลือกจากข้อมูลร้าน)</span><textarea className="textarea" rows={2} maxLength={1000} value={settings.brief} onChange={(e) => setSettings({ ...settings, brief: e.target.value })} placeholder="เช่น สลับแนะนำสินค้า เคล็ดลับ และเรื่องราวของร้าน" /></label>
|
||||
<fieldset className="flex flex-wrap gap-3 text-[13px]">
|
||||
<legend className="mb-2">ช่องทางของฉบับร่าง auto</legend>
|
||||
{(["FACEBOOK", "INSTAGRAM"] as const).map((target) => <label key={target} className="flex items-center gap-1.5 min-h-10"><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>)}
|
||||
</fieldset>
|
||||
<button type="button" className="btn btn-primary btn-sm self-start" onClick={save}>{busy ? "กำลังบันทึก…" : "บันทึกโหมด auto"}</button>
|
||||
</fieldset>
|
||||
{settings.enabled && settings.nextRunAt && <p className="text-[12px] text-ink-2">รอบถัดไป: {formatTime(settings.nextRunAt)} · เวลาเริ่มเข้าคิว ไม่ใช่เวลาเผยแพร่</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>}
|
||||
{error && <Notice tone="red">{error}</Notice>}
|
||||
{message && <p role="status" className="text-[13px] text-ink-2">{message}</p>}
|
||||
</section>;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { withBase } from "@/lib/base-path";
|
||||
import { POST_JOB_STAGE, POST_JOB_STATUS, postJobProgress, type PostJobView } from "@/lib/ai/post-queue-shared";
|
||||
import { Icon } from "./Icon";
|
||||
import { Notice } from "./ui";
|
||||
import { ContentAutoForm } from "./ContentAutoForm";
|
||||
|
||||
function duration(job: PostJobView): string {
|
||||
if (!job.finishedAt) return "";
|
||||
@@ -81,6 +82,7 @@ export function PostGenerationQueue({ pageId, targets, plannedDay, initialTopic
|
||||
|
||||
return (
|
||||
<section aria-label="คิวสร้างโพสต์ด้วย AI" className="border border-line rounded-xl p-4 flex flex-col gap-3 bg-board">
|
||||
<ContentAutoForm key={pageId} pageId={pageId} />
|
||||
<h3 className="font-semibold text-[15px]">สร้างทั้งโพสต์ด้วย AI</h3>
|
||||
<p className="text-[13px] text-ink-2">ให้ Codex เขียนเนื้อหาและ ChatGPT สร้างภาพ แล้วบันทึกเป็นฉบับร่างรอคุณตรวจ ปิดหน้านี้แล้วกลับมาดูคิวได้</p>
|
||||
<label className="field">
|
||||
|
||||
Reference in New Issue
Block a user