mattermost-mobile/app/screens/notification/index.tsx
Kyriakos Z 6896129c73
MM-36980, MM-37030: Opens the thread on push notification and Notifications settings (#5618)
* MM-36980: Opens the thread on push notification

This commit enables tapping on a push notification for a reply to open
the thread.
This is done only if the user has CRT set to 'on'.

* Refactor, and handle in app notifications

* Minor change

* Fixes erroneous check

* Fixes on dismiss in app notification

* Fixes in app push notification for CRT

* Adds CRT notification settings for android

Adds support for CRT (*_threads) notification settings (notify_props).

Adds android comoponents to toggle those settings for push notifications
and email notifications.

* Adds CRT notification settings for iOS

Adds iOS components to toggle those CRT notifications settings
for push notifications and email notifications.

* Fixes bad JSON and intl ids

* Fixes i18n ids

* Fixes tests

* Adds email_threads to the default notify_props

* Fixes push_threads notify_prop default value

* Fixes style, and channel missing on thread open

* Fixes test

* Fixes click notification to open thread

Previous handling of opening the thread on notification clicked fell
into an infinite loop when the app was closed.
This commit fixes that by adding the post to selectedPostId reducer and
then emitting an event to open the thread on channel_base, and only if
the app was started by the notification.

When the app is in the background emitting the event from
handleNotification works just fine.

When the app is in the foreground the notification clicked gets handled
elsewhere and this commit does not change that.

* Removes reply settings when CRT is ON

"Mentions and Replies" section becomes just "Mentions" when the user has
the Collapsed Reply Threads set to "ON".

* Fixes prop types

Co-authored-by: Kyriakos Ziakoulis <koox00@Kyriakoss-MacBook-Pro.local>
2021-08-20 19:49:00 +03:00

216 lines
6.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useEffect, useRef, useState} from 'react';
import {Platform, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import * as Animatable from 'react-native-animatable';
import {PanGestureHandler} from 'react-native-gesture-handler';
import {Navigation} from 'react-native-navigation';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {useDispatch, useSelector} from 'react-redux';
import {popToRoot, dismissAllModals, dismissOverlay} from '@actions/navigation';
import {loadFromPushNotification} from '@actions/views/root';
import {NavigationTypes} from '@constants';
import {isCollapsedThreadsEnabled} from '@mm-redux/selectors/entities/preferences';
import EventEmitter from '@mm-redux/utils/event_emitter';
import {changeOpacity} from '@utils/theme';
import NotificationIcon from './notification_icon';
import NotificationTitle from './notification_title';
interface NotificationProps {
componentId: string;
notification: NotificationWithData;
}
interface SlideAnimation extends Animatable.CustomAnimation {
from: {
translateY: number;
};
to: {
translateY: number;
};
}
const AUTO_DISMISS_TIME_MILLIS = 5000;
const initialAnimation: SlideAnimation = {
from: {
translateY: -100,
},
to: {
translateY: 0,
},
};
const Notification = ({componentId, notification}: NotificationProps) => {
const [animation, setAnimation] = useState<SlideAnimation>(initialAnimation);
const insets = useSafeAreaInsets();
const dispatch = useDispatch();
const dismissTimerRef = useRef<NodeJS.Timeout | null>(null);
const tapped = useRef<boolean>(false);
const collapsedThreadsEnabled = useSelector(isCollapsedThreadsEnabled);
const animateDismissOverlay = () => {
cancelDismissTimer();
setAnimation({
from: {
translateY: 0,
},
to: {
translateY: -100,
},
});
dismissTimerRef.current = setTimeout(dismiss, 1000);
};
const cancelDismissTimer = () => {
if (dismissTimerRef.current) {
clearTimeout(dismissTimerRef.current);
}
};
const dismiss = () => {
cancelDismissTimer();
dismissOverlay(componentId);
};
const notificationTapped = () => {
tapped.current = true;
EventEmitter.emit(NavigationTypes.CLOSE_MAIN_SIDEBAR);
EventEmitter.emit(NavigationTypes.CLOSE_SETTINGS_SIDEBAR);
dismiss();
if (!notification.payload?.userInfo?.local) {
dispatch(loadFromPushNotification(notification));
}
};
useEffect(() => {
dismissTimerRef.current = setTimeout(() => {
if (!tapped.current) {
animateDismissOverlay();
}
}, AUTO_DISMISS_TIME_MILLIS);
return cancelDismissTimer;
}, []);
useEffect(() => {
const didDismissListener = Navigation.events().registerComponentDidDisappearListener(async ({componentId: screen}) => {
if (componentId === screen && tapped.current) {
await dismissAllModals();
await popToRoot();
const {root_id: rootId, channel_id: channelId} = notification.payload || {};
if (rootId && collapsedThreadsEnabled) {
EventEmitter.emit('goToThread', {id: rootId, channel_id: channelId});
}
}
});
return () => {
didDismissListener.remove();
};
}, []);
useEffect(() => {
EventEmitter.on(NavigationTypes.NAVIGATION_SHOW_OVERLAY, dismiss);
return () => EventEmitter.off(NavigationTypes.NAVIGATION_SHOW_OVERLAY, dismiss);
}, []);
const message = notification.payload?.body || notification.payload?.message;
return (
<PanGestureHandler
onGestureEvent={animateDismissOverlay}
minOffsetY={-20}
>
<Animatable.View
duration={250}
style={[styles.container, {height: (styles.container.height || 0) + (insets.top / 2), paddingTop: (insets.top / 2)}]}
useNativeDriver={true}
animation={animation}
testID='in_app_notification.screen'
>
<View style={styles.flex}>
<TouchableOpacity
style={styles.touchable}
onPress={notificationTapped}
activeOpacity={1}
>
<NotificationIcon
fromWebhook={notification.payload?.from_webhook === 'true'}
senderId={notification.payload?.sender_id || ''}
useUserIcon={notification.payload?.use_user_icon === 'true'}
overrideIconUrl={notification.payload?.override_icon_url}
/>
<View style={styles.titleContainer}>
<NotificationTitle channelName={notification.payload?.channel_name || ''}/>
<View style={styles.flex}>
<Text
numberOfLines={1}
ellipsizeMode='tail'
style={styles.message}
testID='in_app_notification.message'
>
{message}
</Text>
</View>
</View>
</TouchableOpacity>
</View>
</Animatable.View>
</PanGestureHandler>
);
};
const styles = StyleSheet.create({
container: {
alignItems: 'flex-start',
backgroundColor: changeOpacity('#000', 1),
flexDirection: 'row',
justifyContent: 'flex-start',
paddingHorizontal: 10,
width: '100%',
...Platform.select({
android: {
height: 68,
},
ios: {
height: 88,
},
}),
},
flex: {
flex: 1,
},
message: {
color: '#FFFFFF',
fontSize: 14,
},
titleContainer: {
flex: 1,
flexDirection: 'column',
alignSelf: 'stretch',
alignItems: 'flex-start',
marginLeft: 10,
...Platform.select({
android: {
marginTop: 5,
height: 50,
},
ios: {
paddingTop: 37,
},
}),
},
touchable: {
flex: 1,
flexDirection: 'row',
},
});
export default Notification;