Messages bugfix again :D (#164)

* Handle offline messages | Fetch messages when broadcasted | Fetch message groups

* Fetch messages on notification click

* Dont fetch groups if duplicate group

* Only submit new message on click submit

* Fix infinite loading when duplicated group is creatded

* Fix infinite loading when group already existed

* Make code prettier

* Centralize fetch messages on goToConversation

* Remove duplicated event

* Improve message groups mapping to include yourself for unreadCount + Fix redirection after adding contact + General improvements

* Remove conflicts

* Fix media paths
This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-03-18 23:01:54 -03:00
1 parent a2c0f94bd8
commit 0ec2b682e7
12 files changed
+132 -69

No files matched your search

@@ -16,7 +16,7 @@ export const ExampleApp = () => {
const exampleString = useExample();
const example = useApp('EXAMPLE');
const [soundName, setSound] = useState('pixel.ogg');
const { play, playing, stop } = useSound('./media/ringtones/' + soundName);
const { play, playing, stop } = useSound('media/ringtones/' + soundName);
const toggleSound = () => {
if (soundName === 'pixel.ogg') {
@@ -1,4 +1,4 @@
import React, { FormEvent, useCallback, useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Box, Button, TextField } from '@material-ui/core';
@@ -24,27 +24,22 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
const isGroupChat = participants.length > 1;
const handleSubmit = useCallback(
(event: FormEvent) => {
event.preventDefault();
const handleSubmit = useCallback(() => {
// handles phone numbers in a csv format and strips all spaces and
// external characters out of them:
// 123-4567, 987-6543, 333-4444
const phoneNumbers = participants.map(({ number }) => number.replace(/[^0-9]/g, ''));
const labelValue = isGroupChat ? label.trim() : null;
// handles phone numbers in a csv format and strips all spaces and
// external characters out of them:
// 123-4567, 987-6543, 333-4444
const phoneNumbers = participants.map(({ number }) => number.replace(/[^0-9]/g, ''));
const labelValue = isGroupChat ? label.trim() : null;
if (phoneNumbers.length) {
Nui.send('phone:createMessageGroup', {
phoneNumbers,
label: labelValue,
});
setParticipants([]);
history.push('/messages');
}
},
[history, label, participants, isGroupChat],
);
if (phoneNumbers.length) {
Nui.send('phone:createMessageGroup', {
phoneNumbers,
label: labelValue,
});
setParticipants([]);
history.push('/messages');
}
}, [history, label, participants, isGroupChat]);
const onAutocompleteChange = (_e, value: any) => {
const lastIdx = value.length - 1;
@@ -77,7 +72,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
const submitDisabled = !participants.length || (isGroupChat && !label);
return (
<form onSubmit={handleSubmit}>
<Box>
<Box px={2} py={3}>
<Autocomplete<Contact, boolean, boolean, boolean>
value={participants}
@@ -102,6 +97,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
)}
<Box px={2} py={3}>
<Button
onClick={() => handleSubmit()}
disabled={submitDisabled}
variant="contained"
fullWidth
@@ -111,7 +107,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
{t('APPS_MESSAGES_NEW_MESSAGE_GROUP_SUBMIT')}
</Button>
</Box>
</form>
</Box>
);
};
@@ -23,7 +23,7 @@ const MessageGroupItem = ({ messageGroup, handleClick }: IProps): any => {
<ListItem key={messageGroup.groupId} onClick={handleClick(messageGroup)} divider button>
<ListItemAvatar>
<Badge color="error" variant="dot" invisible={!hasUnread}>
<MuiAvatar src={messageGroup?.avatar} />
<MuiAvatar src={messageGroup.avatar} />
</Badge>
</ListItemAvatar>
<ListItemText>{messageGroup.label || messageGroup.groupDisplay}</ListItemText>
@@ -15,7 +15,13 @@ const MessagesList = (): any => {
const history = useHistory();
const { t } = useTranslation();
const { messageGroups, createMessageGroupResult, clearMessageGroupResult } = useMessages();
const {
messageGroups,
createMessageGroupResult,
clearMessageGroupResult,
getMessageGroupById,
setActiveMessageGroup,
} = useMessages();
const [searchValue, setSearchValue] = useState('');
@@ -32,18 +38,24 @@ const MessagesList = (): any => {
useEffect(() => {
if (createMessageGroupResult?.groupId) {
const findGroup = messageGroups.find((g) => g.groupId === createMessageGroupResult.groupId);
const findGroup = getMessageGroupById(createMessageGroupResult.groupId);
clearMessageGroupResult();
if (findGroup) {
goToConversation(findGroup, history);
}
}
}, [messageGroups, createMessageGroupResult, clearMessageGroupResult, history]);
}, [
messageGroups,
createMessageGroupResult,
clearMessageGroupResult,
history,
setActiveMessageGroup,
getMessageGroupById,
]);
if (!messageGroups) return null;
const handleClick = (messageGroup: MessageGroup) => () => {
Nui.send('phone:fetchMessages', { groupId: messageGroup.groupId });
goToConversation(messageGroup, history);
};
@@ -77,7 +77,6 @@ export const MessageModal = () => {
useEffect(() => {
if (!groupId) return;
setActiveMessageGroup(groupId);
}, [groupId, setActiveMessageGroup]);
@@ -109,10 +108,11 @@ export const MessageModal = () => {
const handleAddContact = (number) => {
const exists = getContactByNumber(number);
const referal = encodeURIComponent(pathname);
if (exists) {
return history.push(`/contacts/${exists.id}/?referal=${pathname}`);
return history.push(`/contacts/${exists.id}/?referal=${referal}`);
}
return history.push(`/contacts/-1/?addNumber=${number}&referal=${pathname}`);
return history.push(`/contacts/-1/?addNumber=${number}&referal=${referal}`);
};
const phoneNumbers = activeMessageGroup?.phoneNumbers || [];
@@ -164,7 +164,7 @@ export const MessageModal = () => {
) : null}
</Box>
</Paper>
{isLoaded ? (
{isLoaded && activeMessageGroup ? (
<Conversation
onClickDisplay={handleAddContact}
messages={messages}
@@ -10,11 +10,7 @@ const NOTIFICATION_ID = 'messages:broadcast';
export const useMessageNotifications = () => {
const { t } = useTranslation();
const history = useHistory();
const {
removeId,
addNotification,
addNotificationAlert,
} = useNotifications();
const { removeId, addNotification, addNotificationAlert } = useNotifications();
const { icon, notificationIcon } = useApp('MESSAGES');
const { getMessageGroupById } = useMessages();
@@ -1,12 +1,16 @@
import { useSetRecoilState } from 'recoil';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { messageState } from './state';
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
import { IAlert, useSnackbar } from '../../../ui/hooks/useSnackbar';
import { useTranslation } from 'react-i18next';
import { useMessageNotifications } from './useMessageNotifications';
import { useCallback } from 'react';
import Nui from '../../../os/nui-events/utils/Nui';
export const useMessagesService = () => {
const [activeMessageGroup, setActiveMessageGroup] = useRecoilState(
messageState.activeMessageGroup,
);
const setMessageGroups = useSetRecoilState(messageState.messageGroups);
const setMessages = useSetRecoilState(messageState.messages);
const setCreateMessageGroupResult = useSetRecoilState(messageState.createMessageGroupResult);
@@ -24,10 +28,32 @@ export const useMessagesService = () => {
[addAlert, t],
);
useNuiEvent('MESSAGES', 'phone:fetchMessageGroupsSuccess', setMessageGroups);
const handleMessageBroadcast = useCallback(
({ groupId, message }) => {
if (groupId === activeMessageGroup?.groupId) {
Nui.send('phone:fetchMessages', { groupId: activeMessageGroup.groupId });
}
setNotification({ groupId, message });
},
[activeMessageGroup, setNotification],
);
const _setMessageGroups = useCallback(
(groups) => {
if (activeMessageGroup && activeMessageGroup.groupId) {
setActiveMessageGroup(
(curr) => groups.find((g) => g.groupId === activeMessageGroup.groupId) || curr,
);
}
setMessageGroups(groups);
},
[activeMessageGroup, setActiveMessageGroup, setMessageGroups],
);
useNuiEvent('MESSAGES', 'phone:fetchMessageGroupsSuccess', _setMessageGroups);
useNuiEvent('MESSAGES', 'phone:fetchMessagesSuccess', setMessages);
useNuiEvent('MESSAGES', 'phone:createMessageGroupSuccess', setCreateMessageGroupResult);
useNuiEvent('MESSAGES', 'phone:createMessageGroupFailed', setCreateMessageGroupResult);
useNuiEvent('MESSAGES', 'createMessagesBroadcast', setNotification);
useNuiEvent('MESSAGES', 'createMessagesBroadcast', handleMessageBroadcast);
useNuiEvent('MESSAGES', 'phone:setMessagesAlert', handleAddAlert);
};
@@ -1,2 +1,7 @@
export const goToConversation = (messageGroup, history) =>
import Nui from '../../../os/nui-events/utils/Nui';
export const goToConversation = (messageGroup, history) => {
if (!messageGroup?.groupId || !history) return;
history.push(`/messages/conversations/${messageGroup.groupId}`);
Nui.send('phone:fetchMessages', { groupId: messageGroup.groupId });
};
@@ -1,4 +1,3 @@
export const getRingtonePath = (name?: string) => `./media/ringtones/${name || 'pixel'}.ogg`;
export const getRingtonePath = (name?: string) => `media/ringtones/${name || 'pixel'}.ogg`;
export const getNotificationPath = (name?: string) =>
`./media/notifications/${name || 'online'}.ogg`;
export const getNotificationPath = (name?: string) => `media/notifications/${name || 'online'}.ogg`;
+2 -2
View File
@@ -1,5 +1,5 @@
export const IMG_DEFAULT_AVATAR = './media/twitter/egg.png';
export const IMG_INVALID_AVATAR = './media/twitter/invalid.png';
export const IMG_DEFAULT_AVATAR = 'media/twitter/egg.png';
export const IMG_INVALID_AVATAR = 'media/twitter/invalid.png';
export const APP_TWITTER = 'TWITTER';
export const SETTING_MENTIONS = 'mentions';