"use client"; import { withBase } from "@/lib/base-path"; import { useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { Icon } from "./Icon"; import { EmptyState, formatDateTime, Notice, numberFmt } from "./ui"; import type { AnalysisBrief, AnalysisReport } from "@/lib/analysis/types"; type Goal = AnalysisBrief["goal"]; type Status = "QUEUED" | "RUNNING" | "COMPLETED" | "FAILED"; export interface AnalysisItem { id: string; status: Status; brief: AnalysisBrief; postCount: number; warnings: string[]; report: AnalysisReport | null; budget: { testBaht: number; scaleBaht: number; reserveBaht: number; daily: number[] } | null; error: string | null; createdAt: string; finishedAt: string | null; requestedBy: string | null; } // Keep in sync with GOALS in src/lib/analysis/plan.mjs. const GOAL_LABEL: Record = { MESSAGES: "เพิ่มข้อความสอบถาม", SALES: "เพิ่มยอดขาย", AWARENESS: "เพิ่มการรับรู้" }; const STATUS_META: Record = { QUEUED: { label: "รอคิว", pill: "pill-neutral" }, RUNNING: { label: "กำลังวิเคราะห์", pill: "pill-blue" }, COMPLETED: { label: "เสร็จแล้ว", pill: "pill-green" }, FAILED: { label: "ไม่สำเร็จ", pill: "pill-red" }, }; const baht = (n: number) => `${numberFmt.format(n)} บาท`; export function AnalysisView({ pageId, pageActive, canAnalyze, canWrite, briefDefaults, items, }: { pageId: string; pageActive: boolean; canAnalyze: boolean; /** Can create posts on this page — shows "ใช้ไอเดียนี้". */ canWrite: boolean; /** Prefilled from the shop profile. */ briefDefaults: { business: string; audience: string }; items: AnalysisItem[]; }) { const router = useRouter(); const [selectedId, setSelectedId] = useState(items[0]?.id ?? null); const selected = items.find((i) => i.id === selectedId) ?? items[0] ?? null; const pending = items.some((i) => i.status === "QUEUED" || i.status === "RUNNING"); // The worker updates rows out of band — poll while something is in flight. useEffect(() => { if (!pending) return; const t = setInterval(() => router.refresh(), 5000); return () => clearInterval(t); }, [pending, router]); return (
{canAnalyze ? ( { setSelectedId(id); router.refresh(); }} /> ) : ( ผู้จัดการเพจเท่านั้นที่สั่งวิเคราะห์และวางงบได้ )}

ประวัติการวิเคราะห์

{items.length === 0 ? ( ) : (
    {items.map((i) => (
  • ))}
)}
{selected ? : null}
); } function BriefForm({ pageId, defaults, disabled, disabledReason, onQueued, }: { pageId: string; defaults: { business: string; audience: string }; disabled: boolean; disabledReason: string | null; onQueued: (id: string) => void; }) { // Analysis caps these at 1,500 / 1,000 characters; the profile allows longer text. const [business, setBusiness] = useState(defaults.business.slice(0, 1500)); const [audience, setAudience] = useState(defaults.audience.slice(0, 1000)); const [goal, setGoal] = useState("MESSAGES"); const [budget, setBudget] = useState(""); const [days, setDays] = useState("14"); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); async function submit(e: React.FormEvent) { e.preventDefault(); setError(null); setBusy(true); const res = await fetch(withBase("/api/analysis"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pageId, business, audience, goal, budgetBaht: Number(budget.replace(/,/g, "")), days: Number(days) }), }).catch(() => null); const body = res ? await res.json().catch(() => ({})) : {}; setBusy(false); if (!res?.ok) return setError(body.error ?? "เริ่มวิเคราะห์ไม่สำเร็จ ลองใหม่อีกครั้ง"); onQueued(body.id); } return (

สั่งวิเคราะห์ใหม่