"use client"; import React, { useId, useState } from "react"; import { Sparkles } from "lucide-react"; import { useAuth } from "@/hooks/useAuth"; import { useAiStatus } from "@/hooks/useAiStatus"; import { runAi } from "@/lib/ai"; import { errorMessage } from "@/lib/utils"; import { AiButton, AiNotice } from "@/components/shared/AiButton"; type Draft = { subject: string; body: string }; /** * "Draft a message": a short note in, an email (subject and body) or WhatsApp message out, using * {{name}} and the event placeholders. The draft goes into the message box only with "Use this", * where it can be edited before sending. Hidden on sites without AI. */ export function AiDraftMessage({ channel, eventId, onUse }: { channel: "email" | "whatsapp"; eventId?: string; onUse: (draft: Draft) => void }) { const { token } = useAuth(); const status = useAiStatus(); const noteId = useId(); const [open, setOpen] = useState(false); const [note, setNote] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [draft, setDraft] = useState(null); if (!status?.enabled) return null; if (!open) { return setOpen(true)}>Draft with AI; } const run = async () => { if (!token) return; if (note.trim().length < 3) { setError("Say in a few words what the message is about."); return; } setBusy(true); setError(null); try { setDraft(await runAi("draft_message", { channel, note, ...(eventId ? { eventId } : {}) }, token)); } catch (e) { setError(errorMessage(e) || "The AI couldn't draft this message."); } setBusy(false); }; return (