mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
feat/messages additions (in memory of Leonard Nimoy) (#118)
* feat(resouces/messages): broadcast messagse to participants * feat(phone/messages): notifications for messages * remove weird empty onNet * reformat? * last reformat! This pr will be squashed * tweak(phone/messages): send groupName to notification alert * fix identifer * fixing notification groupName * resbasee...i think * feat(messages): check for unread messages * send nui event is count > 0 * remove usless test * import.sql update * Refactor NotificationsProvider and fix messages notifications * Send groupId on message broadcast * Update phone/src/apps/messages/components/modal/Conversation.tsx * Update phone/src/apps/messages/hooks/useMessageNotifications.ts * Update phone/src/os/notifications/providers/NotificationsProvider.tsx * Update phone/src/common/typings/messages.ts * Update phone/src/apps/messages/hooks/useMessageNotifications.ts * clean sql file * Update resources/server/sv_messages.ts * Fetch message groups when other person created for you * fix sql * Select unreadCount from messageGroups * whatever husky again * Add members menu and add contact in messages * Add from group bubbles * Fix messages scroll with new msg * Message image menu flicker + justify bottom messages * Improve debugging * Add container to Slider * Improve logger * Fix create message * add creator identifier to group hash * Fix dafuq i broke * group_id bigger varchar in column * group_id bigger varchar in column * Phone visibility default browser * Add debugging Co-authored-by: J Francisco Rader <[email protected]> Co-authored-by: J Francisco Rader <[email protected]>
This commit is contained in:
38 files changed
+634
-328
No files matched your search
@@ -45,7 +45,12 @@ InjectDebugData([
|
||||
{
|
||||
id: 3,
|
||||
display: 'Chip',
|
||||
number: '777-7777',
|
||||
number: '111-1111',
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
display: 'Kidz',
|
||||
number: '444-4444',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Avatar as MuiAvatar, Box, Button, Paper, TextField } from '@material-ui/core';
|
||||
import { makeStyles, Theme } from '@material-ui/core/styles';
|
||||
import { makeStyles } from '@material-ui/core/styles';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useHistory, useParams } from 'react-router-dom';
|
||||
import { useContacts } from '../../hooks/useContacts';
|
||||
@@ -19,7 +19,7 @@ interface ContactInfoRouteQuery {
|
||||
referal?: string;
|
||||
}
|
||||
|
||||
const useStyles = makeStyles((theme: Theme) => ({
|
||||
const useStyles = makeStyles({
|
||||
root: {
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
@@ -44,7 +44,7 @@ const useStyles = makeStyles((theme: Theme) => ({
|
||||
inputProps: {
|
||||
fontSize: 22,
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
const ContactsInfoPage = () => {
|
||||
const classes = useStyles();
|
||||
|
||||
@@ -7,11 +7,13 @@ import { useCallback } from 'react';
|
||||
interface IUseContacts {
|
||||
contacts: Contact[];
|
||||
getDisplayByNumber: (number: string) => string;
|
||||
getContact: (id: number) => Contact;
|
||||
getContactByNumber: (number: string) => Contact | null;
|
||||
getContact: (id: number) => Contact | null;
|
||||
}
|
||||
|
||||
export const useContacts = (): IUseContacts => {
|
||||
const contacts = useRecoilValue(contactsState.contacts);
|
||||
|
||||
const getDisplayByNumber = useCallback(
|
||||
(number: string) => {
|
||||
const found = contacts.find((contact) => contact.number === number);
|
||||
@@ -19,13 +21,26 @@ export const useContacts = (): IUseContacts => {
|
||||
},
|
||||
[contacts],
|
||||
);
|
||||
|
||||
const getContactByNumber = useCallback(
|
||||
(number: string): Contact | null => {
|
||||
for (const contact of contacts) {
|
||||
if (contact.number === number) return contact;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
[contacts],
|
||||
);
|
||||
|
||||
const getContact = useCallback(
|
||||
(id: number): Contact | null => {
|
||||
for (const contact of contacts) {
|
||||
if (contact.id === id) return contact;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
[contacts],
|
||||
);
|
||||
return { contacts, getDisplayByNumber, getContact };
|
||||
|
||||
return { contacts, getDisplayByNumber, getContact, getContactByNumber };
|
||||
};
|
||||
@@ -33,6 +33,7 @@ export const MessagesApp = () => {
|
||||
render={() => (
|
||||
<>
|
||||
<MessagesList />
|
||||
<NewMessageGroupButton onClick={() => history.push('/messages/new')} />
|
||||
</>
|
||||
)}
|
||||
/>
|
||||
@@ -45,7 +46,6 @@ export const MessagesApp = () => {
|
||||
/>
|
||||
</Switch>
|
||||
</AppContent>
|
||||
<NewMessageGroupButton onClick={() => history.push('/messages/new')} />
|
||||
</AppWrapper>
|
||||
</MessagesThemeProvider>
|
||||
);
|
||||
@@ -63,6 +63,7 @@ InjectDebugData([
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
avatar: null,
|
||||
groupId: '2',
|
||||
isRead: true,
|
||||
isMine: false,
|
||||
updatedAt: Date.now(),
|
||||
@@ -74,6 +75,7 @@ InjectDebugData([
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
avatar: null,
|
||||
groupId: '2',
|
||||
isRead: true,
|
||||
isMine: false,
|
||||
updatedAt: Date.now(),
|
||||
@@ -81,80 +83,27 @@ InjectDebugData([
|
||||
{
|
||||
id: 3,
|
||||
message: 'sup!',
|
||||
user_identifier: '1234',
|
||||
user_identifier: '123',
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
avatar: null,
|
||||
groupId: '2',
|
||||
isRead: true,
|
||||
isMine: false,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
message: 'sup!',
|
||||
user_identifier: '1234',
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
id: 3,
|
||||
message: 'what are you doing!?',
|
||||
user_identifier: '12345',
|
||||
phone_number: '444-5555',
|
||||
display: '444-5555',
|
||||
avatar: null,
|
||||
groupId: '2',
|
||||
isRead: true,
|
||||
isMine: false,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
message: 'sup!',
|
||||
user_identifier: '1234',
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
avatar: null,
|
||||
isRead: true,
|
||||
isMine: false,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
message: 'sup!',
|
||||
user_identifier: '1234',
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
avatar: null,
|
||||
isRead: true,
|
||||
isMine: false,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
message: 'sup!',
|
||||
user_identifier: '1234',
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
avatar: null,
|
||||
isRead: true,
|
||||
isMine: true,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
message: 'sup!',
|
||||
user_identifier: '1234',
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
avatar: null,
|
||||
isRead: true,
|
||||
isMine: false,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
{
|
||||
id: 9,
|
||||
message: 'sup!',
|
||||
user_identifier: '1234',
|
||||
phone_number: '444-4444',
|
||||
display: 'kidz',
|
||||
avatar: null,
|
||||
isRead: true,
|
||||
isMine: true,
|
||||
updatedAt: Date.now(),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -165,10 +114,55 @@ InjectDebugData([
|
||||
groupId: '2',
|
||||
groupDisplay: 'dev-chat',
|
||||
isGroupChat: true,
|
||||
phoneNumbers: ['444-4444', '111-1134', '111-1111'],
|
||||
avatar: null,
|
||||
label: null,
|
||||
updatedAt: Date.now(),
|
||||
unreadCount: 2,
|
||||
},
|
||||
{
|
||||
groupId: '3',
|
||||
groupDisplay: 'Chip',
|
||||
isGroupChat: false,
|
||||
phoneNumbers: ['111-1134', '111-1111'],
|
||||
avatar: null,
|
||||
label: null,
|
||||
updatedAt: Date.now(),
|
||||
unreadCount: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
InjectDebugData(
|
||||
[
|
||||
{
|
||||
app: 'MESSAGES',
|
||||
method: 'createMessagesBroadcast',
|
||||
data: {
|
||||
groupId: '2',
|
||||
number: '777-7777',
|
||||
message: 'Hi! Just checking in on you!',
|
||||
},
|
||||
},
|
||||
{
|
||||
app: 'MESSAGES',
|
||||
method: 'createMessagesBroadcast',
|
||||
data: {
|
||||
groupId: '2',
|
||||
number: 'Kidz',
|
||||
message: 'Hi! You are awesome!',
|
||||
},
|
||||
},
|
||||
{
|
||||
app: 'MESSAGES',
|
||||
method: 'createMessagesBroadcast',
|
||||
data: {
|
||||
groupId: '3',
|
||||
number: 'Kidz',
|
||||
message: 'I am testing this like crazy',
|
||||
},
|
||||
},
|
||||
],
|
||||
3000,
|
||||
);
|
||||
@@ -8,6 +8,7 @@ import Nui from '../../../../os/nui-events/utils/Nui';
|
||||
interface IProps {
|
||||
onAddImageClick(): void;
|
||||
messageGroupId: string | undefined;
|
||||
messageGroupName: string | undefined;
|
||||
}
|
||||
|
||||
const useStyles = makeStyles({
|
||||
@@ -23,6 +24,7 @@ const MessageInput = ({ messageGroupId, onAddImageClick }: IProps) => {
|
||||
event.preventDefault();
|
||||
if (message.trim()) {
|
||||
// don't allow the user to submit white space
|
||||
|
||||
Nui.send('phone:sendMessage', {
|
||||
groupId: messageGroupId,
|
||||
message,
|
||||
|
||||
@@ -1,18 +1,30 @@
|
||||
import React from 'react';
|
||||
import { ListItem, ListItemText, ListItemAvatar, Avatar as MuiAvatar } from '@material-ui/core';
|
||||
import {
|
||||
ListItem,
|
||||
ListItemText,
|
||||
ListItemAvatar,
|
||||
Avatar as MuiAvatar,
|
||||
Badge,
|
||||
} from '@material-ui/core';
|
||||
|
||||
import { MessageGroup } from '../../../../common/typings/messages';
|
||||
|
||||
interface IProps {
|
||||
messageGroup: MessageGroup;
|
||||
handleClick: (group: MessageGroup) => () => void;
|
||||
}
|
||||
|
||||
const MessageGroupItem = ({ messageGroup, handleClick }: IProps): any => {
|
||||
// get unread messages, check the length. If we have any, get the groupId,
|
||||
// and show the badge
|
||||
|
||||
const hasUnread = messageGroup.unreadCount > 0;
|
||||
|
||||
return (
|
||||
<ListItem key={messageGroup.groupId} onClick={handleClick(messageGroup)} divider button>
|
||||
<ListItemAvatar>
|
||||
<MuiAvatar src={messageGroup?.avatar} />
|
||||
<Badge color='error' variant='dot' invisible={!hasUnread}>
|
||||
<MuiAvatar src={messageGroup?.avatar} />
|
||||
</Badge>
|
||||
</ListItemAvatar>
|
||||
<ListItemText>{messageGroup.label || messageGroup.groupDisplay}</ListItemText>
|
||||
</ListItem>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { List } from '@material-ui/core';
|
||||
import qs from 'qs';
|
||||
import { MessageGroup } from '../../../../common/typings/messages';
|
||||
import Nui from '../../../../os/nui-events/utils/Nui';
|
||||
import useMessages from '../../hooks/useMessages';
|
||||
@@ -8,6 +7,7 @@ import MessageSearch from './MessageSearch';
|
||||
import MessageGroupItem from './MessageGroupItem';
|
||||
import useStyles from './list.styles';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { goToConversation } from '../../utils/goToConversation';
|
||||
|
||||
const MessagesList = (): any => {
|
||||
const classes = useStyles();
|
||||
@@ -17,31 +17,6 @@ const MessagesList = (): any => {
|
||||
|
||||
const [searchValue, setSearchValue] = useState('');
|
||||
|
||||
const goToConversation = useCallback(
|
||||
(messageGroup) =>
|
||||
history.push(
|
||||
`/messages/conversations/${messageGroup.groupId}/?${qs.stringify(messageGroup)}`,
|
||||
),
|
||||
[history],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (createMessageGroupResult?.groupId) {
|
||||
const findGroup = messageGroups.find((g) => g.groupId === createMessageGroupResult.groupId);
|
||||
clearMessageGroupResult();
|
||||
if (findGroup) {
|
||||
goToConversation(findGroup);
|
||||
}
|
||||
}
|
||||
}, [messageGroups, createMessageGroupResult, goToConversation, clearMessageGroupResult]);
|
||||
|
||||
if (!messageGroups) return null;
|
||||
|
||||
const handleClick = (messageGroup: MessageGroup) => () => {
|
||||
Nui.send('phone:fetchMessages', { groupId: messageGroup.groupId });
|
||||
history.push(`/messages/conversations/${messageGroup.groupId}/?${qs.stringify(messageGroup)}`);
|
||||
};
|
||||
|
||||
const formattedSearch = searchValue.toLowerCase().trim();
|
||||
const filteredGroups = formattedSearch
|
||||
? messageGroups.filter((group) => {
|
||||
@@ -53,6 +28,23 @@ const MessagesList = (): any => {
|
||||
})
|
||||
: messageGroups;
|
||||
|
||||
useEffect(() => {
|
||||
if (createMessageGroupResult?.groupId) {
|
||||
const findGroup = messageGroups.find((g) => g.groupId === createMessageGroupResult.groupId);
|
||||
clearMessageGroupResult();
|
||||
if (findGroup) {
|
||||
goToConversation(findGroup, history);
|
||||
}
|
||||
}
|
||||
}, [messageGroups, createMessageGroupResult, clearMessageGroupResult, history]);
|
||||
|
||||
if (!messageGroups) return null;
|
||||
|
||||
const handleClick = (messageGroup: MessageGroup) => () => {
|
||||
Nui.send('phone:fetchMessages', { groupId: messageGroup.groupId });
|
||||
goToConversation(messageGroup, history);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<MessageSearch value={searchValue} handleChange={(e) => setSearchValue(e.target.value)} />
|
||||
|
||||
@@ -11,11 +11,12 @@ import { MessageBubble } from './MessageBubble';
|
||||
interface IProps {
|
||||
activeMessageGroup: MessageGroup;
|
||||
messages: Message[];
|
||||
onClickDisplay(phoneNumber: string): void;
|
||||
}
|
||||
|
||||
export const CONVERSATION_ELEMENT_ID = 'message-modal-conversation';
|
||||
|
||||
const Conversation = ({ activeMessageGroup, messages }: IProps) => {
|
||||
const Conversation = ({ activeMessageGroup, messages, onClickDisplay }: IProps) => {
|
||||
const classes = useStyles();
|
||||
const [imageModalOpen, setImageModalOpen] = useState(false);
|
||||
const query = useQueryParams();
|
||||
@@ -34,11 +35,13 @@ const Conversation = ({ activeMessageGroup, messages }: IProps) => {
|
||||
height="85%"
|
||||
display="flex"
|
||||
flexDirection="column"
|
||||
justifyContent="flex-end"
|
||||
pb={4}
|
||||
className={classes.overflowAutoY}
|
||||
>
|
||||
{messages.map((message) => (
|
||||
<MessageBubble
|
||||
onClickDisplay={onClickDisplay}
|
||||
key={message.id}
|
||||
message={message}
|
||||
isGroupChat={activeMessageGroup?.isGroupChat}
|
||||
@@ -46,6 +49,8 @@ const Conversation = ({ activeMessageGroup, messages }: IProps) => {
|
||||
))}
|
||||
</Box>
|
||||
<MessageInput
|
||||
/* I should do some groupDiplay here */
|
||||
messageGroupName={activeMessageGroup.label || activeMessageGroup.groupDisplay}
|
||||
messageGroupId={activeMessageGroup.groupId}
|
||||
onAddImageClick={() => setImageModalOpen(true)}
|
||||
/>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Box, makeStyles, Paper, Typography } from '@material-ui/core';
|
||||
import { Box, Link, makeStyles, Paper, Typography } from '@material-ui/core';
|
||||
import React from 'react';
|
||||
import { Message } from '../../../../common/typings/messages';
|
||||
|
||||
@@ -44,9 +44,11 @@ const isImage = (url) => {
|
||||
export const MessageBubble = ({
|
||||
message,
|
||||
isGroupChat,
|
||||
onClickDisplay,
|
||||
}: {
|
||||
message: Message;
|
||||
isGroupChat: boolean;
|
||||
onClickDisplay(phoneNumber: string): void;
|
||||
}) => {
|
||||
const classes = useStyles();
|
||||
return (
|
||||
@@ -60,9 +62,13 @@ export const MessageBubble = ({
|
||||
)}
|
||||
</Box>
|
||||
<Box>
|
||||
<Typography variant="subtitle1" color="secondary">
|
||||
{isGroupChat && !message.isMine ? message.display || message.phone_number : null}
|
||||
</Typography>
|
||||
{isGroupChat && !message.isMine ? (
|
||||
<Link onClick={() => onClickDisplay(message.phone_number)}>
|
||||
<Typography variant="subtitle1" color="secondary">
|
||||
{message.display || message.phone_number}
|
||||
</Typography>
|
||||
</Link>
|
||||
) : null}
|
||||
</Box>
|
||||
</Paper>
|
||||
</div>
|
||||
|
||||
@@ -1,76 +1,92 @@
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import { Slide, Paper, Typography, Button } from '@material-ui/core';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import {
|
||||
Slide,
|
||||
Paper,
|
||||
Typography,
|
||||
Button,
|
||||
Box,
|
||||
List,
|
||||
ListItem,
|
||||
ListItemText,
|
||||
ListItemSecondaryAction,
|
||||
} from '@material-ui/core';
|
||||
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
|
||||
|
||||
import MoreVertIcon from '@material-ui/icons/MoreVert';
|
||||
import PersonAddIcon from '@material-ui/icons/PersonAdd';
|
||||
import useStyles from './modal.styles';
|
||||
import useMessages from '../../hooks/useMessages';
|
||||
import Conversation, { CONVERSATION_ELEMENT_ID } from './Conversation';
|
||||
import MessageSkeletonList from './MessageSkeletonList';
|
||||
import Nui from '../../../../os/nui-events/utils/Nui';
|
||||
import { useQueryParams } from '../../../../common/hooks/useQueryParams';
|
||||
import { MessageGroup } from '../../../../common/typings/messages';
|
||||
import { useHistory, useParams } from 'react-router-dom';
|
||||
import { useHistory, useLocation, useParams } from 'react-router-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import Modal from '../../../../ui/components/Modal';
|
||||
import { useContacts } from '../../../contacts/hooks/useContacts';
|
||||
import { useSimcard } from '../../../../os/simcard/hooks/useSimcard';
|
||||
|
||||
const LARGE_HEADER_CHARS = 30;
|
||||
const MAX_HEADER_CHARS = 80;
|
||||
const MINIMUM_LOAD_TIME = 750;
|
||||
const MESSAGES_REFRESH_RATE = 5000;
|
||||
|
||||
const memberDisplay = (display, number, myNumber, t) => {
|
||||
if (myNumber === number) {
|
||||
return (
|
||||
<span>
|
||||
{t('APPS_MESSAGES_ME')} ({number})
|
||||
</span>
|
||||
);
|
||||
}
|
||||
if (display !== number) {
|
||||
return (
|
||||
<span>
|
||||
{display} ({number})
|
||||
</span>
|
||||
);
|
||||
}
|
||||
return <span>{number}</span>;
|
||||
};
|
||||
|
||||
export const MessageModal = () => {
|
||||
const classes = useStyles();
|
||||
const history = useHistory();
|
||||
const activeMessageGroup = useQueryParams<MessageGroup>();
|
||||
const { number: myNumber } = useSimcard();
|
||||
const { pathname } = useLocation();
|
||||
const { t } = useTranslation();
|
||||
const { groupId } = useParams<{ groupId: string }>();
|
||||
const { messages, setMessages } = useMessages();
|
||||
const { messages, setMessages, activeMessageGroup, setActiveMessageGroup } = useMessages();
|
||||
const { getContactByNumber, getDisplayByNumber } = useContacts();
|
||||
|
||||
const [minimumLoadPassed, setMimimumLoadPassed] = useState(false);
|
||||
|
||||
const minLoadTimeoutRef = useRef(null);
|
||||
const fetchInterval = useRef(null);
|
||||
const [groupActionsOpen, setGroupActionsOpen] = useState(false);
|
||||
|
||||
const closeModal = () => {
|
||||
history.push('/messages');
|
||||
setMessages(null);
|
||||
setMimimumLoadPassed(false);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!groupId) return;
|
||||
|
||||
minLoadTimeoutRef.current = setTimeout(() => {
|
||||
setMimimumLoadPassed(true);
|
||||
}, MINIMUM_LOAD_TIME);
|
||||
|
||||
fetchInterval.current = setInterval(() => {
|
||||
if (!groupId) return;
|
||||
Nui.send('phone:fetchMessages', {
|
||||
groupId,
|
||||
});
|
||||
}, MESSAGES_REFRESH_RATE);
|
||||
|
||||
return () => {
|
||||
clearInterval(fetchInterval.current);
|
||||
fetchInterval.current = null;
|
||||
clearTimeout(minLoadTimeoutRef.current);
|
||||
minLoadTimeoutRef.current = null;
|
||||
};
|
||||
}, [groupId]);
|
||||
setActiveMessageGroup(groupId);
|
||||
}, [groupId, setActiveMessageGroup]);
|
||||
|
||||
useEffect(() => {
|
||||
// when we get a new message group we should scroll to the
|
||||
// bottom to show the latest messages
|
||||
if (activeMessageGroup) {
|
||||
if (messages) {
|
||||
const element = document.getElementById(CONVERSATION_ELEMENT_ID);
|
||||
if (element) {
|
||||
element.scrollTop = element.scrollHeight;
|
||||
}
|
||||
}
|
||||
}, [activeMessageGroup, minimumLoadPassed]);
|
||||
}, [messages]);
|
||||
|
||||
// we add a minimum (but short) load time here so that
|
||||
// there isn't a quick flash of loading and immediately
|
||||
// another flash to the tweets screen.
|
||||
const hasLoaded = messages && minimumLoadPassed;
|
||||
// sends all unread messages
|
||||
useEffect(() => {
|
||||
if (activeMessageGroup?.groupId && activeMessageGroup.unreadCount > 0) {
|
||||
Nui.send('phone:setReadMessages', {
|
||||
groupId: activeMessageGroup.groupId,
|
||||
});
|
||||
}
|
||||
}, [activeMessageGroup]);
|
||||
|
||||
const hasLoaded = !!(messages && activeMessageGroup);
|
||||
|
||||
// don't allow too many characters, it takes too much room
|
||||
let header = activeMessageGroup ? activeMessageGroup.groupDisplay : '';
|
||||
@@ -80,23 +96,73 @@ export const MessageModal = () => {
|
||||
const headerClass =
|
||||
header.length > LARGE_HEADER_CHARS ? classes.largeGroupDisplay : classes.groupdisplay;
|
||||
|
||||
const handleAddContact = (number) => {
|
||||
const exists = getContactByNumber(number);
|
||||
if (exists) {
|
||||
return history.push(`/contacts/${exists.id}/?referal=${pathname}`);
|
||||
}
|
||||
return history.push(`/contacts/-1/?addNumber=${number}&referal=${pathname}`);
|
||||
};
|
||||
|
||||
const phoneNumbers = activeMessageGroup?.phoneNumbers || [];
|
||||
|
||||
return (
|
||||
<Slide direction="left" in={!!activeMessageGroup}>
|
||||
<Paper className={classes.modalRoot}>
|
||||
<Paper className={classes.topContainer}>
|
||||
<Button onClick={closeModal}>
|
||||
<ArrowBackIcon fontSize="large" />
|
||||
</Button>
|
||||
<Typography variant="h5" className={headerClass}>
|
||||
{header}
|
||||
</Typography>
|
||||
<div>
|
||||
<Modal visible={groupActionsOpen} handleClose={() => setGroupActionsOpen(false)}>
|
||||
<Typography variant="h6">{t('APPS_MESSAGES_MEMBERS_TITLE')}</Typography>
|
||||
<List>
|
||||
{phoneNumbers.map((number) => {
|
||||
const display = getDisplayByNumber(number);
|
||||
return (
|
||||
<ListItem divider>
|
||||
<ListItemText>{memberDisplay(display, number, myNumber, t)}</ListItemText>
|
||||
{!getContactByNumber(number) && myNumber !== number && (
|
||||
<ListItemSecondaryAction>
|
||||
<Button onClick={() => handleAddContact(number)}>
|
||||
<PersonAddIcon />
|
||||
</Button>
|
||||
</ListItemSecondaryAction>
|
||||
)}
|
||||
</ListItem>
|
||||
);
|
||||
})}
|
||||
</List>
|
||||
</Modal>
|
||||
<Slide direction="left" in={!!activeMessageGroup}>
|
||||
<Paper className={classes.modalRoot}>
|
||||
<Paper>
|
||||
<Box display="flex" justifyContent="space-between">
|
||||
<Button onClick={closeModal}>
|
||||
<ArrowBackIcon fontSize="large" />
|
||||
</Button>
|
||||
<Typography variant="h5" className={headerClass}>
|
||||
{header}
|
||||
</Typography>
|
||||
{activeMessageGroup?.isGroupChat ? (
|
||||
<Button>
|
||||
<MoreVertIcon onClick={() => setGroupActionsOpen(true)} fontSize="large" />
|
||||
</Button>
|
||||
) : (
|
||||
<Button>
|
||||
<PersonAddIcon
|
||||
onClick={() => handleAddContact(phoneNumbers.find((n) => myNumber !== n))}
|
||||
fontSize="large"
|
||||
/>
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
</Paper>
|
||||
{hasLoaded ? (
|
||||
<Conversation
|
||||
onClickDisplay={handleAddContact}
|
||||
messages={messages}
|
||||
activeMessageGroup={activeMessageGroup}
|
||||
/>
|
||||
) : (
|
||||
<MessageSkeletonList />
|
||||
)}
|
||||
</Paper>
|
||||
{hasLoaded ? (
|
||||
<Conversation messages={messages} activeMessageGroup={activeMessageGroup} />
|
||||
) : (
|
||||
<MessageSkeletonList />
|
||||
)}
|
||||
</Paper>
|
||||
</Slide>
|
||||
</Slide>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -6,15 +6,11 @@ const useStyles = makeStyles({
|
||||
position: 'absolute',
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
overflowY: 'hidden',
|
||||
},
|
||||
modalHide: {
|
||||
display: 'none',
|
||||
},
|
||||
topContainer: {
|
||||
display: 'flex',
|
||||
flexFlow: 'row nowrap',
|
||||
padding: '3px 0px',
|
||||
},
|
||||
overflowAutoY: {
|
||||
overflowY: 'auto',
|
||||
},
|
||||
|
||||
@@ -25,4 +25,8 @@ export const messageState = {
|
||||
key: 'useImageModal',
|
||||
default: false,
|
||||
}),
|
||||
unreadMessagesCount: atom({
|
||||
key: 'unreadMessagesCount',
|
||||
default: 0,
|
||||
})
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { useApp } from '../../../os/apps/hooks/useApps';
|
||||
import { useNotifications } from '../../../os/notifications/hooks/useNotifications';
|
||||
import { goToConversation } from '../utils/goToConversation';
|
||||
import useMessages from './useMessages';
|
||||
|
||||
const NOTIFICATION_ID = 'messages:broadcast';
|
||||
|
||||
export const useMessageNotifications = () => {
|
||||
const { t } = useTranslation();
|
||||
const history = useHistory();
|
||||
const {
|
||||
removeId,
|
||||
addNotification,
|
||||
addNotificationAlert,
|
||||
} = useNotifications();
|
||||
const { icon, notificationIcon } = useApp('MESSAGES');
|
||||
const { getMessageGroupById } = useMessages();
|
||||
|
||||
const setNotification = ({ groupId, message }) => {
|
||||
const group = getMessageGroupById(groupId);
|
||||
if (!group) return;
|
||||
|
||||
const id = `${NOTIFICATION_ID}:${groupId}`;
|
||||
|
||||
const notification = {
|
||||
app: 'MESSAGES',
|
||||
id,
|
||||
sound: true,
|
||||
title: group.label || group.groupDisplay,
|
||||
onClick: () => goToConversation(group, history),
|
||||
content: message,
|
||||
icon,
|
||||
notificationIcon,
|
||||
};
|
||||
|
||||
addNotificationAlert(notification, (n) => {
|
||||
removeId(id);
|
||||
if (group.unreadCount > 1) {
|
||||
addNotification({
|
||||
...n,
|
||||
title: group.label || group.groupDisplay,
|
||||
content: t('APPS_MESSAGES_UNREAD_MESSAGES', {
|
||||
count: group.unreadCount,
|
||||
}),
|
||||
});
|
||||
return;
|
||||
}
|
||||
addNotification(n);
|
||||
});
|
||||
};
|
||||
|
||||
return { setNotification };
|
||||
};
|
||||
@@ -2,16 +2,19 @@ import { useSetRecoilState } from 'recoil';
|
||||
import { messageState } from './state';
|
||||
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
|
||||
import { useSnackbar } from '../../../ui/hooks/useSnackbar';
|
||||
import { useMessageNotifications } from './useMessageNotifications';
|
||||
|
||||
export const useMessagesService = () => {
|
||||
const setMessageGroups = useSetRecoilState(messageState.messageGroups);
|
||||
const setMessages = useSetRecoilState(messageState.messages);
|
||||
const setCreateMessageGroupResult = useSetRecoilState(messageState.createMessageGroupResult);
|
||||
const { addAlert } = useSnackbar();
|
||||
const { setNotification } = useMessageNotifications();
|
||||
|
||||
useNuiEvent('MESSAGES', 'phone:fetchMessageGroupsSuccess', setMessageGroups);
|
||||
useNuiEvent('MESSAGES', 'phone:fetchMessagesSuccess', setMessages);
|
||||
useNuiEvent('MESSAGES', 'phone:createMessageGroupSuccess', setCreateMessageGroupResult);
|
||||
useNuiEvent('MESSAGES', 'phone:createMessageGroupFailed', setCreateMessageGroupResult);
|
||||
useNuiEvent('MESSAGES', 'phone:setMessagesAlert', addAlert);
|
||||
useNuiEvent('MESSAGES', 'createMessagesBroadcast', setNotification);
|
||||
};
|
||||
@@ -1,3 +1,4 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useRecoilState, useRecoilValue } from 'recoil';
|
||||
import { CreateMessageGroupResult, Message, MessageGroup } from '../../../common/typings/messages';
|
||||
import { messageState } from './state';
|
||||
@@ -8,11 +9,24 @@ interface IUseMessages {
|
||||
messageGroups?: MessageGroup[] | null;
|
||||
createMessageGroupResult?: CreateMessageGroupResult | null;
|
||||
clearMessageGroupResult(): void;
|
||||
getMessageGroupById: (id: string) => MessageGroup | null;
|
||||
setActiveMessageGroup: (groupId: string) => MessageGroup | null;
|
||||
activeMessageGroup: MessageGroup | null;
|
||||
}
|
||||
|
||||
export default (): IUseMessages => {
|
||||
const [messages, setMessages] = useRecoilState<Message[] | null>(messageState.messages);
|
||||
const messageGroups = useRecoilValue<MessageGroup[] | null>(messageState.messageGroups);
|
||||
const [activeMessageGroup, _setActiveMessageGroup] = useRecoilState<MessageGroup | null>(
|
||||
messageState.activeMessageGroup,
|
||||
);
|
||||
|
||||
const getMessageGroupById = useCallback(
|
||||
(id: string): MessageGroup | null => {
|
||||
return messageGroups ? messageGroups.find((c) => c.groupId === id) : null;
|
||||
},
|
||||
[messageGroups],
|
||||
);
|
||||
|
||||
const [
|
||||
createMessageGroupResult,
|
||||
@@ -21,11 +35,23 @@ export default (): IUseMessages => {
|
||||
|
||||
const clearMessageGroupResult = () => setCreateMessageGroupResult(null);
|
||||
|
||||
const setActiveMessageGroup = useCallback(
|
||||
(groupId: string) => {
|
||||
const group = getMessageGroupById(groupId);
|
||||
_setActiveMessageGroup(group);
|
||||
return group;
|
||||
},
|
||||
[_setActiveMessageGroup, getMessageGroupById],
|
||||
);
|
||||
|
||||
return {
|
||||
messages,
|
||||
setMessages,
|
||||
messageGroups,
|
||||
createMessageGroupResult,
|
||||
clearMessageGroupResult,
|
||||
getMessageGroupById,
|
||||
activeMessageGroup,
|
||||
setActiveMessageGroup,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,2 @@
|
||||
export const goToConversation = (messageGroup, history) =>
|
||||
history.push(`/messages/conversations/${messageGroup.groupId}`);
|
||||
@@ -1,4 +1,3 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { useRecoilState } from 'recoil';
|
||||
@@ -6,43 +5,50 @@ import { useApp } from '../../../os/apps/hooks/useApps';
|
||||
import { useNotifications } from '../../../os/notifications/hooks/useNotifications';
|
||||
import { twitterState } from './state';
|
||||
|
||||
const NOTIFICATION_ID = 'twitter:broadcast';
|
||||
|
||||
export const useTwitterNotifications = () => {
|
||||
const { t } = useTranslation();
|
||||
const history = useHistory();
|
||||
|
||||
const { addNotificationAlert } = useNotifications();
|
||||
const {
|
||||
addNotificationAlert,
|
||||
addNotification,
|
||||
hasNotification,
|
||||
removeId,
|
||||
} = useNotifications();
|
||||
|
||||
const { icon, notificationIcon } = useApp('TWITTER');
|
||||
|
||||
const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount);
|
||||
|
||||
const setNotification = useCallback(
|
||||
({ profile_name, message }) => {
|
||||
setUnreadCount((curr) => {
|
||||
const unread = curr + 1;
|
||||
addNotificationAlert(
|
||||
{
|
||||
app: 'TWITTER',
|
||||
sound: true,
|
||||
title: t('APPS_TWITTER_NEW_BROADCAST', { profile_name }),
|
||||
onClick: () => history.push('/twitter'),
|
||||
content: message,
|
||||
icon,
|
||||
notificationIcon,
|
||||
},
|
||||
true,
|
||||
{
|
||||
title: t('APPS_TWITTER_UNREAD_MESSAGES', {
|
||||
count: unread,
|
||||
}),
|
||||
content: null,
|
||||
},
|
||||
);
|
||||
return unread;
|
||||
const setNotification = ({ profile_name, message }) => {
|
||||
const notification = {
|
||||
app: 'TWITTER',
|
||||
id: NOTIFICATION_ID,
|
||||
sound: true,
|
||||
title: t('APPS_TWITTER_NEW_BROADCAST', { profile_name }),
|
||||
onClick: () => history.push('/twitter'),
|
||||
content: message,
|
||||
icon,
|
||||
notificationIcon,
|
||||
};
|
||||
const newCount = unreadCount + 1;
|
||||
setUnreadCount(newCount);
|
||||
addNotificationAlert(notification);
|
||||
if (hasNotification(NOTIFICATION_ID)) {
|
||||
removeId(NOTIFICATION_ID);
|
||||
addNotification({
|
||||
...notification,
|
||||
title: t('APPS_TWITTER_UNREAD_MESSAGES', {
|
||||
count: newCount,
|
||||
}),
|
||||
content: null,
|
||||
});
|
||||
},
|
||||
[setUnreadCount, addNotificationAlert, t, icon, notificationIcon, history],
|
||||
);
|
||||
return;
|
||||
}
|
||||
addNotification(notification);
|
||||
};
|
||||
|
||||
return { setNotification, setUnreadCount, unreadCount };
|
||||
};
|
||||
Reference in new issue
Block a user