4c93bf0a11
Adds PageProfile.visualStyle (with migration), feeds it into every AI prompt route, and samples a logo palette for image generation. Also hardens Chrome focus handling and image copy in the ChatGPT driver. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
116 lines
5.1 KiB
TypeScript
116 lines
5.1 KiB
TypeScript
"use client";
|
|
import { withBase } from "@/lib/base-path";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { Icon } from "./Icon";
|
|
import { formatDateTime, Notice } from "./ui";
|
|
import { useToast } from "./Toast";
|
|
import { PROFILE_FIELDS, type ShopProfile } from "@/lib/ai/shared";
|
|
|
|
const EMPTY: ShopProfile = { business: "", audience: "", tone: "", visualStyle: "", avoid: "", cta: "" };
|
|
|
|
export function ProfileForm({
|
|
pageId,
|
|
canEdit,
|
|
canDraft,
|
|
initial,
|
|
updatedAt,
|
|
}: {
|
|
pageId: string;
|
|
canEdit: boolean;
|
|
canDraft: boolean;
|
|
initial: ShopProfile | null;
|
|
updatedAt: string | null;
|
|
}) {
|
|
const router = useRouter();
|
|
const toast = useToast();
|
|
const [values, setValues] = useState<ShopProfile>(initial ?? EMPTY);
|
|
const [busy, setBusy] = useState<"save" | "draft" | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
async function request(path: string, method: string, body?: ShopProfile) {
|
|
const res = await fetch(withBase(path), { method, headers: { "Content-Type": "application/json" }, body: body ? JSON.stringify(body) : undefined }).catch(() => null);
|
|
const json = res ? await res.json().catch(() => ({})) : {};
|
|
return { ok: Boolean(res?.ok), json };
|
|
}
|
|
|
|
async function save(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setBusy("save");
|
|
setError(null);
|
|
const { ok, json } = await request(`/api/pages/${pageId}/profile`, "PUT", values);
|
|
setBusy(null);
|
|
if (!ok) return setError(json.error ?? "บันทึกไม่สำเร็จ");
|
|
toast.show("บันทึกโปรไฟล์ร้านแล้ว");
|
|
router.refresh();
|
|
}
|
|
|
|
async function draft() {
|
|
setBusy("draft");
|
|
setError(null);
|
|
const { ok, json } = await request(`/api/pages/${pageId}/profile/draft`, "POST");
|
|
setBusy(null);
|
|
if (!ok) return setError(json.error ?? "AI ร่างโปรไฟล์ไม่สำเร็จ");
|
|
const drafted = json.profile as ShopProfile;
|
|
// Keep what the owner already typed where the AI found nothing.
|
|
setValues((cur) => {
|
|
const next = { ...cur };
|
|
for (const f of PROFILE_FIELDS) if (drafted[f.key]) next[f.key] = drafted[f.key];
|
|
return next;
|
|
});
|
|
toast.show(`AI ร่างจาก ${json.postCount} โพสต์แล้ว — ตรวจแล้วกดบันทึก`, "warn");
|
|
}
|
|
|
|
return (
|
|
<form className="card max-w-[780px]" onSubmit={save}>
|
|
<div className="card-head">
|
|
<h2>ข้อมูลร้าน</h2>
|
|
{updatedAt && <span className="text-[12px] text-ink-4 ml-auto">บันทึกล่าสุด {formatDateTime(updatedAt)}</span>}
|
|
</div>
|
|
<div className="card-body flex flex-col gap-4">
|
|
{!canEdit && <Notice tone="blue" title="ดูได้อย่างเดียว">ผู้อนุมัติขึ้นไปเท่านั้นที่แก้โปรไฟล์ร้านได้</Notice>}
|
|
{canEdit && canDraft && (
|
|
<div className="flex flex-wrap items-center gap-3 rounded-[12px] border border-line bg-board p-3.5">
|
|
<Icon name="sparkle" size={18} />
|
|
<p className="flex-1 min-w-[220px] text-[13px] text-ink-2">ยังไม่รู้จะเขียนอะไร? ให้ AI อ่านโพสต์ 90 วันล่าสุดของเพจแล้วร่างให้ก่อน</p>
|
|
<button type="button" className="btn btn-ghost btn-sm" disabled={busy !== null} onClick={draft}>
|
|
{busy === "draft" ? "AI กำลังอ่านโพสต์…" : "ให้ AI ร่างจากโพสต์เดิม"}
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{PROFILE_FIELDS.map((f) => (
|
|
<label key={f.key} className="field">
|
|
<span className="flex">
|
|
{f.label}
|
|
{f.key === "business" && <span className="text-danger ml-1">*</span>}
|
|
<span className="ml-auto num text-ink-4">{values[f.key].length.toLocaleString("th-TH")}/{f.max.toLocaleString("th-TH")}</span>
|
|
</span>
|
|
<textarea
|
|
className="textarea"
|
|
rows={f.rows}
|
|
maxLength={f.max}
|
|
required={f.key === "business"}
|
|
disabled={!canEdit}
|
|
value={values[f.key]}
|
|
onChange={(e) => setValues({ ...values, [f.key]: e.target.value })}
|
|
placeholder={f.hint}
|
|
/>
|
|
</label>
|
|
))}
|
|
|
|
{error && <Notice tone="red">{error}</Notice>}
|
|
{canEdit && (
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
<button type="submit" className="btn btn-primary" disabled={busy !== null}>
|
|
{busy === "save" ? "กำลังบันทึก…" : "บันทึกโปรไฟล์"}
|
|
</button>
|
|
<p className="text-[12px] text-ink-4 flex-1 min-w-[220px]">ข้อมูลนี้และโพสต์ของเพจจะถูกส่งไปให้ Codex ประมวลผลตอนใช้ผู้ช่วย AI</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|