Feat: Agregar botón para iniciar conversación con número nuevo
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m11s

- Nuevo endpoint POST /api/messages/:instanceId/new-chat
- Valida que el número esté registrado en WhatsApp
- Crea el chat en la DB si no existe
- Modal en UI para ingresar número de teléfono
- Botón verde "+" junto a la barra de búsqueda
This commit is contained in:
2025-12-04 12:59:01 -06:00
parent af5ed0f0c5
commit 23e78fb0b2
2 changed files with 251 additions and 0 deletions

View File

@@ -70,6 +70,14 @@
icon="i-lucide-search"
class="flex-1"
/>
<button
@click="showNewChatModal = true"
class="text-xs px-2 py-2 rounded bg-[var(--wa-green)] hover:bg-[var(--wa-green-dark)] text-white"
title="Nueva conversacion"
:disabled="!selectedInstance"
>
<UIcon name="i-lucide-plus" class="w-4 h-4" />
</button>
<button
@click="showChatsDebug = !showChatsDebug"
class="text-xs px-2 py-2 rounded bg-gray-700 hover:bg-gray-600 text-gray-300"
@@ -242,6 +250,57 @@
</template>
</div>
</div>
<!-- New Chat Modal -->
<UModal v-model:open="showNewChatModal">
<template #content>
<div class="p-6 bg-[var(--wa-bg-dark)]">
<h3 class="text-lg font-semibold text-[var(--wa-text)] mb-4">
Nueva conversación
</h3>
<div class="space-y-4">
<div>
<label class="block text-sm text-[var(--wa-text-muted)] mb-2">
Número de teléfono
</label>
<UInput
v-model="newChatPhoneNumber"
placeholder="Ej: 50588887777"
icon="i-lucide-phone"
class="w-full"
@keyup.enter="startNewChat"
/>
<p class="text-xs text-[var(--wa-text-muted)] mt-1">
Ingresa el número con código de país, sin espacios ni guiones
</p>
</div>
<div v-if="newChatError" class="text-sm text-red-400">
{{ newChatError }}
</div>
<div class="flex justify-end gap-2 pt-2">
<UButton
color="neutral"
variant="ghost"
@click="closeNewChatModal"
>
Cancelar
</UButton>
<UButton
color="primary"
:loading="newChatLoading"
:disabled="!newChatPhoneNumber.trim()"
@click="startNewChat"
>
Iniciar conversación
</UButton>
</div>
</div>
</div>
</template>
</UModal>
</div>
</template>
@@ -279,6 +338,12 @@ const showDebugPanel = ref(false)
const showChatsDebug = ref(false)
const showSelectedChatDebug = ref(false)
// New chat modal state
const showNewChatModal = ref(false)
const newChatPhoneNumber = ref('')
const newChatLoading = ref(false)
const newChatError = ref('')
// Instance options for selector
const instanceOptions = computed(() =>
instances.value
@@ -658,6 +723,54 @@ const handleReact = async (message: any, emoji: string) => {
}
}
// New chat modal functions
const closeNewChatModal = () => {
showNewChatModal.value = false
newChatPhoneNumber.value = ''
newChatError.value = ''
}
const startNewChat = async () => {
if (!selectedInstance.value?.value || !newChatPhoneNumber.value.trim()) return
newChatLoading.value = true
newChatError.value = ''
try {
const result = await $fetch(`/api/messages/${selectedInstance.value.value}/new-chat`, {
method: 'POST',
body: {
phoneNumber: newChatPhoneNumber.value.trim()
}
})
if (result.success && result.chat) {
// If it's a new chat, add it to the list
if (result.isNew) {
chats.value.unshift(result.chat)
}
// Select the chat
selectedChat.value = result.chat
// Close the modal
closeNewChatModal()
toast.add({
title: result.isNew ? 'Conversación creada' : 'Conversación encontrada',
icon: 'i-lucide-check',
color: 'success',
timeout: 2000
})
}
} catch (e: any) {
console.error('Error creating new chat:', e)
newChatError.value = e?.data?.message || e?.message || 'Error al crear la conversación'
} finally {
newChatLoading.value = false
}
}
// Handle typing event from input
const handleTyping = () => {
if (!selectedChat.value?.jid || selectedChat.value?.isGroup) return