nitro-react/src/views/notification-center/NotificationCenterMessageHandler.tsx

93 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-08-22 07:00:05 +02:00
import { AchievementNotificationMessageEvent, HabboBroadcastMessageEvent, HotelWillShutdownEvent, ModeratorMessageEvent, MOTDNotificationEvent, NotificationDialogMessageEvent, RespectReceivedEvent } from '@nitrots/nitro-renderer';
2021-06-22 18:40:28 +02:00
import { FC, useCallback } from 'react';
2021-08-22 07:00:05 +02:00
import { GetSessionDataManager, LocalizeBadgeName, LocalizeText } from '../../api';
2021-07-08 07:46:59 +02:00
import { NotificationCenterAlertEvent } from '../../events';
import { dispatchUiEvent } from '../../hooks/events';
2021-06-22 18:40:28 +02:00
import { CreateMessageHook } from '../../hooks/messages';
2021-08-05 19:32:09 +02:00
import { HotelWillShutdownNotification } from './common/HotelWillShutdownNotification';
2021-08-22 07:00:05 +02:00
import { NotificationType } from './common/NotificationType';
import { NotificationUtilities } from './common/NotificationUtilities';
2021-06-22 18:40:28 +02:00
import { useNotificationCenterContext } from './context/NotificationCenterContext';
import { INotificationCenterMessageHandlerProps } from './NotificationCenterMessageHandler.types';
import { NotificationCenterActions } from './reducers/NotificationCenterReducer';
export const NotificationCenterMessageHandler: FC<INotificationCenterMessageHandlerProps> = props =>
{
const { dispatchNotificationCenterState = null } = useNotificationCenterContext();
const onHabboBroadcastMessageEvent = useCallback((event: HabboBroadcastMessageEvent) =>
{
const parser = event.getParser();
2021-07-08 07:46:59 +02:00
dispatchUiEvent(new NotificationCenterAlertEvent(NotificationCenterAlertEvent.HOTEL_ALERT, [ parser.message ]));
}, []);
2021-06-22 18:40:28 +02:00
2021-07-08 07:46:59 +02:00
const onModeratorMessageEvent = useCallback((event: ModeratorMessageEvent) =>
2021-06-22 18:40:28 +02:00
{
const parser = event.getParser();
2021-07-08 07:46:59 +02:00
dispatchUiEvent(new NotificationCenterAlertEvent(NotificationCenterAlertEvent.HOTEL_ALERT, [ parser.message ], parser.link));
}, []);
2021-06-22 18:40:28 +02:00
2021-07-08 07:46:59 +02:00
const onMOTDNotificationEvent = useCallback((event: MOTDNotificationEvent) =>
2021-06-22 18:40:28 +02:00
{
const parser = event.getParser();
2021-07-08 07:46:59 +02:00
dispatchUiEvent(new NotificationCenterAlertEvent(NotificationCenterAlertEvent.HOTEL_ALERT, parser.messages));
}, []);
2021-06-22 18:40:28 +02:00
2021-07-08 07:46:59 +02:00
const onHotelWillShutdownEvent = useCallback((event: HotelWillShutdownEvent) =>
2021-06-22 18:40:28 +02:00
{
const parser = event.getParser();
dispatchNotificationCenterState({
type: NotificationCenterActions.ADD_NOTIFICATION,
payload: {
2021-07-08 07:46:59 +02:00
notification: new HotelWillShutdownNotification(parser.minutes)
2021-06-22 18:40:28 +02:00
}
});
}, [ dispatchNotificationCenterState ]);
2021-07-08 07:46:59 +02:00
const onNotificationDialogMessageEvent = useCallback((event: NotificationDialogMessageEvent) =>
2021-06-22 18:40:28 +02:00
{
const parser = event.getParser();
2021-08-22 07:00:05 +02:00
NotificationUtilities.showNotification(parser.type, parser.parameters);
}, []);
2021-08-05 19:32:09 +02:00
2021-08-22 07:00:05 +02:00
const onRespectReceivedEvent = useCallback((event: RespectReceivedEvent) =>
{
const parser = event.getParser();
if(parser.userId !== GetSessionDataManager().userId) return;
const text1 = LocalizeText('notifications.text.respect.1');
const text2 = LocalizeText('notifications.text.respect.2', [ 'count' ], [ parser.respectsReceived.toString() ]);
NotificationUtilities.showSingleBubble(text1, NotificationType.RESPECT);
NotificationUtilities.showSingleBubble(text2, NotificationType.RESPECT);
}, []);
const onAchievementNotificationMessageEvent = useCallback((event: AchievementNotificationMessageEvent) =>
{
const parser = event.getParser();
const text1 = LocalizeText('achievements.levelup.desc');
const badgeName = LocalizeBadgeName(parser.data.badgeCode);
const badgeImage = GetSessionDataManager().getBadgeUrl(parser.data.badgeCode);
const internalLink = 'questengine/achievements/' + parser.data.category;
NotificationUtilities.showSingleBubble((text1 + ' ' + badgeName), NotificationType.ACHIEVEMENT, badgeImage, internalLink);
}, []);
2021-06-22 18:40:28 +02:00
CreateMessageHook(HabboBroadcastMessageEvent, onHabboBroadcastMessageEvent);
CreateMessageHook(ModeratorMessageEvent, onModeratorMessageEvent);
CreateMessageHook(MOTDNotificationEvent, onMOTDNotificationEvent);
CreateMessageHook(HotelWillShutdownEvent, onHotelWillShutdownEvent);
2021-07-08 07:46:59 +02:00
CreateMessageHook(NotificationDialogMessageEvent, onNotificationDialogMessageEvent);
2021-08-22 07:00:05 +02:00
CreateMessageHook(RespectReceivedEvent, onRespectReceivedEvent);
CreateMessageHook(AchievementNotificationMessageEvent, onAchievementNotificationMessageEvent);
2021-06-22 18:40:28 +02:00
return null;
}