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,132 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import Link from "next/link";
|
||||
import { Icon } from "./Icon";
|
||||
import { Notice } from "./ui";
|
||||
import { CAPTION_REFINEMENTS, type CaptionRefinement } from "@/lib/ai/shared";
|
||||
|
||||
interface Variant {
|
||||
angle: string;
|
||||
caption: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drafts or rewrites the caption with Claude. The chosen text only fills the
|
||||
* editor — saving and approval stay exactly as for hand-written posts.
|
||||
*/
|
||||
export function CaptionAssistant({
|
||||
pageId,
|
||||
targets,
|
||||
caption,
|
||||
initialTopic,
|
||||
onChange,
|
||||
}: {
|
||||
pageId: string;
|
||||
targets: string[];
|
||||
caption: string;
|
||||
initialTopic?: string;
|
||||
onChange: (text: string, fromAi: boolean) => void;
|
||||
}) {
|
||||
const [open, setOpen] = useState(Boolean(initialTopic));
|
||||
const [topic, setTopic] = useState(initialTopic ?? "");
|
||||
const [variants, setVariants] = useState<Variant[]>([]);
|
||||
const [busy, setBusy] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [profileMissing, setProfileMissing] = useState(false);
|
||||
const [previous, setPrevious] = useState<string | null>(null);
|
||||
|
||||
async function run(input: { topic: string } | { refine: CaptionRefinement }) {
|
||||
const label = "refine" in input ? input.refine : "draft";
|
||||
setBusy(label);
|
||||
setError(null);
|
||||
const res = await fetch("/api/ai/caption", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ pageId, targets, caption, ...input }),
|
||||
}).catch(() => null);
|
||||
const body = res ? await res.json().catch(() => ({})) : {};
|
||||
setBusy(null);
|
||||
if (!res?.ok) return setError(body.error ?? "AI ทำงานไม่สำเร็จ ลองใหม่อีกครั้ง");
|
||||
setVariants(body.variants ?? []);
|
||||
setProfileMissing(Boolean(body.profileMissing));
|
||||
}
|
||||
|
||||
function use(v: Variant) {
|
||||
setPrevious(caption);
|
||||
onChange(v.caption, true);
|
||||
setVariants([]);
|
||||
}
|
||||
|
||||
if (!open) {
|
||||
return (
|
||||
<button type="button" className="btn btn-ghost btn-sm self-start" onClick={() => setOpen(true)}>
|
||||
<Icon name="sparkle" size={15} /> ให้ AI ช่วยเขียน caption
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="rounded-[12px] border border-line bg-board p-3.5 flex flex-col gap-3" aria-label="ผู้ช่วยเขียน caption">
|
||||
<div className="flex items-center gap-2">
|
||||
<Icon name="sparkle" size={16} />
|
||||
<b className="text-[14px]">ให้ AI ช่วยเขียน</b>
|
||||
<button type="button" className="btn btn-ghost btn-sm ml-auto" onClick={() => setOpen(false)} aria-label="ปิดผู้ช่วย AI">
|
||||
<Icon name="x" size={14} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<label className="field">
|
||||
<span>อยากโพสต์เรื่องอะไร</span>
|
||||
<textarea className="textarea" rows={2} maxLength={1000} value={topic} onChange={(e) => setTopic(e.target.value)} placeholder="เช่น โปรเค้กวันแม่ ลด 15% ถึง 12 ส.ค. สั่งล่วงหน้า 2 วัน" />
|
||||
</label>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<button type="button" className="btn btn-primary btn-sm" disabled={busy !== null || !topic.trim()} onClick={() => run({ topic })}>
|
||||
<Icon name="sparkle" size={14} /> {busy === "draft" ? "AI กำลังเขียน…" : "เขียนให้ 3 แบบ"}
|
||||
</button>
|
||||
{caption.trim() && (
|
||||
<>
|
||||
<span className="text-[12px] text-ink-3 ml-1">ปรับข้อความที่มี:</span>
|
||||
{(Object.keys(CAPTION_REFINEMENTS) as CaptionRefinement[]).map((k) => (
|
||||
<button key={k} type="button" className="chip" disabled={busy !== null} onClick={() => run({ refine: k })}>
|
||||
{busy === k ? "กำลังปรับ…" : CAPTION_REFINEMENTS[k].label}
|
||||
</button>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{error && <Notice tone="red">{error}</Notice>}
|
||||
{profileMissing && (
|
||||
<p className="text-[12px] text-ink-3">
|
||||
ยังไม่มีโปรไฟล์ร้าน — <Link href="/profile" className="underline">ตั้งไว้</Link> แล้ว AI จะเขียนตรงกับร้านมากขึ้น
|
||||
</p>
|
||||
)}
|
||||
{previous !== null && variants.length === 0 && (
|
||||
<p className="text-[12px] text-ink-3">
|
||||
ใส่ข้อความจาก AI แล้ว ตรวจและแก้ได้ตามต้องการ ·{" "}
|
||||
<button type="button" className="underline" onClick={() => { onChange(previous, false); setPrevious(null); }}>
|
||||
เอาข้อความเดิมคืน
|
||||
</button>
|
||||
</p>
|
||||
)}
|
||||
|
||||
{variants.length > 0 && (
|
||||
<ul className="flex flex-col gap-2">
|
||||
{variants.map((v, i) => (
|
||||
<li key={i} className="rounded-[10px] border border-line bg-paper p-3 flex flex-col gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="pill pill-blue">{v.angle || `แบบที่ ${i + 1}`}</span>
|
||||
<button type="button" className="btn btn-primary btn-sm ml-auto" onClick={() => use(v)}>
|
||||
{caption.trim() ? "แทนที่ด้วยแบบนี้" : "ใช้แบบนี้"}
|
||||
</button>
|
||||
</div>
|
||||
<p className="text-[14px] whitespace-pre-wrap break-words">{v.caption}</p>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<p className="text-[11px] text-ink-4">AI ร่างเท่านั้น — ตรวจราคาและข้อมูลในวงเล็บ [ ] ก่อนส่งอนุมัติ</p>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user