2021-09-13 20:41:05 +00:00
|
|
|
import React, { memo, useEffect } from 'react';
|
|
|
|
import { Easing, Notifier, NotifierRoot } from 'react-native-notifier';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { dequal } from 'dequal';
|
|
|
|
|
2022-03-22 14:01:55 +00:00
|
|
|
import NotifierComponent, { INotifierComponent } from './NotifierComponent';
|
2021-09-13 20:41:05 +00:00
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import Navigation from '../../lib/Navigation';
|
|
|
|
import { getActiveRoute } from '../../utils/navigation';
|
2022-03-22 14:01:55 +00:00
|
|
|
import { IApplicationState } from '../../definitions';
|
|
|
|
import { IRoom } from '../../reducers/room';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
export const INAPP_NOTIFICATION_EMITTER = 'NotificationInApp';
|
|
|
|
|
|
|
|
const InAppNotification = memo(
|
2022-03-22 14:01:55 +00:00
|
|
|
({ rooms, appState }: { rooms: IRoom['rooms']; appState: string }) => {
|
|
|
|
const show = (notification: INotifierComponent['notification']) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
if (appState !== 'foreground') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { payload } = notification;
|
|
|
|
const state = Navigation.navigationRef.current?.getRootState();
|
|
|
|
const route = getActiveRoute(state);
|
|
|
|
if (payload.rid) {
|
|
|
|
if (rooms.includes(payload.rid) || route?.name === 'JitsiMeetView') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Notifier.showNotification({
|
|
|
|
showEasing: Easing.inOut(Easing.quad),
|
|
|
|
Component: NotifierComponent,
|
|
|
|
componentProps: {
|
|
|
|
notification
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const listener = EventEmitter.addEventListener(INAPP_NOTIFICATION_EMITTER, show);
|
|
|
|
return () => {
|
|
|
|
EventEmitter.removeListener(INAPP_NOTIFICATION_EMITTER, listener);
|
|
|
|
};
|
|
|
|
}, [rooms]);
|
|
|
|
|
|
|
|
return <NotifierRoot />;
|
|
|
|
},
|
|
|
|
(prevProps, nextProps) => dequal(prevProps.rooms, nextProps.rooms)
|
|
|
|
);
|
|
|
|
|
2022-03-22 14:01:55 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2021-09-13 20:41:05 +00:00
|
|
|
rooms: state.room.rooms,
|
|
|
|
appState: state.app.ready && state.app.foreground ? 'foreground' : 'background'
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(InAppNotification);
|