jitsi-roomsv2/frontend/src/hooks/useBackendData.ts

32 lines
1 KiB
TypeScript
Raw Normal View History

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-02-07 20:31:07 +01:00
function useBackendData(userInfo: UserInfo) {
2023-04-10 23:28:28 +02:00
console.log('[Rooms] useBackendData')
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')
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-04-10 23:28:28 +02:00
!!messageObject.roomsData && setRoomData(messageObject)
!!messageObject.content && addChatMessage(messageObject)
return disconnect
}
onMessage(messageCallback)
return () => cleanUpOnMessage(messageCallback)
2023-08-01 09:20:25 +02:00
}, [])
2023-04-10 23:28:28 +02:00
return { roomData, sendMessage }
}
2023-02-07 20:31:07 +01:00
2023-04-10 23:28:28 +02:00
export default useBackendData