mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
Tweaks / Message notifications (#174)
* Remove msg notifications when opening one | Dont alert notifications when inside that conversation * Fix msg events * Fix goToConversation util * Fix nuiEvents for messages
This commit is contained in:
1 parent
b42ee97450
commit
a3d7c19f44
3 files changed
+33
-11
No files matched your search
@@ -1,5 +1,6 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { matchPath, useHistory } from 'react-router-dom';
|
||||||
import { useApp } from '../../../os/apps/hooks/useApps';
|
import { useApp } from '../../../os/apps/hooks/useApps';
|
||||||
import { useNotifications } from '../../../os/notifications/hooks/useNotifications';
|
import { useNotifications } from '../../../os/notifications/hooks/useNotifications';
|
||||||
import { goToConversation } from '../utils/goToConversation';
|
import { goToConversation } from '../utils/goToConversation';
|
||||||
@@ -12,7 +13,20 @@ export const useMessageNotifications = () => {
|
|||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const { removeId, addNotification, addNotificationAlert } = useNotifications();
|
const { removeId, addNotification, addNotificationAlert } = useNotifications();
|
||||||
const { icon, notificationIcon } = useApp('MESSAGES');
|
const { icon, notificationIcon } = useApp('MESSAGES');
|
||||||
const { getMessageGroupById } = useMessages();
|
const { getMessageGroupById, activeMessageGroup } = useMessages();
|
||||||
|
|
||||||
|
// Remove notifications from groups when opening them
|
||||||
|
history.listen((location) => {
|
||||||
|
if (
|
||||||
|
activeMessageGroup?.groupId &&
|
||||||
|
matchPath(location.pathname, {
|
||||||
|
path: `/messages/conversations/${activeMessageGroup.groupId}`,
|
||||||
|
exact: true,
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
removeId(`${NOTIFICATION_ID}:${activeMessageGroup.groupId}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const setNotification = ({ groupId, message }) => {
|
const setNotification = ({ groupId, message }) => {
|
||||||
const group = getMessageGroupById(groupId);
|
const group = getMessageGroupById(groupId);
|
||||||
|
|||||||
@@ -6,8 +6,11 @@ import { useTranslation } from 'react-i18next';
|
|||||||
import { useMessageNotifications } from './useMessageNotifications';
|
import { useMessageNotifications } from './useMessageNotifications';
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import Nui from '../../../os/nui-events/utils/Nui';
|
import Nui from '../../../os/nui-events/utils/Nui';
|
||||||
|
import { useLocation } from 'react-router';
|
||||||
|
import { MessageEvents } from '../../../../../typings/messages';
|
||||||
|
|
||||||
export const useMessagesService = () => {
|
export const useMessagesService = () => {
|
||||||
|
const { pathname } = useLocation();
|
||||||
const [activeMessageGroup, setActiveMessageGroup] = useRecoilState(
|
const [activeMessageGroup, setActiveMessageGroup] = useRecoilState(
|
||||||
messageState.activeMessageGroup,
|
messageState.activeMessageGroup,
|
||||||
);
|
);
|
||||||
@@ -31,11 +34,15 @@ export const useMessagesService = () => {
|
|||||||
const handleMessageBroadcast = useCallback(
|
const handleMessageBroadcast = useCallback(
|
||||||
({ groupId, message }) => {
|
({ groupId, message }) => {
|
||||||
if (groupId === activeMessageGroup?.groupId) {
|
if (groupId === activeMessageGroup?.groupId) {
|
||||||
Nui.send('phone:fetchMessages', { groupId: activeMessageGroup.groupId });
|
Nui.send(MessageEvents.FETCH_MESSAGES, { groupId: activeMessageGroup.groupId });
|
||||||
|
if (pathname.includes('messages/conversations')) {
|
||||||
|
// Dont trigger notification if conversation is open.
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
setNotification({ groupId, message });
|
setNotification({ groupId, message });
|
||||||
},
|
},
|
||||||
[activeMessageGroup, setNotification],
|
[activeMessageGroup, pathname, setNotification],
|
||||||
);
|
);
|
||||||
|
|
||||||
const _setMessageGroups = useCallback(
|
const _setMessageGroups = useCallback(
|
||||||
@@ -50,10 +57,10 @@ export const useMessagesService = () => {
|
|||||||
[activeMessageGroup, setActiveMessageGroup, setMessageGroups],
|
[activeMessageGroup, setActiveMessageGroup, setMessageGroups],
|
||||||
);
|
);
|
||||||
|
|
||||||
useNuiEvent('MESSAGES', 'phone:fetchMessageGroupsSuccess', _setMessageGroups);
|
useNuiEvent('MESSAGES', MessageEvents.FETCH_MESSAGE_GROUPS_SUCCESS, _setMessageGroups);
|
||||||
useNuiEvent('MESSAGES', 'phone:fetchMessagesSuccess', setMessages);
|
useNuiEvent('MESSAGES', MessageEvents.FETCH_MESSAGES_SUCCESS, setMessages);
|
||||||
useNuiEvent('MESSAGES', 'phone:createMessageGroupSuccess', setCreateMessageGroupResult);
|
useNuiEvent('MESSAGES', MessageEvents.CREATE_MESSAGE_GROUP_SUCCESS, setCreateMessageGroupResult);
|
||||||
useNuiEvent('MESSAGES', 'phone:createMessageGroupFailed', setCreateMessageGroupResult);
|
useNuiEvent('MESSAGES', MessageEvents.CREATE_MESSAGE_GROUP_FAILED, setCreateMessageGroupResult);
|
||||||
useNuiEvent('MESSAGES', 'createMessagesBroadcast', handleMessageBroadcast);
|
useNuiEvent('MESSAGES', MessageEvents.CREATE_MESSAGE_BROADCAST, handleMessageBroadcast);
|
||||||
useNuiEvent('MESSAGES', 'phone:setMessagesAlert', handleAddAlert);
|
useNuiEvent('MESSAGES', MessageEvents.ACTION_RESULT, handleAddAlert);
|
||||||
};
|
};
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
|
import { MessageEvents } from '../../../../../typings/messages';
|
||||||
import Nui from '../../../os/nui-events/utils/Nui';
|
import Nui from '../../../os/nui-events/utils/Nui';
|
||||||
|
|
||||||
export const goToConversation = (messageGroup, history) => {
|
export const goToConversation = (messageGroup, history) => {
|
||||||
if (!messageGroup?.groupId || !history) return;
|
if (!messageGroup?.groupId || !history) return;
|
||||||
history.push(`/messages/conversations/${messageGroup.groupId}`);
|
history.push(`/messages/conversations/${messageGroup.groupId}`);
|
||||||
Nui.send('phone:fetchMessages', { groupId: messageGroup.groupId });
|
Nui.send(MessageEvents.FETCH_MESSAGES, { groupId: messageGroup.groupId });
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user