4a673ebb28
Adds basePath config, withBase/appUrl helpers, base-path-aware cookies, OAuth redirects, manifest, service worker and cron runner. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
4.0 KiB
TypeScript
83 lines
4.0 KiB
TypeScript
"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<Issue["level"], { label: string; pill: string }> = {
|
|
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<string | null>(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 (
|
|
<div className="flex flex-col gap-2">
|
|
{PLACEHOLDER.test(caption) && (
|
|
<p className="text-[12px] text-danger flex items-center gap-1.5">
|
|
<Icon name="alert" size={14} /> ยังมีช่องในวงเล็บ [ ] ที่ต้องเติมก่อนเผยแพร่
|
|
</p>
|
|
)}
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<button type="button" className="btn btn-ghost btn-sm" disabled={busy || !caption.trim()} onClick={run}>
|
|
<Icon name="shield" size={14} /> {busy ? "AI กำลังตรวจ…" : result ? "ตรวจอีกครั้ง" : "ให้ AI ตรวจก่อนส่ง"}
|
|
</button>
|
|
{stale && <span className="text-[12px] text-ink-3">ข้อความเปลี่ยนหลังตรวจ — ตรวจใหม่เพื่อดูผลล่าสุด</span>}
|
|
</div>
|
|
{error && <Notice tone="red">{error}</Notice>}
|
|
{result && !stale && (
|
|
<div className="rounded-[12px] border border-line bg-board p-3 flex flex-col gap-2">
|
|
<p className="text-[13px] font-medium flex items-center gap-1.5">
|
|
{result.issues.length === 0 && <Icon name="check" size={15} />}
|
|
{result.summary || (result.issues.length ? "พบจุดที่ควรดู" : "ไม่พบปัญหา")}
|
|
</p>
|
|
{result.issues.length > 0 && (
|
|
<ul className="flex flex-col gap-2">
|
|
{result.issues.map((i, n) => (
|
|
<li key={n} className="flex items-start gap-2 text-[13px]">
|
|
<span className={`pill ${LEVEL_META[i.level].pill} shrink-0`}>{LEVEL_META[i.level].label}</span>
|
|
<span className="flex-1 min-w-0">
|
|
{i.problem}
|
|
{i.suggestion && <span className="block text-ink-3">แก้: {i.suggestion}</span>}
|
|
</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
<p className="text-[11px] text-ink-4">AI ช่วยดูเบื้องต้นเท่านั้น ผู้อนุมัติยังต้องตรวจเอง</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|