2020-06-16 20:32:30 +00:00
|
|
|
import React, { memo, useEffect } from 'react';
|
|
|
|
import { NotifierRoot, Notifier, Easing } from 'react-native-notifier';
|
2021-02-11 21:42:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-02-26 16:01:45 +00:00
|
|
|
import { dequal } from 'dequal';
|
2020-06-16 20:32:30 +00:00
|
|
|
|
|
|
|
import NotifierComponent from './NotifierComponent';
|
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import Navigation from '../../lib/Navigation';
|
|
|
|
import { getActiveRoute } from '../../utils/navigation';
|
|
|
|
|
|
|
|
export const INAPP_NOTIFICATION_EMITTER = 'NotificationInApp';
|
|
|
|
|
2021-07-19 19:52:45 +00:00
|
|
|
const InAppNotification = memo(({ rooms, appState }: {rooms: any, appState: string}) => {
|
|
|
|
const show = (notification: any) => {
|
2021-03-05 16:10:21 +00:00
|
|
|
if (appState !== 'foreground') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:32:30 +00:00
|
|
|
const { payload } = notification;
|
2020-07-02 17:10:11 +00:00
|
|
|
const state = Navigation.navigationRef.current?.getRootState();
|
2020-06-16 20:32:30 +00:00
|
|
|
const route = getActiveRoute(state);
|
|
|
|
if (payload.rid) {
|
2021-02-11 21:42:50 +00:00
|
|
|
if (rooms.includes(payload.rid) || route?.name === 'JitsiMeetView') {
|
2020-06-16 20:32:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Notifier.showNotification({
|
|
|
|
showEasing: Easing.inOut(Easing.quad),
|
|
|
|
Component: NotifierComponent,
|
|
|
|
componentProps: {
|
|
|
|
notification
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-02-11 21:42:50 +00:00
|
|
|
const listener = EventEmitter.addEventListener(INAPP_NOTIFICATION_EMITTER, show);
|
2020-06-16 20:32:30 +00:00
|
|
|
return () => {
|
2021-02-11 21:42:50 +00:00
|
|
|
EventEmitter.removeListener(INAPP_NOTIFICATION_EMITTER, listener);
|
2020-06-16 20:32:30 +00:00
|
|
|
};
|
2021-02-11 21:42:50 +00:00
|
|
|
}, [rooms]);
|
2020-06-16 20:32:30 +00:00
|
|
|
|
|
|
|
return <NotifierRoot />;
|
2021-02-26 16:01:45 +00:00
|
|
|
}, (prevProps, nextProps) => dequal(prevProps.rooms, nextProps.rooms));
|
2021-02-11 21:42:50 +00:00
|
|
|
|
2021-07-19 19:52:45 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2021-03-05 16:10:21 +00:00
|
|
|
rooms: state.room.rooms,
|
|
|
|
appState: state.app.ready && state.app.foreground ? 'foreground' : 'background'
|
2020-06-16 20:32:30 +00:00
|
|
|
});
|
|
|
|
|
2021-02-11 21:42:50 +00:00
|
|
|
export default connect(mapStateToProps)(InAppNotification);
|