2021-10-22 04:32:17 +02:00
|
|
|
import { RoomInfoEvent, RoomSessionChatEvent, RoomSessionEvent } from '@nitrots/nitro-renderer';
|
|
|
|
import { FC, useCallback, useState } from 'react';
|
|
|
|
import { GetRoomSession } from '../../api';
|
2021-10-23 00:37:54 +02:00
|
|
|
import { CreateMessageHook, useRoomSessionManagerEvent } from '../../hooks';
|
2021-10-22 04:32:17 +02:00
|
|
|
import { useChatHistoryContext } from './context/ChatHistoryContext';
|
2021-10-23 00:37:54 +02:00
|
|
|
import { ChatEntryType, CHAT_HISTORY_MAX, IChatEntry, IRoomHistoryEntry, ROOM_HISTORY_MAX } from './context/ChatHistoryContext.types';
|
2021-10-22 04:32:17 +02:00
|
|
|
import { currentDate } from './utils/Utilities';
|
|
|
|
|
|
|
|
export const ChatHistoryMessageHandler: FC<{}> = props =>
|
|
|
|
{
|
2021-10-23 00:37:54 +02:00
|
|
|
const { chatHistoryState = null, roomHistoryState = null } = useChatHistoryContext();
|
2021-10-22 04:32:17 +02:00
|
|
|
|
|
|
|
const [needsRoomInsert, setNeedsRoomInsert ] = useState(false);
|
|
|
|
|
|
|
|
const addChatEntry = useCallback((entry: IChatEntry) =>
|
|
|
|
{
|
2021-10-23 00:37:54 +02:00
|
|
|
entry.id = chatHistoryState.chats.length;
|
|
|
|
|
|
|
|
chatHistoryState.chats.push(entry);
|
2021-10-22 04:32:17 +02:00
|
|
|
|
|
|
|
//check for overflow
|
2021-10-23 00:37:54 +02:00
|
|
|
if(chatHistoryState.chats.length > CHAT_HISTORY_MAX)
|
2021-10-22 04:32:17 +02:00
|
|
|
{
|
2021-10-23 00:37:54 +02:00
|
|
|
chatHistoryState.chats.shift();
|
2021-10-22 04:32:17 +02:00
|
|
|
}
|
2021-10-23 00:37:54 +02:00
|
|
|
chatHistoryState.notify();
|
2021-10-22 04:32:17 +02:00
|
|
|
|
2021-10-23 00:37:54 +02:00
|
|
|
//dispatchUiEvent(new ChatHistoryEvent(ChatHistoryEvent.CHAT_HISTORY_CHANGED));
|
2021-10-22 04:32:17 +02:00
|
|
|
|
2021-10-23 00:37:54 +02:00
|
|
|
}, [chatHistoryState]);
|
|
|
|
|
|
|
|
const addRoomHistoryEntry = useCallback((entry: IRoomHistoryEntry) =>
|
|
|
|
{
|
|
|
|
roomHistoryState.roomHistory.push(entry);
|
|
|
|
|
|
|
|
// check for overflow
|
|
|
|
if(roomHistoryState.roomHistory.length > ROOM_HISTORY_MAX)
|
|
|
|
{
|
|
|
|
roomHistoryState.roomHistory.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
roomHistoryState.notify();
|
|
|
|
}, [roomHistoryState]);
|
2021-10-22 04:32:17 +02:00
|
|
|
|
|
|
|
const onChatEvent = useCallback((event: RoomSessionChatEvent) =>
|
|
|
|
{
|
|
|
|
const roomSession = GetRoomSession();
|
|
|
|
|
|
|
|
if(!roomSession) return;
|
|
|
|
|
|
|
|
const userData = roomSession.userDataManager.getUserDataByIndex(event.objectId);
|
|
|
|
|
|
|
|
if(!userData) return;
|
|
|
|
|
|
|
|
const timeString = currentDate();
|
|
|
|
|
2021-10-25 04:54:54 +02:00
|
|
|
const entry: IChatEntry = { id: -1, entityId: userData.webID, name: userData.name, look: userData.figure, entityType: userData.type, message: event.message, timestamp: timeString, type: ChatEntryType.TYPE_CHAT, roomId: roomSession.roomId };
|
2021-10-22 04:32:17 +02:00
|
|
|
|
|
|
|
addChatEntry(entry);
|
|
|
|
}, [addChatEntry]);
|
|
|
|
|
|
|
|
useRoomSessionManagerEvent(RoomSessionChatEvent.CHAT_EVENT, onChatEvent);
|
|
|
|
|
|
|
|
const onRoomSessionEvent = useCallback((event: RoomSessionEvent) =>
|
|
|
|
{
|
|
|
|
switch(event.type)
|
|
|
|
{
|
|
|
|
case RoomSessionEvent.STARTED:
|
|
|
|
setNeedsRoomInsert(true);
|
|
|
|
break;
|
|
|
|
case RoomSessionEvent.ENDED:
|
|
|
|
//dispatchUiEvent(new ChatHistoryEvent(ChatHistoryEvent.HIDE_CHAT_HISTORY));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useRoomSessionManagerEvent(RoomSessionEvent.ENDED, onRoomSessionEvent);
|
|
|
|
useRoomSessionManagerEvent(RoomSessionEvent.STARTED, onRoomSessionEvent);
|
|
|
|
|
|
|
|
const onRoomInfoEvent = useCallback((event: RoomInfoEvent) =>
|
|
|
|
{
|
|
|
|
const parser = event.getParser();
|
|
|
|
|
|
|
|
if(!parser) return;
|
|
|
|
|
|
|
|
const session = GetRoomSession();
|
|
|
|
|
|
|
|
if(!session || (session.roomId !== parser.data.roomId)) return;
|
|
|
|
|
|
|
|
if(needsRoomInsert)
|
|
|
|
{
|
2021-10-25 04:54:54 +02:00
|
|
|
const chatEntry: IChatEntry = { id: -1, entityId: parser.data.roomId, name: parser.data.roomName, timestamp: currentDate(), type: ChatEntryType.TYPE_ROOM_INFO, roomId: parser.data.roomId };
|
2021-10-22 04:32:17 +02:00
|
|
|
|
|
|
|
addChatEntry(chatEntry);
|
|
|
|
|
2021-10-23 00:37:54 +02:00
|
|
|
const roomEntry: IRoomHistoryEntry = { id: parser.data.roomId, name: parser.data.roomName };
|
|
|
|
|
|
|
|
addRoomHistoryEntry(roomEntry);
|
|
|
|
|
2021-10-22 04:32:17 +02:00
|
|
|
setNeedsRoomInsert(false);
|
|
|
|
}
|
2021-10-23 00:37:54 +02:00
|
|
|
}, [addChatEntry, addRoomHistoryEntry, needsRoomInsert]);
|
2021-10-22 04:32:17 +02:00
|
|
|
|
|
|
|
CreateMessageHook(RoomInfoEvent, onRoomInfoEvent);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|