"use client"; import { useState, useRef, useEffect } from "react"; interface SiteMenteVoiceWidgetProps { businessName?: string; businessType?: "restaurant" | "real-estate" | "clinic" | "car-rental" | "default"; theme?: "dark" | "light"; initialLang?: string; } type Mode = "synthflow" | "text" | "off"; const SYNTHFLOW_WIDGET_ID = "0ee1b79c-43c2-41e0-aa6a-d2a560e0ca6a"; export default function SiteMenteVoiceWidget({ businessName = "SiteMente", businessType = "default", theme = "dark", initialLang = "en" }: SiteMenteVoiceWidgetProps) { const [mode, setMode] = useState("off"); const [showChat, setShowChat] = useState(false); const [language, setLanguage] = useState<'en' | 'es'>(initialLang as 'en' | 'es'); // Text chat state const [messages, setMessages] = useState<{role: "user" | "assistant", content: string}[]>([]); const [input, setInput] = useState(""); const [isSending, setIsSending] = useState(false); const messagesEndRef = useRef(null); // Scroll to bottom of messages useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages]); // Toggle modes const cycleMode = () => { const modes: Mode[] = ["off", "text", "synthflow"]; const currentIndex = modes.indexOf(mode); const nextIndex = (currentIndex + 1) % modes.length; setModeInternal(modes[nextIndex]); }; const setModeInternal = (newMode: Mode) => { setMode(newMode); }; // Text mode functions const sendMessage = async () => { if (!input.trim() || isSending) return; const userMessage = input.trim(); setInput(""); setIsSending(true); setMessages(prev => [...prev, { role: "user", content: userMessage }]); try { await new Promise(resolve => setTimeout(resolve, 1000)); const englishResponses: Record = { "hello": `Hello! 👋 I'm the AI assistant for ${businessName}. How can I help you today?`, "hours": `We're open Monday to Sunday. Do you have a specific question?`, "book": "I can help you book right now. What service are you interested in?", "contact": `You can call us at +34 XXX XXX XXX or message us here.`, "price": "We offer packages starting from €299/month. Would you like me to tell you more?", "menu": "We serve delicious food daily. Would you like to see our menu or make a reservation?", "reservation": "I can help you make a reservation! What date and time works for you?", "thanks": "You're welcome! Is there anything else I can help you with?", "thank you": "You're welcome! Is there anything else I can help you with?", }; const spanishResponses: Record = { "hola": `¡Hola! 👋 Soy el asistente de ${businessName}. ¿En qué puedo ayudarte hoy?`, "horario": `Nuestros horarios de atención son de lunes a domingo. ¿Tienes alguna pregunta específica?`, "reservar": "Para hacer una reserva, puedo ayudarte ahora mismo. ¿Qué servicio te interesa?", "contacto": `Puedes llamarnos al +34 XXX XXX XXX o escribirnos aquí.`, }; const lowerInput = userMessage.toLowerCase(); const spanishKeywords = ['hola', 'gracias', 'si', 'no', 'por favor', 'quiero', 'necesito', 'reserva', 'horario', 'precio', 'contacto']; const isSpanish = spanishKeywords.some(keyword => lowerInput.includes(keyword)); const responses = (language === 'es' || isSpanish) ? spanishResponses : englishResponses; const defaultResponse = language === 'es' ? "Gracias por tu mensaje. Un miembro de nuestro equipo te responderá pronto." : "Thanks for your message! A team member will get back to you shortly."; let response = defaultResponse; for (const [key, value] of Object.entries(responses)) { if (lowerInput.includes(key)) { response = value; break; } } setMessages(prev => [...prev, { role: "assistant", content: response }]); } catch (error) { console.error("Send error:", error); } finally { setIsSending(false); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); sendMessage(); } }; const buttonColor = theme === "dark" ? "bg-brand-pink" : "bg-blue-600"; const bgColor = theme === "dark" ? "bg-[#1a1625]" : "bg-white"; const textColor = theme === "dark" ? "text-white" : "text-gray-900"; const inputBg = theme === "dark" ? "bg-white/10" : "bg-gray-100"; // Get mode icon const getModeIcon = () => { if (mode === "synthflow") return "🎙️"; if (mode === "text") return "💬"; return "⚪"; }; const getModeLabel = () => { if (mode === "synthflow") return "AI Voice"; if (mode === "text") return "Chat"; return "Off"; }; return (
{/* Language Toggle */} {/* Mode Toggle Button */} {/* Synthflow Widget */} {mode === "synthflow" && (