mirror of
https://github.com/darkwire/darkwire.io.git
synced 2025-07-26 21:38:14 +00:00
Refactor New message notifications
This commit is contained in:
parent
31db176328
commit
bbee09fe48
@ -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';
|
||||||
|
@ -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,109 +19,98 @@ const mapStateToProps = state => {
|
|||||||
|
|
||||||
const mapDispatchToProps = {
|
const mapDispatchToProps = {
|
||||||
toggleNotificationAllowed,
|
toggleNotificationAllowed,
|
||||||
toggleNotificationEnabled,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const WithNewMessageNotification = WrappedComponent => {
|
const WithNewMessageNotification = ({
|
||||||
return connect(
|
room: { id: roomId },
|
||||||
mapStateToProps,
|
activities,
|
||||||
mapDispatchToProps,
|
notificationIsEnabled,
|
||||||
)(
|
notificationIsAllowed,
|
||||||
class WithNotificationHOC extends Component {
|
soundIsEnabled,
|
||||||
state = { lastMessage: null, unreadMessageCount: 0 };
|
unreadMessageCount,
|
||||||
|
windowIsFocused,
|
||||||
|
toggleNotificationAllowed,
|
||||||
|
children,
|
||||||
|
}) => {
|
||||||
|
const [lastMessage, setLastMessage] = React.useState(null);
|
||||||
|
const [lastUnreadMessageCount, setLastUnreadMessageCount] = React.useState(0);
|
||||||
|
|
||||||
static getDerivedStateFromProps(nextProps, prevState) {
|
React.useEffect(() => {
|
||||||
const {
|
if (activities.length === 0) {
|
||||||
room: { id: roomId },
|
return;
|
||||||
activities,
|
}
|
||||||
notificationIsEnabled,
|
const currentLastMessage = activities[activities.length - 1];
|
||||||
notificationIsAllowed,
|
const { username, type, text, fileName, locked, newUsername, currentUsername, action } = currentLastMessage;
|
||||||
soundIsEnabled,
|
|
||||||
unreadMessageCount,
|
|
||||||
windowIsFocused,
|
|
||||||
} = nextProps;
|
|
||||||
|
|
||||||
if (activities.length === 0) {
|
if (currentLastMessage !== lastMessage && !windowIsFocused) {
|
||||||
return null;
|
setLastMessage(currentLastMessage);
|
||||||
}
|
if (notificationIsAllowed && notificationIsEnabled) {
|
||||||
|
// Generate the proper notification according to the message type
|
||||||
const lastMessage = activities[activities.length - 1];
|
switch (type) {
|
||||||
const { username, type, text, fileName, locked, newUsername, currentUsername, action } = lastMessage;
|
case 'USER_ENTER':
|
||||||
|
notify(`User ${username} joined`);
|
||||||
if (lastMessage !== prevState.lastMessage && !windowIsFocused) {
|
|
||||||
if (notificationIsAllowed && notificationIsEnabled) {
|
|
||||||
// Generate the proper notification according to the message type
|
|
||||||
switch (type) {
|
|
||||||
case 'USER_ENTER':
|
|
||||||
notify(`User ${username} joined`);
|
|
||||||
break;
|
|
||||||
case 'USER_EXIT':
|
|
||||||
notify(`User ${username} left`);
|
|
||||||
break;
|
|
||||||
case 'RECEIVE_FILE':
|
|
||||||
notify(`${username} sent file <${fileName}>`);
|
|
||||||
break;
|
|
||||||
case 'TEXT_MESSAGE':
|
|
||||||
notify(`${username} said:`, text);
|
|
||||||
break;
|
|
||||||
case 'USER_ACTION':
|
|
||||||
notify(`${username} ${action}`);
|
|
||||||
break;
|
|
||||||
case 'CHANGE_USERNAME':
|
|
||||||
notify(`${currentUsername} changed their name to ${newUsername}`);
|
|
||||||
break;
|
|
||||||
case 'TOGGLE_LOCK_ROOM':
|
|
||||||
if (locked) {
|
|
||||||
notify(`Room ${roomId} is now locked`);
|
|
||||||
} else {
|
|
||||||
notify(`Room ${roomId} is now unlocked`);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (soundIsEnabled) beep.play();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (unreadMessageCount !== prevState.unreadMessageCount) {
|
|
||||||
Tinycon.setBubble(unreadMessageCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
return { lastMessage, unreadMessageCount };
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
switch (Notification.permission) {
|
|
||||||
case 'granted':
|
|
||||||
this.props.toggleNotificationAllowed(true);
|
|
||||||
break;
|
break;
|
||||||
case 'denied':
|
case 'USER_EXIT':
|
||||||
this.props.toggleNotificationAllowed(false);
|
notify(`User ${username} left`);
|
||||||
|
break;
|
||||||
|
case 'RECEIVE_FILE':
|
||||||
|
notify(`${username} sent file <${fileName}>`);
|
||||||
|
break;
|
||||||
|
case 'TEXT_MESSAGE':
|
||||||
|
notify(`${username} said:`, text);
|
||||||
|
break;
|
||||||
|
case 'USER_ACTION':
|
||||||
|
notify(`${username} ${action}`);
|
||||||
|
break;
|
||||||
|
case 'CHANGE_USERNAME':
|
||||||
|
notify(`${currentUsername} changed their name to ${newUsername}`);
|
||||||
|
break;
|
||||||
|
case 'TOGGLE_LOCK_ROOM':
|
||||||
|
if (locked) {
|
||||||
|
notify(`Room ${roomId} is now locked`);
|
||||||
|
} else {
|
||||||
|
notify(`Room ${roomId} is now unlocked`);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
this.props.toggleNotificationAllowed(null);
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (soundIsEnabled) beep.play();
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
if (unreadMessageCount !== lastUnreadMessageCount) {
|
||||||
// Filter props
|
setLastUnreadMessageCount(unreadMessageCount);
|
||||||
const {
|
Tinycon.setBubble(unreadMessageCount);
|
||||||
room,
|
}
|
||||||
activities,
|
}, [
|
||||||
notificationIsEnabled,
|
activities,
|
||||||
motificationIsAllowed,
|
lastMessage,
|
||||||
soundIsEnabled,
|
lastUnreadMessageCount,
|
||||||
unreadMessageCount,
|
notificationIsAllowed,
|
||||||
windowIsFocused,
|
notificationIsEnabled,
|
||||||
toggleNotificationAllowed,
|
roomId,
|
||||||
toggleNotificationnEnabled,
|
soundIsEnabled,
|
||||||
...rest
|
unreadMessageCount,
|
||||||
} = this.props;
|
windowIsFocused,
|
||||||
return <WrappedComponent {...rest} />;
|
]);
|
||||||
}
|
|
||||||
},
|
React.useEffect(() => {
|
||||||
);
|
switch (Notification.permission) {
|
||||||
|
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;
|
||||||
|
@ -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;
|
||||||
|
@ -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';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user