"use client"; import { useState } from "react"; import { Button } from "@/app/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/app/components/ui/dialog"; import { Textarea } from "@/app/components/ui/textarea"; import { ImageAttachments, useAttachments } from "@/app/components/messages/ImageAttachments"; import { MemberSearch } from "@/app/components/messages/MemberSearch"; import { useT } from "@/app/lib/i18n/MessagesProvider"; import { MAX_BODY, messageErrorText, startAdminConversation } from "@/app/lib/services/adminMessages"; import type { AdminMember } from "@/app/lib/types"; // Admin -> member: opens or reuses the member's support (S) thread. Mutation on an explicit click only. export function NewMessageDialog({ onClose, onOpened, initialMember, initialBody, }: { onClose: () => void; onOpened: (conversationUuid: string) => void; /** Deep link: member code to message, skipping the search step. */ initialMember?: string; initialBody?: string; }) { const t = useT(); // ponytail: a deep-linked member shows by code only (no name lookup); upgrade by fetching the member if the name is wanted. const [member, setMember] = useState(initialMember ? ({ memberCode: initialMember } as AdminMember) : null); const [body, setBody] = useState(initialBody ?? ""); const [sending, setSending] = useState(false); const [error, setError] = useState(null); const attachments = useAttachments(); const canSend = !!member && !sending && !attachments.busy && body.length <= MAX_BODY && (body.trim() !== "" || attachments.refs.length > 0); const send = async () => { if (!member) return; setSending(true); setError(null); try { const r = await startAdminConversation(member.memberCode, body.trim(), attachments.refs); onOpened(r.conversationUuid); } catch (e) { setError(messageErrorText(e, t)); setSending(false); } }; return ( !open && !sending && onClose()}> {t("MsgNewMessage")} {t("MsgNewMessageHint")} {member ? ( <>
{member.name ?? "—"} · {member.memberCode}