File size: 3,982 Bytes
a0c836f 852e9a3 a0c836f 852e9a3 a0c836f 852e9a3 a0c836f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | import { useState, useEffect, useRef } from "react"
import Sidebar from "./components/Sidebar"
import ChatWindow from "./components/ChatWindow"
import InputBar from "./components/InputBar"
import { sendMessage, createConversation, getMessages, deleteConversation } from "./api"
import { useIsMobile } from "./useMediaQuery"
const WELCOME_MESSAGE = {
role: "bot",
text: "Hi! I'm DoseMate, your medication mate. Ask me anything about your medicines 💊",
}
export default function App() {
const [messages, setMessages] = useState([WELCOME_MESSAGE])
const [loading, setLoading] = useState(false)
const [conversationId, setConversationId] = useState(null)
const conversationIdRef = useRef(null)
const sidebarRef = useRef(null)
const isFirstMessageRef = useRef(true)
const isMobile = useIsMobile()
useEffect(() => {
initConversation()
}, [])
const initConversation = async () => {
try {
const id = await createConversation()
setConversationId(id)
conversationIdRef.current = id
isFirstMessageRef.current = true
} catch (e) {
console.error("Failed to create conversation", e)
}
}
const handleNewConversation = async (id) => {
const newId = id || await createConversation()
setConversationId(newId)
conversationIdRef.current = newId
isFirstMessageRef.current = true
setMessages([WELCOME_MESSAGE])
}
const handleSelectConversation = async (id) => {
setConversationId(id)
conversationIdRef.current = id
isFirstMessageRef.current = false
try {
const data = await getMessages(id)
const formatted = data.map(msg => ({
role: msg.role,
text: msg.text,
imageUrl: msg.image_path || null,
}))
setMessages([WELCOME_MESSAGE, ...formatted])
} catch (e) {
console.error("Failed to load messages", e)
}
}
const handleDeleteConversation = async (id) => {
try {
await deleteConversation(id)
if (id === conversationId) {
await initConversation()
setMessages([WELCOME_MESSAGE])
}
} catch (e) {
console.error("Failed to delete conversation", e)
}
}
const handleSend = async (text, image) => {
const currentId = conversationIdRef.current
if (!currentId) return
const imageUrl = image ? URL.createObjectURL(image) : null
setMessages(prev => [...prev, { role: "user", text: text || "", imageUrl }])
setLoading(true)
// Optimistic title: first 5 words instantly
if (isFirstMessageRef.current && text && text.trim()) {
const optimisticTitle = text.trim().split(" ").slice(0, 5).join(" ")
sidebarRef.current?.updateTitle(currentId, optimisticTitle)
}
try {
const response = await sendMessage(text, image, currentId)
setMessages(prev => [...prev, { role: "bot", text: response }])
// After response, refresh sidebar to replace with real AI title
if (isFirstMessageRef.current) {
sidebarRef.current?.refresh()
isFirstMessageRef.current = false
}
} catch (error) {
console.error("Send error:", error)
setMessages(prev => [...prev, {
role: "bot",
text: "Sorry, I couldn't connect to the server. Make sure the backend is running.",
}])
} finally {
setLoading(false)
}
}
return (
<div style={{ width: "100%", height: "100vh", display: "flex", overflow: "hidden" }}>
<Sidebar
ref={sidebarRef}
activeConversationId={conversationId}
onSelectConversation={handleSelectConversation}
onNewConversation={handleNewConversation}
onDeleteConversation={handleDeleteConversation}
/>
<div style={{ flex: 1, display: "flex", flexDirection: "column", background: "#fff", overflow: "hidden", paddingTop: isMobile ? 52 : 0 }}>
<ChatWindow messages={messages} loading={loading} />
<InputBar onSend={handleSend} loading={loading} />
</div>
</div>
)
} |