mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
Make use of library instead of having code duplicated (#201)
* Use fivem-nui-react-lib
This commit is contained in:
1 parent
0cf5bc2bc7
commit
8e79eb1345
60 files changed
+231
-347
No files matched your search
@@ -9,17 +9,18 @@ import { Route, Switch, useHistory } from 'react-router-dom';
|
||||
import { MessageModal } from './modal/MessageModal';
|
||||
import InjectDebugData from '../../../os/debug/InjectDebugData';
|
||||
import NewMessageGroupButton from './form/NewMessageGroupButton';
|
||||
import Nui from '../../../os/nui-events/utils/Nui';
|
||||
import { useNuiRequest } from 'fivem-nui-react-lib';
|
||||
import { MessagesThemeProvider } from '../providers/MessagesThemeProvider';
|
||||
import { MessageEvents } from '../../../../../typings/messages';
|
||||
|
||||
export const MessagesApp = () => {
|
||||
const Nui = useNuiRequest();
|
||||
const messages = useApp('MESSAGES');
|
||||
const history = useHistory();
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
Nui.send(MessageEvents.FETCH_MESSAGE_GROUPS);
|
||||
}, []);
|
||||
}, [Nui]);
|
||||
|
||||
return (
|
||||
<MessagesThemeProvider>
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next';
|
||||
import { Paper, Box, makeStyles, TextField, Button } from '@material-ui/core';
|
||||
import SendIcon from '@material-ui/icons/Send';
|
||||
import ImageIcon from '@material-ui/icons/Image';
|
||||
import Nui from '../../../../os/nui-events/utils/Nui';
|
||||
import { useNuiRequest } from 'fivem-nui-react-lib';
|
||||
import { MessageEvents } from '../../../../../../typings/messages';
|
||||
|
||||
interface IProps {
|
||||
@@ -17,6 +17,7 @@ const useStyles = makeStyles({
|
||||
});
|
||||
|
||||
const MessageInput = ({ messageGroupId, onAddImageClick }: IProps) => {
|
||||
const Nui = useNuiRequest();
|
||||
const { t } = useTranslation();
|
||||
const classes = useStyles();
|
||||
const [message, setMessage] = useState('');
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Box, Button, TextField } from '@material-ui/core';
|
||||
import Nui from '../../../../os/nui-events/utils/Nui';
|
||||
import { useNuiRequest } from 'fivem-nui-react-lib';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { Autocomplete } from '@material-ui/lab';
|
||||
import { useContacts } from '../../../contacts/hooks/useContacts';
|
||||
@@ -11,6 +11,7 @@ import { PHONE_NUMBER_REGEX } from '../../../../../../typings/phone';
|
||||
import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
|
||||
|
||||
const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
||||
const Nui = useNuiRequest();
|
||||
const history = useHistory();
|
||||
const { t } = useTranslation();
|
||||
const { addAlert } = useSnackbar();
|
||||
@@ -42,7 +43,7 @@ const NewMessageGroupForm = ({ phoneNumber }: { phoneNumber?: string }) => {
|
||||
setParticipants([]);
|
||||
history.push('/messages');
|
||||
}
|
||||
}, [history, label, participants, isGroupChat]);
|
||||
}, [history, label, participants, isGroupChat, Nui]);
|
||||
|
||||
const onAutocompleteChange = (_e, value: any) => {
|
||||
const lastIdx = value.length - 1;
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Box, List } from '@material-ui/core';
|
||||
import { MessageEvents, MessageGroup } from '../../../../../../typings/messages';
|
||||
import Nui from '../../../../os/nui-events/utils/Nui';
|
||||
import { MessageGroup } from '../../../../../../typings/messages';
|
||||
import useMessages from '../../hooks/useMessages';
|
||||
import MessageGroupItem from './MessageGroupItem';
|
||||
import useStyles from './list.styles';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { goToConversation } from '../../utils/goToConversation';
|
||||
import { SearchField } from '../../../../ui/components/SearchField';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
@@ -17,6 +15,7 @@ const MessagesList = (): any => {
|
||||
|
||||
const {
|
||||
messageGroups,
|
||||
goToConversation,
|
||||
createMessageGroupResult,
|
||||
clearMessageGroupResult,
|
||||
getMessageGroupById,
|
||||
@@ -41,7 +40,7 @@ const MessagesList = (): any => {
|
||||
const findGroup = getMessageGroupById(createMessageGroupResult.groupId);
|
||||
clearMessageGroupResult();
|
||||
if (findGroup) {
|
||||
goToConversation(findGroup, history);
|
||||
goToConversation(findGroup);
|
||||
}
|
||||
}
|
||||
}, [
|
||||
@@ -51,12 +50,13 @@ const MessagesList = (): any => {
|
||||
history,
|
||||
setActiveMessageGroup,
|
||||
getMessageGroupById,
|
||||
goToConversation,
|
||||
]);
|
||||
|
||||
if (!messageGroups) return null;
|
||||
|
||||
const handleClick = (messageGroup: MessageGroup) => () => {
|
||||
goToConversation(messageGroup, history);
|
||||
goToConversation(messageGroup);
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import qs from 'qs';
|
||||
import Nui from '../../../../os/nui-events/utils/Nui';
|
||||
import { useNuiRequest } from 'fivem-nui-react-lib';
|
||||
import Modal from '../../../../ui/components/Modal';
|
||||
import PhotoLibraryIcon from '@material-ui/icons/PhotoLibrary';
|
||||
import { Box, Typography, Button } from '@material-ui/core';
|
||||
@@ -18,6 +18,7 @@ interface IProps {
|
||||
}
|
||||
|
||||
export const MessageImageModal = ({ isOpen, messageGroupId, onClose, image }: IProps) => {
|
||||
const Nui = useNuiRequest();
|
||||
const history = useHistory();
|
||||
const { pathname, search } = useLocation();
|
||||
const [queryParamImagePreview, setQueryParamImagePreview] = useState(null);
|
||||
@@ -35,7 +36,7 @@ export const MessageImageModal = ({ isOpen, messageGroupId, onClose, image }: IP
|
||||
});
|
||||
onClose();
|
||||
},
|
||||
[messageGroupId, onClose],
|
||||
[messageGroupId, onClose, Nui],
|
||||
);
|
||||
|
||||
const sendFromQueryParam = useCallback(
|
||||
|
||||
@@ -17,7 +17,7 @@ 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 { useNuiRequest } from 'fivem-nui-react-lib';
|
||||
import { useHistory, useLocation, useParams } from 'react-router-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import Modal from '../../../../ui/components/Modal';
|
||||
@@ -48,6 +48,7 @@ const memberDisplay = (display, number, myNumber, t) => {
|
||||
};
|
||||
|
||||
export const MessageModal = () => {
|
||||
const Nui = useNuiRequest();
|
||||
const classes = useStyles();
|
||||
const history = useHistory();
|
||||
const { number: myNumber } = useSimcard();
|
||||
@@ -97,7 +98,7 @@ export const MessageModal = () => {
|
||||
groupId: activeMessageGroup.groupId,
|
||||
});
|
||||
}
|
||||
}, [activeMessageGroup]);
|
||||
}, [activeMessageGroup, Nui]);
|
||||
|
||||
// don't allow too many characters, it takes too much room
|
||||
let header = activeMessageGroup ? activeMessageGroup.groupDisplay : '';
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { matchPath, 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';
|
||||
@@ -13,7 +11,7 @@ export const useMessageNotifications = () => {
|
||||
const history = useHistory();
|
||||
const { removeId, addNotification, addNotificationAlert } = useNotifications();
|
||||
const { icon, notificationIcon } = useApp('MESSAGES');
|
||||
const { getMessageGroupById, activeMessageGroup } = useMessages();
|
||||
const { getMessageGroupById, activeMessageGroup, goToConversation } = useMessages();
|
||||
|
||||
// Remove notifications from groups when opening them
|
||||
history.listen((location) => {
|
||||
@@ -39,7 +37,7 @@ export const useMessageNotifications = () => {
|
||||
id,
|
||||
sound: true,
|
||||
title: group.label || group.groupDisplay,
|
||||
onClick: () => goToConversation(group, history),
|
||||
onClick: () => goToConversation(group),
|
||||
content: message,
|
||||
icon,
|
||||
notificationIcon,
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { useRecoilState, useSetRecoilState } from 'recoil';
|
||||
import { messageState } from './state';
|
||||
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
|
||||
import { useNuiEvent, useNuiRequest } from 'fivem-nui-react-lib';
|
||||
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';
|
||||
import { useLocation } from 'react-router';
|
||||
import { MessageEvents } from '../../../../../typings/messages';
|
||||
import { TOptionsBase } from 'i18next';
|
||||
|
||||
export const useMessagesService = () => {
|
||||
const Nui = useNuiRequest();
|
||||
const { pathname } = useLocation();
|
||||
const [activeMessageGroup, setActiveMessageGroup] = useRecoilState(
|
||||
messageState.activeMessageGroup,
|
||||
@@ -43,7 +43,7 @@ export const useMessagesService = () => {
|
||||
}
|
||||
setNotification({ groupId, message });
|
||||
},
|
||||
[activeMessageGroup, pathname, setNotification],
|
||||
[activeMessageGroup, pathname, setNotification, Nui],
|
||||
);
|
||||
|
||||
const _setMessageGroups = useCallback(
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useRecoilState, useRecoilValue } from 'recoil';
|
||||
import { CreateMessageGroupResult, Message, MessageGroup } from '../../../../../typings/messages';
|
||||
import {
|
||||
CreateMessageGroupResult,
|
||||
Message,
|
||||
MessageEvents,
|
||||
MessageGroup,
|
||||
} from '../../../../../typings/messages';
|
||||
import { messageState } from './state';
|
||||
import { useNuiRequest } from 'fivem-nui-react-lib';
|
||||
import { useHistory } from 'react-router';
|
||||
|
||||
interface IUseMessages {
|
||||
messages?: Message[] | null;
|
||||
@@ -12,9 +19,13 @@ interface IUseMessages {
|
||||
getMessageGroupById: (id: string) => MessageGroup | null;
|
||||
setActiveMessageGroup: (groupId: string) => MessageGroup | null;
|
||||
activeMessageGroup: MessageGroup | null;
|
||||
goToConversation(g: Pick<MessageGroup, 'groupId'>): void;
|
||||
}
|
||||
|
||||
export default (): IUseMessages => {
|
||||
const useMessages = (): IUseMessages => {
|
||||
const Nui = useNuiRequest();
|
||||
const history = useHistory();
|
||||
|
||||
const [messages, setMessages] = useRecoilState<Message[] | null>(messageState.messages);
|
||||
const messageGroups = useRecoilValue<MessageGroup[] | null>(messageState.messageGroups);
|
||||
const [activeMessageGroup, _setActiveMessageGroup] = useRecoilState<MessageGroup | null>(
|
||||
@@ -44,6 +55,12 @@ export default (): IUseMessages => {
|
||||
[_setActiveMessageGroup, getMessageGroupById],
|
||||
);
|
||||
|
||||
const goToConversation = (messageGroup) => {
|
||||
if (!messageGroup?.groupId || !history) return;
|
||||
history.push(`/messages/conversations/${messageGroup.groupId}`);
|
||||
Nui.send(MessageEvents.FETCH_MESSAGES, { groupId: messageGroup.groupId });
|
||||
};
|
||||
|
||||
return {
|
||||
messages,
|
||||
setMessages,
|
||||
@@ -53,5 +70,8 @@ export default (): IUseMessages => {
|
||||
getMessageGroupById,
|
||||
activeMessageGroup,
|
||||
setActiveMessageGroup,
|
||||
goToConversation,
|
||||
};
|
||||
};
|
||||
|
||||
export default useMessages;
|
||||
@@ -1,8 +0,0 @@
|
||||
import { MessageEvents } from '../../../../../typings/messages';
|
||||
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(MessageEvents.FETCH_MESSAGES, { groupId: messageGroup.groupId });
|
||||
};
|
||||
Reference in new issue
Block a user