Upload 194 files
Browse files
src/components/chat/ChatRoom.tsx
CHANGED
|
@@ -35,25 +35,21 @@ export function ChatRoom({ roomId, codeId, userId }: ChatRoomProps) {
|
|
| 35 |
loadMessages()
|
| 36 |
}, [roomId])
|
| 37 |
|
| 38 |
-
//
|
| 39 |
useEffect(() => {
|
| 40 |
-
|
| 41 |
-
const interval = setInterval(async () => {
|
| 42 |
try {
|
| 43 |
-
if (typeof window !== 'undefined' && navigator.onLine) {
|
| 44 |
await syncPendingMessages()
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
-
const latest = await getMessages(roomId)
|
| 47 |
-
if (active) setMessages(latest)
|
| 48 |
} catch (err) {
|
| 49 |
-
|
| 50 |
-
console.error('Polling messages failed:', err)
|
| 51 |
}
|
| 52 |
-
}, 3000)
|
| 53 |
-
return () => {
|
| 54 |
-
active = false
|
| 55 |
-
clearInterval(interval)
|
| 56 |
}
|
|
|
|
|
|
|
| 57 |
}, [roomId])
|
| 58 |
|
| 59 |
const handleSendMessage = async (content: string) => {
|
|
|
|
| 35 |
loadMessages()
|
| 36 |
}, [roomId])
|
| 37 |
|
| 38 |
+
// Refresh messages when tab becomes visible (no polling)
|
| 39 |
useEffect(() => {
|
| 40 |
+
const onVisible = async () => {
|
|
|
|
| 41 |
try {
|
| 42 |
+
if (typeof window !== 'undefined' && document.visibilityState === 'visible' && navigator.onLine) {
|
| 43 |
await syncPendingMessages()
|
| 44 |
+
const latest = await getMessages(roomId)
|
| 45 |
+
setMessages(latest)
|
| 46 |
}
|
|
|
|
|
|
|
| 47 |
} catch (err) {
|
| 48 |
+
console.error('Refreshing messages failed:', err)
|
|
|
|
| 49 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
+
document.addEventListener('visibilitychange', onVisible)
|
| 52 |
+
return () => document.removeEventListener('visibilitychange', onVisible)
|
| 53 |
}, [roomId])
|
| 54 |
|
| 55 |
const handleSendMessage = async (content: string) => {
|