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

31 lines
963 B
TypeScript
Raw Normal View History

2023-02-07 20:31:07 +01:00
import { useEffect } from "react";
import { UserInfo } from "../components/jitsi/types";
2023-04-08 15:58:11 +02:00
import useAllChat from "./useAllChat";
2023-02-07 20:31:07 +01:00
import useRoomData from "./useRoomData";
import useWebSocketConnection from "./useWebSocketConnection";
2023-02-07 20:31:07 +01:00
function useBackendData(userInfo: UserInfo) {
console.log("[Rooms] useBackendData");
2023-04-01 17:46:33 +02:00
const { onMessage, sendMessage, disconnect } =
useWebSocketConnection(userInfo);
2023-02-07 20:31:07 +01:00
const { roomData, setRoomData } = useRoomData();
2023-04-08 15:58:11 +02:00
const { chatMesages, addChatMessage } = useAllChat()
2023-04-01 17:46:33 +02:00
useEffect(() => {
onMessage((messageString) => {
console.log("[Rooms] message from ws", messageString);
const messageObject = JSON.parse(messageString);
2023-04-01 17:46:33 +02:00
!!messageObject.roomsData && setRoomData(messageObject);
2023-04-08 15:58:11 +02:00
!!messageObject.content && addChatMessage(messageObject);
2023-04-01 17:46:33 +02:00
return disconnect;
});
}, [onMessage, setRoomData, disconnect]);
2023-02-07 20:31:07 +01:00
return { roomData, sendMessage };
}
2023-02-07 20:31:07 +01:00
export default useBackendData;