moved stuff to message handler

This commit is contained in:
dank074 2021-10-20 22:45:21 -05:00
parent cf17d152d4
commit 301cf3aa5a
2 changed files with 110 additions and 100 deletions

View File

@ -1,13 +1,18 @@
import { IssueInfoMessageEvent, ModeratorInitMessageEvent } from '@nitrots/nitro-renderer'; import { IssueInfoMessageEvent, ModeratorInitMessageEvent, RoomEngineEvent } from '@nitrots/nitro-renderer';
import { FC, useCallback } from 'react'; import { FC, useCallback } from 'react';
import { CreateMessageHook } from '../../hooks'; import { ModToolsEvent } from '../../events/mod-tools/ModToolsEvent';
import { ModToolsOpenRoomChatlogEvent } from '../../events/mod-tools/ModToolsOpenRoomChatlogEvent';
import { ModToolsOpenRoomInfoEvent } from '../../events/mod-tools/ModToolsOpenRoomInfoEvent';
import { ModToolsOpenUserChatlogEvent } from '../../events/mod-tools/ModToolsOpenUserChatlogEvent';
import { ModToolsOpenUserInfoEvent } from '../../events/mod-tools/ModToolsOpenUserInfoEvent';
import { CreateMessageHook, useRoomEngineEvent, useUiEvent } from '../../hooks';
import { useModToolsContext } from './context/ModToolsContext'; import { useModToolsContext } from './context/ModToolsContext';
import { ModToolsActions } from './reducers/ModToolsReducer'; import { ModToolsActions } from './reducers/ModToolsReducer';
export const ModToolsMessageHandler: FC<{}> = props => export const ModToolsMessageHandler: FC<{}> = props =>
{ {
const { modToolsState = null, dispatchModToolsState = null } = useModToolsContext(); const { modToolsState = null, dispatchModToolsState = null } = useModToolsContext();
const { tickets= null } = modToolsState; const { openRooms = null, openRoomChatlogs = null, openUserChatlogs = null, openUserInfo = null, tickets= null } = modToolsState;
const onModeratorInitMessageEvent = useCallback((event: ModeratorInitMessageEvent) => const onModeratorInitMessageEvent = useCallback((event: ModeratorInitMessageEvent) =>
{ {
@ -63,5 +68,103 @@ export const ModToolsMessageHandler: FC<{}> = props =>
CreateMessageHook(ModeratorInitMessageEvent, onModeratorInitMessageEvent); CreateMessageHook(ModeratorInitMessageEvent, onModeratorInitMessageEvent);
CreateMessageHook(IssueInfoMessageEvent, onIssueInfoMessageEvent); CreateMessageHook(IssueInfoMessageEvent, onIssueInfoMessageEvent);
const onRoomEngineEvent = useCallback((event: RoomEngineEvent) =>
{
switch(event.type)
{
case RoomEngineEvent.INITIALIZED:
dispatchModToolsState({
type: ModToolsActions.SET_CURRENT_ROOM_ID,
payload: {
currentRoomId: event.roomId
}
});
return;
case RoomEngineEvent.DISPOSED:
dispatchModToolsState({
type: ModToolsActions.SET_CURRENT_ROOM_ID,
payload: {
currentRoomId: null
}
});
return;
}
}, [ dispatchModToolsState ]);
useRoomEngineEvent(RoomEngineEvent.INITIALIZED, onRoomEngineEvent);
useRoomEngineEvent(RoomEngineEvent.DISPOSED, onRoomEngineEvent);
const onModToolsEvent = useCallback((event: ModToolsEvent) =>
{
switch(event.type)
{
case ModToolsEvent.OPEN_ROOM_INFO: {
const castedEvent = (event as ModToolsOpenRoomInfoEvent);
if(openRooms && openRooms.includes(castedEvent.roomId)) return;
const rooms = openRooms || [];
dispatchModToolsState({
type: ModToolsActions.SET_OPEN_ROOMS,
payload: {
openRooms: [...rooms, castedEvent.roomId]
}
});
return;
}
case ModToolsEvent.OPEN_ROOM_CHATLOG: {
const castedEvent = (event as ModToolsOpenRoomChatlogEvent);
if(openRoomChatlogs && openRoomChatlogs.includes(castedEvent.roomId)) return;
const chatlogs = openRoomChatlogs || [];
dispatchModToolsState({
type: ModToolsActions.SET_OPEN_ROOM_CHATLOGS,
payload: {
openRoomChatlogs: [...chatlogs, castedEvent.roomId]
}
});
return;
}
case ModToolsEvent.OPEN_USER_INFO: {
const castedEvent = (event as ModToolsOpenUserInfoEvent);
if(openUserInfo && openUserInfo.includes(castedEvent.userId)) return;
const userInfo = openUserInfo || [];
dispatchModToolsState({
type: ModToolsActions.SET_OPEN_USERINFO,
payload: {
openUserInfo: [...userInfo, castedEvent.userId]
}
});
return;
}
case ModToolsEvent.OPEN_USER_CHATLOG: {
const castedEvent = (event as ModToolsOpenUserChatlogEvent);
if(openUserChatlogs && openUserChatlogs.includes(castedEvent.userId)) return;
const userChatlog = openUserChatlogs || [];
dispatchModToolsState({
type: ModToolsActions.SET_OPEN_USER_CHATLOGS,
payload: {
openUserChatlogs: [...userChatlog, castedEvent.userId]
}
});
return;
}
}
}, [openRooms, dispatchModToolsState, openRoomChatlogs, openUserInfo, openUserChatlogs]);
useUiEvent(ModToolsEvent.OPEN_ROOM_INFO, onModToolsEvent);
useUiEvent(ModToolsEvent.OPEN_ROOM_CHATLOG, onModToolsEvent);
useUiEvent(ModToolsEvent.OPEN_USER_INFO, onModToolsEvent);
useUiEvent(ModToolsEvent.OPEN_USER_CHATLOG, onModToolsEvent);
return null; return null;
} }

View File

@ -1,10 +1,9 @@
import { RoomEngineEvent, RoomEngineObjectEvent, RoomObjectCategory } from '@nitrots/nitro-renderer'; import { RoomEngineObjectEvent, RoomObjectCategory } from '@nitrots/nitro-renderer';
import { FC, useCallback, useReducer, useState } from 'react'; import { FC, useCallback, useReducer, useState } from 'react';
import { GetRoomSession } from '../../api'; import { GetRoomSession } from '../../api';
import { ModToolsEvent } from '../../events/mod-tools/ModToolsEvent'; import { ModToolsEvent } from '../../events/mod-tools/ModToolsEvent';
import { ModToolsOpenRoomChatlogEvent } from '../../events/mod-tools/ModToolsOpenRoomChatlogEvent'; import { ModToolsOpenRoomChatlogEvent } from '../../events/mod-tools/ModToolsOpenRoomChatlogEvent';
import { ModToolsOpenRoomInfoEvent } from '../../events/mod-tools/ModToolsOpenRoomInfoEvent'; import { ModToolsOpenRoomInfoEvent } from '../../events/mod-tools/ModToolsOpenRoomInfoEvent';
import { ModToolsOpenUserChatlogEvent } from '../../events/mod-tools/ModToolsOpenUserChatlogEvent';
import { ModToolsOpenUserInfoEvent } from '../../events/mod-tools/ModToolsOpenUserInfoEvent'; import { ModToolsOpenUserInfoEvent } from '../../events/mod-tools/ModToolsOpenUserInfoEvent';
import { useRoomEngineEvent } from '../../hooks/events'; import { useRoomEngineEvent } from '../../hooks/events';
import { dispatchUiEvent, useUiEvent } from '../../hooks/events/ui/ui-event'; import { dispatchUiEvent, useUiEvent } from '../../hooks/events/ui/ui-event';
@ -24,7 +23,7 @@ export const ModToolsView: FC<ModToolsViewProps> = props =>
{ {
const [ isVisible, setIsVisible ] = useState(false); const [ isVisible, setIsVisible ] = useState(false);
const [ modToolsState, dispatchModToolsState ] = useReducer(ModToolsReducer, initialModTools); const [ modToolsState, dispatchModToolsState ] = useReducer(ModToolsReducer, initialModTools);
const { currentRoomId = null, openRooms = null, openRoomChatlogs = null, openUserChatlogs = null, openUserInfo = null, tickets = null } = modToolsState; const { currentRoomId = null, openRooms = null, openRoomChatlogs = null, openUserChatlogs = null, openUserInfo = null } = modToolsState;
const [ selectedUser, setSelectedUser] = useState<ISelectedUser>(null); const [ selectedUser, setSelectedUser] = useState<ISelectedUser>(null);
const [ isTicketsVisible, setIsTicketsVisible ] = useState(false); const [ isTicketsVisible, setIsTicketsVisible ] = useState(false);
@ -41,102 +40,12 @@ export const ModToolsView: FC<ModToolsViewProps> = props =>
case ModToolsEvent.TOGGLE_MOD_TOOLS: case ModToolsEvent.TOGGLE_MOD_TOOLS:
setIsVisible(value => !value); setIsVisible(value => !value);
return; return;
case ModToolsEvent.OPEN_ROOM_INFO: {
const castedEvent = (event as ModToolsOpenRoomInfoEvent);
if(openRooms && openRooms.includes(castedEvent.roomId)) return;
const rooms = openRooms || [];
dispatchModToolsState({
type: ModToolsActions.SET_OPEN_ROOMS,
payload: {
openRooms: [...rooms, castedEvent.roomId]
} }
}); }, []);
return;
}
case ModToolsEvent.OPEN_ROOM_CHATLOG: {
const castedEvent = (event as ModToolsOpenRoomChatlogEvent);
if(openRoomChatlogs && openRoomChatlogs.includes(castedEvent.roomId)) return;
const chatlogs = openRoomChatlogs || [];
dispatchModToolsState({
type: ModToolsActions.SET_OPEN_ROOM_CHATLOGS,
payload: {
openRoomChatlogs: [...chatlogs, castedEvent.roomId]
}
});
return;
}
case ModToolsEvent.OPEN_USER_INFO: {
const castedEvent = (event as ModToolsOpenUserInfoEvent);
if(openUserInfo && openUserInfo.includes(castedEvent.userId)) return;
const userInfo = openUserInfo || [];
dispatchModToolsState({
type: ModToolsActions.SET_OPEN_USERINFO,
payload: {
openUserInfo: [...userInfo, castedEvent.userId]
}
});
return;
}
case ModToolsEvent.OPEN_USER_CHATLOG: {
const castedEvent = (event as ModToolsOpenUserChatlogEvent);
if(openUserChatlogs && openUserChatlogs.includes(castedEvent.userId)) return;
const userChatlog = openUserChatlogs || [];
dispatchModToolsState({
type: ModToolsActions.SET_OPEN_USER_CHATLOGS,
payload: {
openUserChatlogs: [...userChatlog, castedEvent.userId]
}
});
return;
}
}
}, [openRooms, openRoomChatlogs, openUserInfo, openUserChatlogs]);
useUiEvent(ModToolsEvent.SHOW_MOD_TOOLS, onModToolsEvent); useUiEvent(ModToolsEvent.SHOW_MOD_TOOLS, onModToolsEvent);
useUiEvent(ModToolsEvent.HIDE_MOD_TOOLS, onModToolsEvent); useUiEvent(ModToolsEvent.HIDE_MOD_TOOLS, onModToolsEvent);
useUiEvent(ModToolsEvent.TOGGLE_MOD_TOOLS, onModToolsEvent); useUiEvent(ModToolsEvent.TOGGLE_MOD_TOOLS, onModToolsEvent);
useUiEvent(ModToolsEvent.OPEN_ROOM_INFO, onModToolsEvent);
useUiEvent(ModToolsEvent.OPEN_ROOM_CHATLOG, onModToolsEvent);
useUiEvent(ModToolsEvent.OPEN_USER_INFO, onModToolsEvent);
useUiEvent(ModToolsEvent.OPEN_USER_CHATLOG, onModToolsEvent);
const onRoomEngineEvent = useCallback((event: RoomEngineEvent) =>
{
switch(event.type)
{
case RoomEngineEvent.INITIALIZED:
dispatchModToolsState({
type: ModToolsActions.SET_CURRENT_ROOM_ID,
payload: {
currentRoomId: event.roomId
}
});
return;
case RoomEngineEvent.DISPOSED:
dispatchModToolsState({
type: ModToolsActions.SET_CURRENT_ROOM_ID,
payload: {
currentRoomId: null
}
});
return;
}
}, [ dispatchModToolsState ]);
useRoomEngineEvent(RoomEngineEvent.INITIALIZED, onRoomEngineEvent);
useRoomEngineEvent(RoomEngineEvent.DISPOSED, onRoomEngineEvent);
const onRoomEngineObjectEvent = useCallback((event: RoomEngineObjectEvent) => const onRoomEngineObjectEvent = useCallback((event: RoomEngineObjectEvent) =>
{ {
@ -276,8 +185,6 @@ export const ModToolsView: FC<ModToolsViewProps> = props =>
} }
}, [openRooms, currentRoomId, openRoomChatlogs, selectedUser, openUserInfo, openUserChatlogs]); }, [openRooms, currentRoomId, openRoomChatlogs, selectedUser, openUserInfo, openUserChatlogs]);
if(!isVisible) return null;
return ( return (
<ModToolsContextProvider value={ { modToolsState, dispatchModToolsState } }> <ModToolsContextProvider value={ { modToolsState, dispatchModToolsState } }>
<ModToolsMessageHandler /> <ModToolsMessageHandler />