import React, { useState, useCallback } from 'react' export default function NewConversationModal({ currentUserId, apiFetch, onCreated, onClose }) { const [type, setType] = useState('direct') const [recipientInput, setRecipient] = useState('') const [groupTitle, setGroupTitle] = useState('') const [participantInputs, setParticipantInputs] = useState(['', '']) const [body, setBody] = useState('') const [sending, setSending] = useState(false) const [error, setError] = useState(null) const addParticipant = () => setParticipantInputs((prev) => [...prev, '']) const updateParticipant = (index, value) => setParticipantInputs((prev) => prev.map((entry, currentIndex) => (currentIndex === index ? value : entry))) const removeParticipant = (index) => setParticipantInputs((prev) => prev.filter((_, currentIndex) => currentIndex !== index)) const handleSubmit = useCallback(async (e) => { e.preventDefault() setError(null) setSending(true) try { const payload = { type, body } if (type === 'direct') { const user = await resolveUsername(recipientInput.trim(), apiFetch) payload.recipient_id = user.id } else { const resolved = await Promise.all( participantInputs .map((entry) => entry.trim()) .filter(Boolean) .map((entry) => resolveUsername(entry, apiFetch)), ) payload.participant_ids = resolved.map((user) => user.id) payload.title = groupTitle.trim() } const conv = await apiFetch('/api/messages/conversation', { method: 'POST', body: JSON.stringify(payload), }) onCreated(conv) } catch (e) { setError(e.message) } finally { setSending(false) } }, [apiFetch, body, groupTitle, onCreated, participantInputs, recipientInput, type]) return (
Compose
Send a direct message or set up a group thread with collaborators.