"use client"; import { withBase } from "@/lib/base-path"; import { useState } from "react"; import { Icon } from "./Icon"; import { Notice } from "./ui"; interface Issue { level: "high" | "medium" | "low"; problem: string; suggestion: string; } const LEVEL_META: Record = { high: { label: "ต้องแก้", pill: "pill-red" }, medium: { label: "ควรแก้", pill: "pill-amber" }, low: { label: "เล็กน้อย", pill: "pill-neutral" }, }; // Square-bracket gaps the AI leaves for facts it must not invent, e.g. [ราคา]. const PLACEHOLDER = /\[[^\]\n]{1,40}\]/; /** Pre-approval review of the editor's current caption. Advisory only — it never blocks approval. */ export function PostCheck({ pageId, caption, targets, hasImage }: { pageId: string; caption: string; targets: string[]; hasImage: boolean }) { const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [result, setResult] = useState<{ caption: string; summary: string; issues: Issue[] } | null>(null); async function run() { setBusy(true); setError(null); const res = await fetch(withBase("/api/ai/check"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pageId, caption, targets, hasImage }), }).catch(() => null); const body = res ? await res.json().catch(() => ({})) : {}; setBusy(false); if (!res?.ok) return setError(body.error ?? "AI ตรวจไม่สำเร็จ ลองใหม่อีกครั้ง"); setResult({ caption, summary: body.summary ?? "", issues: body.issues ?? [] }); } const stale = result !== null && result.caption !== caption; return (
{PLACEHOLDER.test(caption) && (

ยังมีช่องในวงเล็บ [ ] ที่ต้องเติมก่อนเผยแพร่

)}
{stale && ข้อความเปลี่ยนหลังตรวจ — ตรวจใหม่เพื่อดูผลล่าสุด}
{error && {error}} {result && !stale && (

{result.issues.length === 0 && } {result.summary || (result.issues.length ? "พบจุดที่ควรดู" : "ไม่พบปัญหา")}

{result.issues.length > 0 && (
    {result.issues.map((i, n) => (
  • {LEVEL_META[i.level].label} {i.problem} {i.suggestion && แก้: {i.suggestion}}
  • ))}
)}

AI ช่วยดูเบื้องต้นเท่านั้น ผู้อนุมัติยังต้องตรวจเอง

)}
); }