Describe your event (new event), Improve/Shorter/Fix mistakes (event description, banner, email and WhatsApp boxes), Draft with AI (email and WhatsApp attendees), Summarise this report (people-free reports) and Summarise feedback (per event). Built-in privacy notice text for AI (2.0.0 entry in BUILT_IN_UPDATES). npm run ai:eval runs the trial cases through the Hub with fact checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMeckMWrKPfc1MMyMJF
66 lines
3.6 KiB
TypeScript
66 lines
3.6 KiB
TypeScript
"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<string | null>(null);
|
|
const [draft, setDraft] = useState<Draft | null>(null);
|
|
if (!status?.enabled) return null;
|
|
|
|
if (!open) {
|
|
return <AiButton helper="draft_message" size="sm" onClick={() => setOpen(true)}>Draft with AI</AiButton>;
|
|
}
|
|
|
|
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>("draft_message", { channel, note, ...(eventId ? { eventId } : {}) }, token)); } catch (e) { setError(errorMessage(e) || "The AI couldn't draft this message."); }
|
|
setBusy(false);
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-2 rounded-lg border border-violet-200 bg-violet-50/60 p-3" data-testid="ai-draft-message">
|
|
<label htmlFor={noteId} className="flex items-center gap-1.5 text-sm font-medium text-violet-900"><Sparkles className="h-4 w-4" aria-hidden /> What should the message say?</label>
|
|
<textarea id={noteId} rows={2} maxLength={3000} className="w-full rounded border bg-white px-3 py-2 text-sm" value={note} onChange={e => setNote(e.target.value)}
|
|
placeholder={channel === "whatsapp" ? "e.g. remind them to bring a sleeping bag; the bus leaves at 7:30 on Friday from the church car park" : "e.g. the breakfast moves to the Protea Hotel on Main Road, same time, park at the back, nothing to do if you've paid"} />
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<AiButton helper="draft_message" size="sm" busy={busy} onClick={run}>{busy ? "Drafting…" : `Draft the ${channel === "whatsapp" ? "WhatsApp message" : "email"}`}</AiButton>
|
|
<button type="button" className="text-xs text-gray-600 underline" onClick={() => { setOpen(false); setDraft(null); setError(null); }}>Close</button>
|
|
</div>
|
|
{error && <p className="text-sm text-red-700" role="alert">{error}</p>}
|
|
{draft && (
|
|
<div className="space-y-2 rounded border border-violet-200 bg-white p-2 text-sm">
|
|
<AiNotice />
|
|
{draft.subject && <p><span className="text-xs text-gray-500">Subject: </span>{draft.subject}</p>}
|
|
<p className="whitespace-pre-wrap">{draft.body}</p>
|
|
<div className="flex gap-2">
|
|
<button type="button" className="rounded bg-brand-600 px-3 py-1 text-xs font-medium text-white hover:bg-brand-700" onClick={() => { onUse(draft); setDraft(null); setOpen(false); }}>Use this</button>
|
|
<button type="button" className="rounded border px-3 py-1 text-xs hover:bg-gray-50" onClick={() => setDraft(null)}>Discard</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|