Spaces:
Sleeping
Sleeping
Đỗ Hải Nam
commited on
Commit
·
be2adbe
1
Parent(s):
565924b
fix: resolve first message disappear bug and improve session state handling
Browse files
frontend/src/App.jsx
CHANGED
|
@@ -23,7 +23,8 @@ function App() {
|
|
| 23 |
|
| 24 |
const [conversations, setConversations] = useState([])
|
| 25 |
const [currentConversation, setCurrentConversation] = useState(() => {
|
| 26 |
-
|
|
|
|
| 27 |
})
|
| 28 |
const [messages, setMessages] = useState([])
|
| 29 |
const [isLoading, setIsLoading] = useState(false)
|
|
@@ -87,6 +88,7 @@ function App() {
|
|
| 87 |
})
|
| 88 |
const appRef = useRef(null)
|
| 89 |
const isCreatingSessionRef = useRef(false)
|
|
|
|
| 90 |
const isInitialMountRef = useRef(true) // Track if this is the first mount after refresh
|
| 91 |
|
| 92 |
// --- Effects ---
|
|
@@ -235,15 +237,19 @@ function App() {
|
|
| 235 |
useEffect(() => {
|
| 236 |
if (currentConversation) {
|
| 237 |
if (isCreatingSessionRef.current) {
|
|
|
|
|
|
|
| 238 |
isCreatingSessionRef.current = false
|
| 239 |
return
|
| 240 |
}
|
| 241 |
-
|
| 242 |
-
if (
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
localStorage.setItem('lastConversationId', currentConversation)
|
| 246 |
}
|
|
|
|
|
|
|
|
|
|
| 247 |
} else {
|
| 248 |
setMessages([])
|
| 249 |
}
|
|
@@ -380,7 +386,7 @@ function App() {
|
|
| 380 |
const createConversation = () => {
|
| 381 |
setCurrentConversation(null)
|
| 382 |
setMessages([])
|
| 383 |
-
localStorage.
|
| 384 |
if (window.innerWidth < 768) setSidebarOpen(false)
|
| 385 |
}
|
| 386 |
|
|
@@ -497,6 +503,8 @@ function App() {
|
|
| 497 |
const sendMessage = async (text, uploadedImages) => {
|
| 498 |
const userMessage = text.trim()
|
| 499 |
const imagePreviews = uploadedImages.map(img => img.preview)
|
|
|
|
|
|
|
| 500 |
|
| 501 |
// Optimistic UI update
|
| 502 |
setMessages(prev => [...prev, {
|
|
@@ -614,6 +622,8 @@ function App() {
|
|
| 614 |
})
|
| 615 |
} finally {
|
| 616 |
setIsLoading(false)
|
|
|
|
|
|
|
| 617 |
}
|
| 618 |
}
|
| 619 |
|
|
|
|
| 23 |
|
| 24 |
const [conversations, setConversations] = useState([])
|
| 25 |
const [currentConversation, setCurrentConversation] = useState(() => {
|
| 26 |
+
const lastId = localStorage.getItem('lastConversationId')
|
| 27 |
+
return (lastId === 'null' || !lastId) ? null : lastId
|
| 28 |
})
|
| 29 |
const [messages, setMessages] = useState([])
|
| 30 |
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
| 88 |
})
|
| 89 |
const appRef = useRef(null)
|
| 90 |
const isCreatingSessionRef = useRef(false)
|
| 91 |
+
const isSendingMessageRef = useRef(false) // Track if we're currently sending a message
|
| 92 |
const isInitialMountRef = useRef(true) // Track if this is the first mount after refresh
|
| 93 |
|
| 94 |
// --- Effects ---
|
|
|
|
| 237 |
useEffect(() => {
|
| 238 |
if (currentConversation) {
|
| 239 |
if (isCreatingSessionRef.current) {
|
| 240 |
+
// If we JUST created this session, don't clear messages!
|
| 241 |
+
// The current messages state already contains the optimistic user message.
|
| 242 |
isCreatingSessionRef.current = false
|
| 243 |
return
|
| 244 |
}
|
| 245 |
+
|
| 246 |
+
// Skip loading if we're currently sending a message (to prevent race condition)
|
| 247 |
+
if (isSendingMessageRef.current) {
|
| 248 |
+
return
|
|
|
|
| 249 |
}
|
| 250 |
+
|
| 251 |
+
loadMessages(currentConversation)
|
| 252 |
+
localStorage.setItem('lastConversationId', currentConversation)
|
| 253 |
} else {
|
| 254 |
setMessages([])
|
| 255 |
}
|
|
|
|
| 386 |
const createConversation = () => {
|
| 387 |
setCurrentConversation(null)
|
| 388 |
setMessages([])
|
| 389 |
+
localStorage.removeItem('lastConversationId') // Clear instead of setting "null"
|
| 390 |
if (window.innerWidth < 768) setSidebarOpen(false)
|
| 391 |
}
|
| 392 |
|
|
|
|
| 503 |
const sendMessage = async (text, uploadedImages) => {
|
| 504 |
const userMessage = text.trim()
|
| 505 |
const imagePreviews = uploadedImages.map(img => img.preview)
|
| 506 |
+
// Set flag to prevent loadMessages from overwriting optimistic updates
|
| 507 |
+
isSendingMessageRef.current = true
|
| 508 |
|
| 509 |
// Optimistic UI update
|
| 510 |
setMessages(prev => [...prev, {
|
|
|
|
| 622 |
})
|
| 623 |
} finally {
|
| 624 |
setIsLoading(false)
|
| 625 |
+
// Clear flag after message sending is complete
|
| 626 |
+
isSendingMessageRef.current = false
|
| 627 |
}
|
| 628 |
}
|
| 629 |
|
frontend/src/components/MessageList.jsx
CHANGED
|
@@ -176,7 +176,9 @@ const MessageList = ({
|
|
| 176 |
}
|
| 177 |
|
| 178 |
setIsRestored(false)
|
| 179 |
-
|
|
|
|
|
|
|
| 180 |
prevConversationId.current = conversationId
|
| 181 |
prevMessagesLengthRef.current = 0
|
| 182 |
}
|
|
|
|
| 176 |
}
|
| 177 |
|
| 178 |
setIsRestored(false)
|
| 179 |
+
// Only set transitioning if we're moving from one real session to another
|
| 180 |
+
// This prevents "white screen" on new sessions while streaming starts
|
| 181 |
+
setIsTransitioning(!!prevConversationId.current && !!conversationId)
|
| 182 |
prevConversationId.current = conversationId
|
| 183 |
prevMessagesLengthRef.current = 0
|
| 184 |
}
|