import { useState } from 'react'; import { Send, Loader2, MessageSquare } from 'lucide-react'; export default function ChatInterface() { const [message, setMessage] = useState(''); const [chatHistory, setChatHistory] = useState([]); const [isLoading, setIsLoading] = useState(false); const sendMessage = async (e) => { e.preventDefault(); if (!message.trim()) return; setIsLoading(true); const newMessage = { text: message, sender: 'user', timestamp: new Date().toISOString() }; setChatHistory(prev => [...prev, newMessage]); try { // Replace YOUR_N8N_WEBHOOK_URL with your actual n8n webhook URL const response = await fetch('YOUR_N8N_WEBHOOK_URL', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ message: message, timestamp: new Date().toISOString(), }), }); if (response.ok) { // Add system response if your n8n workflow sends one back const data = await response.json(); if (data.response) { setChatHistory(prev => [...prev, { text: data.response, sender: 'system', timestamp: new Date().toISOString() }]); } } } catch (error) { console.error('Error sending message:', error); setChatHistory(prev => [...prev, { text: 'Error sending message. Please try again.', sender: 'system', timestamp: new Date().toISOString() }]); } setIsLoading(false); setMessage(''); }; return (

N8N Chat Trigger

Connected to your workflow

{chatHistory.length === 0 && (

No messages yet

Start the conversation to trigger your n8n workflow

)} {chatHistory.map((chat, index) => (

{chat.text}

{new Date(chat.timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}

))}
setMessage(e.target.value)} placeholder="Type your message..." className="flex-1 rounded-xl border border-gray-200 px-4 py-3 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200 placeholder:text-gray-400 text-gray-700" disabled={isLoading} />
); }