32 lines
1 KiB
TypeScript
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
|