import { useEffect, useMemo, useRef, useState } from "react"; import "./App.css"; function Bubble({ role, text, meta }) { return (
{text}
{role === "bot" && meta && (
Intent: {meta.intent || "-"} Topic: {meta.topic || "-"} Emotions:{" "} {Array.isArray(meta.emotions) && meta.emotions.length ? meta.emotions.join(", ") : "-"} KB:{" "} {Array.isArray(meta.kb_sources) && meta.kb_sources.length ? meta.kb_sources.join(", ") : "-"}
)}
); } export default function App() { const [messages, setMessages] = useState([ { role: "bot", text: "Hi 💗 I’m EmpowerHer. You can talk to me about your feelings, or ask questions about periods. What’s on your mind?", }, ]); const [input, setInput] = useState(""); const [loading, setLoading] = useState(false); const endRef = useRef(null); const suggestions = useMemo( () => [ "My period is late and I’m scared 😟", "How can I reduce cramps naturally?", "Is it okay to eat ice cream during periods?", "I feel angry and sad before my period", "Help me calm down, I’m overthinking", ], [] ); useEffect(() => { endRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages, loading]); async function sendMessage(text) { const msg = (text ?? input).trim(); if (!msg || loading) return; setMessages((prev) => [...prev, { role: "user", text: msg }]); setInput(""); setLoading(true); try { const res = await fetch("/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ message: msg }), }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); const reply = data?.reply || "Sorry 😕 I couldn’t generate a reply."; setMessages((prev) => [ ...prev, { role: "bot", text: reply, meta: data }, ]); } catch (e) { setMessages((prev) => [ ...prev, { role: "bot", text: "Oops 😕 I can’t connect to the backend. Is Flask running on http://127.0.0.1:5000 ?", }, ]); } finally { setLoading(false); } } function onKeyDown(e) { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); sendMessage(); } } return (
🌸

EmpowerHer

Menstrual Support Chatbot • Emotion + Knowledge Base

{messages.map((m, i) => ( ))} {loading && (
)}