2023-04-10 23:28:28 +02:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { UserInfo } from '../components/jitsi/types'
|
|
|
|
import useAllChat from './useAllChat'
|
|
|
|
import useRoomData from './useRoomData'
|
|
|
|
import useWebSocketConnection from './useWebSocketConnection'
|
2023-01-21 11:18:41 +01:00
|
|
|
|
2023-02-07 20:31:07 +01:00
|
|
|
function useBackendData(userInfo: UserInfo) {
|
2023-04-10 23:28:28 +02:00
|
|
|
console.log('[Rooms] useBackendData')
|
2023-01-21 11:18:41 +01:00
|
|
|
|
2023-08-06 13:03:39 +02:00
|
|
|
const { onMessage, cleanUpOnMessage, sendMessage, disconnect } = useWebSocketConnection(userInfo)
|
2023-04-10 23:28:28 +02:00
|
|
|
const { roomData, setRoomData } = useRoomData()
|
2023-04-10 23:37:30 +02:00
|
|
|
const { addChatMessage } = useAllChat()
|
2023-04-08 15:58:11 +02:00
|
|
|
|
2023-04-01 17:46:33 +02:00
|
|
|
useEffect(() => {
|
2023-08-01 09:20:25 +02:00
|
|
|
console.log('[Rooms] add onMessage Listener')
|
2023-08-06 13:03:39 +02:00
|
|
|
const messageCallback = (messageString: string) => {
|
2023-04-10 23:28:28 +02:00
|
|
|
console.log('[Rooms] message from ws', messageString)
|
|
|
|
const messageObject = JSON.parse(messageString)
|
2023-01-21 11:18:41 +01:00
|
|
|
|
2023-04-10 23:28:28 +02:00
|
|
|
!!messageObject.roomsData && setRoomData(messageObject)
|
|
|
|
!!messageObject.content && addChatMessage(messageObject)
|
|
|
|
return disconnect
|
2023-08-06 13:03:39 +02:00
|
|
|
}
|
|
|
|
onMessage(messageCallback)
|
|
|
|
return () => cleanUpOnMessage(messageCallback)
|
2023-08-01 09:20:25 +02:00
|
|
|
}, [])
|
2023-01-21 11:18:41 +01:00
|
|
|
|
2023-04-10 23:28:28 +02:00
|
|
|
return { roomData, sendMessage }
|
2023-01-21 11:18:41 +01:00
|
|
|
}
|
2023-02-07 20:31:07 +01:00
|
|
|
|
2023-04-10 23:28:28 +02:00
|
|
|
export default useBackendData
|