"use client"; import { useState, useRef, useEffect } from "react"; import { Client } from "@gradio/client"; import { FiSend, FiMessageSquare, FiUser } from "react-icons/fi"; import { MdSupportAgent } from "react-icons/md"; import { RiRobot2Line } from "react-icons/ri"; import "./SupportChat.css"; export default function SupportChat() { const [messages, setMessages] = useState([ { role: "bot", text: "👋 Hi! I'm the Learnix support bot. Ask me anything about the platform — subjects, uploads, works, accounts, or anything else!", }, ]); const [input, setInput] = useState(""); const [loading, setLoading] = useState(false); const [isLoaded, setIsLoaded] = useState(false); const messagesEndRef = useRef(null); const messagesContainerRef = useRef(null); const inputRef = useRef(null); useEffect(() => { setTimeout(() => setIsLoaded(true), 80); }, []); // Scroll only the messages container, not the page useEffect(() => { const container = messagesContainerRef.current; if (container) { container.scrollTop = container.scrollHeight; } }, [messages, loading]); const send = async () => { const q = input.trim(); if (!q || loading) return; setInput(""); setMessages((prev) => [...prev, { role: "user", text: q }]); setLoading(true); try { const client = await Client.connect("shashidharak99/learnix-chatbot"); const result = await client.predict("/chat", { question: q }); const reply = Array.isArray(result.data) ? result.data[0] : result.data; setMessages((prev) => [...prev, { role: "bot", text: String(reply) }]); } catch { setMessages((prev) => [ ...prev, { role: "bot", text: "⚠️ Sorry, I couldn't reach the support server. Please try again in a moment.", }, ]); } finally { setLoading(false); inputRef.current?.focus(); } }; const handleKey = (e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); send(); } }; return (
{/* Header */}

Learnix Support

AI-powered help, available 24/7

Online
{/* Divider */}
{/* Messages */}
{messages.map((msg, i) => (
{msg.role === "bot" && (
)}
{msg.text}
{msg.role === "user" && (
)}
))} {/* Typing indicator */} {loading && (
)}
{/* Divider */}
{/* Input area */}