2020-06-16 20:32:30 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { StyleSheet, Text, View } from 'react-native';
|
2020-06-16 20:32:30 +00:00
|
|
|
import Touchable from 'react-native-platform-touchable';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { Notifier } from 'react-native-notifier';
|
|
|
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
|
|
|
|
|
|
|
import Avatar from '../Avatar';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2020-06-16 20:32:30 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2020-06-16 20:32:30 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2022-04-20 21:37:54 +00:00
|
|
|
import { ROW_HEIGHT } from '../RoomItem';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { goRoom } from '../../lib/methods/helpers/goRoom';
|
2022-04-07 13:22:19 +00:00
|
|
|
import Navigation from '../../lib/navigation/appNavigation';
|
2020-06-17 17:35:58 +00:00
|
|
|
import { useOrientation } from '../../dimensions';
|
2022-03-22 14:01:55 +00:00
|
|
|
import { IApplicationState, ISubscription, SubscriptionType } from '../../definitions';
|
2020-06-16 20:32:30 +00:00
|
|
|
|
2022-03-22 14:01:55 +00:00
|
|
|
export interface INotifierComponent {
|
|
|
|
notification: {
|
|
|
|
text: string;
|
|
|
|
payload: {
|
|
|
|
sender: { username: string };
|
|
|
|
type: SubscriptionType;
|
|
|
|
} & Pick<ISubscription, '_id' | 'name' | 'rid' | 'prid'>;
|
|
|
|
title: string;
|
|
|
|
avatar: string;
|
|
|
|
};
|
2021-09-13 20:41:05 +00:00
|
|
|
isMasterDetail: boolean;
|
|
|
|
}
|
|
|
|
|
2020-06-16 20:32:30 +00:00
|
|
|
const AVATAR_SIZE = 48;
|
2021-09-13 20:41:05 +00:00
|
|
|
const BUTTON_HIT_SLOP = { top: 12, right: 12, bottom: 12, left: 12 };
|
2020-06-16 20:32:30 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
height: ROW_HEIGHT,
|
|
|
|
paddingHorizontal: 14,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
marginHorizontal: 10,
|
|
|
|
borderWidth: StyleSheet.hairlineWidth,
|
|
|
|
borderRadius: 4
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
inner: {
|
|
|
|
flex: 1
|
|
|
|
},
|
|
|
|
avatar: {
|
|
|
|
marginRight: 10
|
|
|
|
},
|
|
|
|
roomName: {
|
|
|
|
fontSize: 17,
|
|
|
|
lineHeight: 20,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
message: {
|
|
|
|
fontSize: 14,
|
|
|
|
lineHeight: 17,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
},
|
|
|
|
close: {
|
|
|
|
marginLeft: 10
|
|
|
|
},
|
|
|
|
small: {
|
|
|
|
width: '50%',
|
|
|
|
alignSelf: 'center'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const hideNotification = () => Notifier.hideNotification();
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const NotifierComponent = React.memo(({ notification, isMasterDetail }: INotifierComponent) => {
|
2022-03-22 14:01:55 +00:00
|
|
|
const { theme } = useTheme();
|
2020-06-16 20:32:30 +00:00
|
|
|
const insets = useSafeAreaInsets();
|
2020-06-17 17:35:58 +00:00
|
|
|
const { isLandscape } = useOrientation();
|
2020-06-16 20:32:30 +00:00
|
|
|
|
2022-03-22 14:01:55 +00:00
|
|
|
const { text, payload } = notification;
|
2020-10-30 13:51:04 +00:00
|
|
|
const { type, rid } = payload;
|
2020-06-16 20:32:30 +00:00
|
|
|
const name = type === 'd' ? payload.sender.username : payload.name;
|
|
|
|
// if sub is not on local database, title and avatar will be null, so we use payload from notification
|
2022-03-22 14:01:55 +00:00
|
|
|
const { title = name, avatar = name } = notification;
|
2020-06-16 20:32:30 +00:00
|
|
|
|
|
|
|
const onPress = () => {
|
2021-07-02 19:17:47 +00:00
|
|
|
const { prid, _id } = payload;
|
2020-06-16 20:32:30 +00:00
|
|
|
if (!rid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const item = {
|
2021-09-13 20:41:05 +00:00
|
|
|
rid,
|
|
|
|
name: title,
|
|
|
|
t: type,
|
|
|
|
prid
|
2020-06-16 20:32:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('DrawerNavigator');
|
2020-07-17 17:39:59 +00:00
|
|
|
} else {
|
|
|
|
Navigation.navigate('RoomsListView');
|
2020-06-16 20:32:30 +00:00
|
|
|
}
|
2021-07-02 19:17:47 +00:00
|
|
|
goRoom({ item, isMasterDetail, jumpToMessageId: _id });
|
2020-06-16 20:32:30 +00:00
|
|
|
hideNotification();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-09-13 20:41:05 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.container,
|
|
|
|
(isMasterDetail || isLandscape) && styles.small,
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].focusedBackground,
|
|
|
|
borderColor: themes[theme].separatorColor,
|
|
|
|
marginTop: insets.top
|
|
|
|
}
|
|
|
|
]}>
|
2020-06-16 20:32:30 +00:00
|
|
|
<Touchable
|
|
|
|
style={styles.content}
|
|
|
|
onPress={onPress}
|
|
|
|
hitSlop={BUTTON_HIT_SLOP}
|
2021-09-13 20:41:05 +00:00
|
|
|
background={Touchable.SelectableBackgroundBorderless()}>
|
2020-06-16 20:32:30 +00:00
|
|
|
<>
|
2020-10-30 13:51:04 +00:00
|
|
|
<Avatar text={avatar} size={AVATAR_SIZE} type={type} rid={rid} style={styles.avatar} />
|
2020-06-16 20:32:30 +00:00
|
|
|
<View style={styles.inner}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Text style={[styles.roomName, { color: themes[theme].titleText }]} numberOfLines={1}>
|
|
|
|
{title}
|
|
|
|
</Text>
|
|
|
|
<Text style={[styles.message, { color: themes[theme].titleText }]} numberOfLines={1}>
|
|
|
|
{text}
|
|
|
|
</Text>
|
2020-06-16 20:32:30 +00:00
|
|
|
</View>
|
|
|
|
</>
|
|
|
|
</Touchable>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Touchable onPress={hideNotification} hitSlop={BUTTON_HIT_SLOP} background={Touchable.SelectableBackgroundBorderless()}>
|
2022-05-02 19:21:15 +00:00
|
|
|
<CustomIcon name='close' size={20} color={themes[theme].titleText} style={styles.close} />
|
2020-06-16 20:32:30 +00:00
|
|
|
</Touchable>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-03-22 14:01:55 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-06-16 20:32:30 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(NotifierComponent);
|