jitsi-roomsv2/frontend/src/hooks/useBackendData.ts
qvalentin bc88f79fef
All checks were successful
continuous-integration/drone/push Build is passing
Fix double chat messages with useEffect cleanup
2023-08-06 13:03:39 +02:00

32 lines
1 KiB
TypeScript

import { useEffect } from 'react'
import { UserInfo } from '../components/jitsi/types'
import useAllChat from './useAllChat'
import useRoomData from './useRoomData'
import useWebSocketConnection from './useWebSocketConnection'
function useBackendData(userInfo: UserInfo) {
console.log('[Rooms] useBackendData')
const { onMessage, cleanUpOnMessage, sendMessage, disconnect } = useWebSocketConnection(userInfo)
const { roomData, setRoomData } = useRoomData()
const { addChatMessage } = useAllChat()
useEffect(() => {
console.log('[Rooms] add onMessage Listener')
const messageCallback = (messageString: string) => {
console.log('[Rooms] message from ws', messageString)
const messageObject = JSON.parse(messageString)
!!messageObject.roomsData && setRoomData(messageObject)
!!messageObject.content && addChatMessage(messageObject)
return disconnect
}
onMessage(messageCallback)
return () => cleanUpOnMessage(messageCallback)
}, [])
return { roomData, sendMessage }
}
export default useBackendData