Add AI auto sales replies for inbox

This commit is contained in:
Loma Bot Ops
2026-11-25 21:31:32 +07:00
parent dd42b81639
commit 89d46ab141
10 changed files with 272 additions and 5 deletions
+67
View File
@@ -0,0 +1,67 @@
"use client";
import { useEffect, useState } from "react";
import { withBase } from "@/lib/base-path";
import { Icon } from "./Icon";
import { Notice } from "./ui";
interface Settings { enabled: boolean; personaName: string; salesGoal: string; instructions: string }
export function InboxAiSettings({ pageId, canManage }: { pageId: string; canManage: boolean }) {
const [settings, setSettings] = useState<Settings | null>(null);
const [open, setOpen] = useState(false);
const [saving, setSaving] = useState(false);
const [message, setMessage] = useState<{ tone: "blue" | "red"; text: string } | null>(null);
const endpoint = withBase(`/api/inbox/${pageId}/ai-settings`);
useEffect(() => {
let active = true;
fetch(endpoint, { cache: "no-store" }).then(async (res) => {
const body = await res.json().catch(() => null);
if (active && res.ok) setSettings(body);
});
return () => { active = false; };
}, [endpoint]);
async function save(next: Settings) {
setSaving(true); setMessage(null);
const res = await fetch(endpoint, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(next) });
const body = await res.json().catch(() => ({}));
setSaving(false);
if (!res.ok) return setMessage({ tone: "red", text: body.error ?? "บันทึก AI Auto ไม่สำเร็จ" });
setSettings(body); setMessage({ tone: "blue", text: "บันทึกการตั้งค่า AI Auto แล้ว" });
}
if (!settings) return null;
return (
<section className="card mb-4 overflow-hidden" aria-labelledby="inbox-ai-title">
<div className="px-4 py-3 sm:px-5 flex flex-wrap items-center gap-3 bg-blue-soft">
<span className={`w-9 h-9 rounded-[10px] grid place-items-center ${settings.enabled ? "bg-green-soft text-green" : "bg-white text-ink-3"}`}><Icon name="bot" size={19} /></span>
<div className="flex-1 min-w-[180px]">
<h2 id="inbox-ai-title" className="font-semibold">AI Auto สำหรับปิดการขาย</h2>
<p className="text-[12px] text-ink-3">{settings.enabled ? `${settings.personaName} กำลังช่วยตอบแชท และส่งต่อให้คนเมื่อไม่มั่นใจ` : "AI ยังไม่ส่งข้อความอัตโนมัติ"}</p>
</div>
<span className={`pill ${settings.enabled ? "pill-green" : "pill-neutral"}`}>{settings.enabled ? "กำลังทำงาน" : "ปิดอยู่"}</span>
<button type="button" className="btn btn-secondary" onClick={() => setOpen((value) => !value)} aria-expanded={open}>ตั้งค่า</button>
</div>
{open && (
<div className="p-4 sm:p-5 grid gap-4 border-t border-line-2">
<Notice tone="blue">AI จะตอบจากข้อมูลโปรไฟล์เพจเท่านั้น ไม่แต่งราคา สต็อก หรือโปรโมชัน และจะหยุดให้แอดมินรับช่วงเมื่อเรื่องเกินขอบเขต</Notice>
<div className="grid sm:grid-cols-2 gap-4">
<label className="field"><span>ชื่อแอดมินในแชท</span><input className="input" value={settings.personaName} disabled={!canManage} maxLength={60} onChange={(e) => setSettings({ ...settings, personaName: e.target.value })} /></label>
<label className="field"><span>เป้าหมายการขาย</span><input className="input" value={settings.salesGoal} disabled={!canManage} maxLength={500} placeholder="เช่น ช่วยเลือกแพ็กเกจและขอช่องทางติดต่อกลับ" onChange={(e) => setSettings({ ...settings, salesGoal: e.target.value })} /></label>
</div>
<label className="field"><span>ข้อมูลหรือแนวทางเพิ่มเติม</span><textarea className="textarea" rows={3} value={settings.instructions} disabled={!canManage} maxLength={1200} placeholder="เช่น ถ้าลูกค้าสนใจ ให้ถามงบประมาณและวันใช้งานทีละคำถาม" onChange={(e) => setSettings({ ...settings, instructions: e.target.value })} /></label>
{message && <Notice tone={message.tone}>{message.text}</Notice>}
<div className="flex flex-wrap justify-between gap-3 items-center">
<label className="flex items-center gap-3 min-h-11">
<button type="button" role="switch" aria-checked={settings.enabled} className="toggle" disabled={!canManage || saving} onClick={() => setSettings({ ...settings, enabled: !settings.enabled })} />
<span><b className="block text-[14px]">อนุญาตให้ AI ตอบอัตโนมัติ</b><span className="text-[12px] text-ink-3">ยังปิดแยกรายแชทได้จากแผงด้านขวา</span></span>
</label>
{canManage ? <button type="button" className="btn btn-primary" disabled={saving} onClick={() => save(settings)}>{saving ? "กำลังบันทึก…" : "บันทึกการตั้งค่า"}</button> : <span className="text-[12px] text-ink-3">ผู้อนุมัติขึ้นไปจึงแก้การตั้งค่านี้ได้</span>}
</div>
</div>
)}
</section>
);
}