2023-07-04 00:03:39 +00:00
|
|
|
import React, { ElementType, memo, useEffect } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { Easing, Notifier, NotifierRoot } from 'react-native-notifier';
|
|
|
|
|
2022-03-22 14:01:55 +00:00
|
|
|
import NotifierComponent, { INotifierComponent } from './NotifierComponent';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../../lib/methods/helpers/events';
|
2022-04-07 13:22:19 +00:00
|
|
|
import Navigation from '../../lib/navigation/appNavigation';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { getActiveRoute } from '../../lib/methods/helpers/navigation';
|
2022-11-25 13:21:56 +00:00
|
|
|
import { useAppSelector } from '../../lib/hooks';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
export const INAPP_NOTIFICATION_EMITTER = 'NotificationInApp';
|
|
|
|
|
2022-11-25 13:21:56 +00:00
|
|
|
const InAppNotification = memo(() => {
|
|
|
|
const { appState, subscribedRoom } = useAppSelector(state => ({
|
|
|
|
subscribedRoom: state.room.subscribedRoom,
|
|
|
|
appState: state.app.ready && state.app.foreground ? 'foreground' : 'background'
|
|
|
|
}));
|
|
|
|
|
2023-07-04 00:03:39 +00:00
|
|
|
const show = (
|
|
|
|
notification: INotifierComponent['notification'] & {
|
|
|
|
customComponent?: ElementType;
|
|
|
|
customTime?: number;
|
|
|
|
customNotification?: boolean;
|
|
|
|
hideOnPress?: boolean;
|
|
|
|
swipeEnabled?: boolean;
|
2022-11-25 13:21:56 +00:00
|
|
|
}
|
2023-07-04 00:03:39 +00:00
|
|
|
) => {
|
|
|
|
if (appState !== 'foreground') return;
|
2022-11-25 13:21:56 +00:00
|
|
|
|
|
|
|
const { payload } = notification;
|
|
|
|
const state = Navigation.navigationRef.current?.getRootState();
|
|
|
|
const route = getActiveRoute(state);
|
2023-07-04 00:03:39 +00:00
|
|
|
if (payload?.rid || notification.customNotification) {
|
|
|
|
if (payload?.rid === subscribedRoom || route?.name === 'JitsiMeetView' || payload?.message?.t === 'videoconf') return;
|
|
|
|
|
2022-11-25 13:21:56 +00:00
|
|
|
Notifier.showNotification({
|
|
|
|
showEasing: Easing.inOut(Easing.quad),
|
2023-07-04 00:03:39 +00:00
|
|
|
Component: notification.customComponent || NotifierComponent,
|
2022-11-25 13:21:56 +00:00
|
|
|
componentProps: {
|
|
|
|
notification
|
2023-07-04 00:03:39 +00:00
|
|
|
},
|
|
|
|
duration: notification.customTime || 3000, // default 3s,
|
|
|
|
hideOnPress: notification.hideOnPress ?? true,
|
|
|
|
swipeEnabled: notification.swipeEnabled ?? true
|
2022-11-25 13:21:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const listener = EventEmitter.addEventListener(INAPP_NOTIFICATION_EMITTER, show);
|
|
|
|
return () => {
|
|
|
|
EventEmitter.removeListener(INAPP_NOTIFICATION_EMITTER, listener);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2022-11-25 13:21:56 +00:00
|
|
|
}, [subscribedRoom, appState]);
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-11-25 13:21:56 +00:00
|
|
|
return <NotifierRoot />;
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
|
|
|
|
2022-11-25 13:21:56 +00:00
|
|
|
export default InAppNotification;
|