Refactor New message notifications

This commit is contained in:
Jeremie Pardou-Piquemal 2022-12-16 09:09:47 +01:00
parent 31db176328
commit bbee09fe48
4 changed files with 94 additions and 100 deletions

View File

@ -1,5 +1,5 @@
/* eslint-disable */ /* eslint-disable */
import React, { Component } from 'react'; import Reactfrom 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { COMMIT_SHA } from '@/config/env'; import { COMMIT_SHA } from '@/config/env';

View File

@ -1,9 +1,9 @@
import React, { Component } from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import Tinycon from 'tinycon'; import Tinycon from 'tinycon';
import { notify, beep } from '@/utils/notifications'; import { notify, beep } from '@/utils/notifications';
import { toggleNotificationAllowed, toggleNotificationEnabled } from '@/actions'; import { toggleNotificationAllowed } from '@/actions';
const mapStateToProps = state => { const mapStateToProps = state => {
return { return {
@ -19,19 +19,9 @@ const mapStateToProps = state => {
const mapDispatchToProps = { const mapDispatchToProps = {
toggleNotificationAllowed, toggleNotificationAllowed,
toggleNotificationEnabled,
}; };
const WithNewMessageNotification = WrappedComponent => { const WithNewMessageNotification = ({
return connect(
mapStateToProps,
mapDispatchToProps,
)(
class WithNotificationHOC extends Component {
state = { lastMessage: null, unreadMessageCount: 0 };
static getDerivedStateFromProps(nextProps, prevState) {
const {
room: { id: roomId }, room: { id: roomId },
activities, activities,
notificationIsEnabled, notificationIsEnabled,
@ -39,16 +29,21 @@ const WithNewMessageNotification = WrappedComponent => {
soundIsEnabled, soundIsEnabled,
unreadMessageCount, unreadMessageCount,
windowIsFocused, windowIsFocused,
} = nextProps; toggleNotificationAllowed,
children,
}) => {
const [lastMessage, setLastMessage] = React.useState(null);
const [lastUnreadMessageCount, setLastUnreadMessageCount] = React.useState(0);
React.useEffect(() => {
if (activities.length === 0) { if (activities.length === 0) {
return null; return;
} }
const currentLastMessage = activities[activities.length - 1];
const { username, type, text, fileName, locked, newUsername, currentUsername, action } = currentLastMessage;
const lastMessage = activities[activities.length - 1]; if (currentLastMessage !== lastMessage && !windowIsFocused) {
const { username, type, text, fileName, locked, newUsername, currentUsername, action } = lastMessage; setLastMessage(currentLastMessage);
if (lastMessage !== prevState.lastMessage && !windowIsFocused) {
if (notificationIsAllowed && notificationIsEnabled) { if (notificationIsAllowed && notificationIsEnabled) {
// Generate the proper notification according to the message type // Generate the proper notification according to the message type
switch (type) { switch (type) {
@ -84,44 +79,38 @@ const WithNewMessageNotification = WrappedComponent => {
if (soundIsEnabled) beep.play(); if (soundIsEnabled) beep.play();
} }
if (unreadMessageCount !== prevState.unreadMessageCount) { if (unreadMessageCount !== lastUnreadMessageCount) {
setLastUnreadMessageCount(unreadMessageCount);
Tinycon.setBubble(unreadMessageCount); Tinycon.setBubble(unreadMessageCount);
} }
}, [
return { lastMessage, unreadMessageCount };
}
componentDidMount() {
switch (Notification.permission) {
case 'granted':
this.props.toggleNotificationAllowed(true);
break;
case 'denied':
this.props.toggleNotificationAllowed(false);
break;
default:
this.props.toggleNotificationAllowed(null);
}
}
render() {
// Filter props
const {
room,
activities, activities,
lastMessage,
lastUnreadMessageCount,
notificationIsAllowed,
notificationIsEnabled, notificationIsEnabled,
motificationIsAllowed, roomId,
soundIsEnabled, soundIsEnabled,
unreadMessageCount, unreadMessageCount,
windowIsFocused, windowIsFocused,
toggleNotificationAllowed, ]);
toggleNotificationnEnabled,
...rest React.useEffect(() => {
} = this.props; switch (Notification.permission) {
return <WrappedComponent {...rest} />; case 'granted':
toggleNotificationAllowed(true);
break;
case 'denied':
toggleNotificationAllowed(false);
break;
default:
toggleNotificationAllowed(null);
} }
}, }, [toggleNotificationAllowed]);
);
return <>{children}</>;
}; };
export default WithNewMessageNotification; const ConnectedWithNewMessageNotification = connect(mapStateToProps, mapDispatchToProps)(WithNewMessageNotification);
export default ConnectedWithNewMessageNotification;

View File

@ -63,11 +63,16 @@ const mapDispatchToProps = {
setLanguage, setLanguage,
}; };
export const ConnectedHome = WithNewMessageNotification(connect(mapStateToProps, mapDispatchToProps)(Home)); export const ConnectedHome = connect(mapStateToProps, mapDispatchToProps)(Home);
const HomeWithParams = ({ ...props }) => { const HomeWithParams = ({ ...props }) => {
const socketId = useLoaderData(); const socketId = useLoaderData();
return <ConnectedHome socketId={socketId} {...props} />; return (
<WithNewMessageNotification>
<ConnectedHome socketId={socketId} {...props} />
</WithNewMessageNotification>
);
// return <WithNewMessageNotification wrappedComponent={} />;
}; };
export default HomeWithParams; export default HomeWithParams;

View File

@ -1,4 +1,4 @@
import React, { Component } from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Copy } from 'react-feather'; import { Copy } from 'react-feather';
import Clipboard from 'clipboard'; import Clipboard from 'clipboard';