|
|
|
@ -2,13 +2,13 @@ import {
@@ -2,13 +2,13 @@ import {
|
|
|
|
|
ChatMemberRole, |
|
|
|
|
ChatMessageEventType, |
|
|
|
|
IChatMessage, |
|
|
|
|
MessageType |
|
|
|
|
MessageType, |
|
|
|
|
} from "@/shared"; |
|
|
|
|
import { |
|
|
|
|
useEventsListener, |
|
|
|
|
useIdsList, |
|
|
|
|
useSocket, |
|
|
|
|
useSocketListener |
|
|
|
|
useSocketListener, |
|
|
|
|
} from "@/shared/hooks/"; |
|
|
|
|
import _ from "lodash"; |
|
|
|
|
import { useCallback, useEffect, useState } from "react"; |
|
|
|
@ -17,9 +17,10 @@ import { IFetchChatMessages } from "@/api/chats-messages/request.interfaces";
@@ -17,9 +17,10 @@ import { IFetchChatMessages } from "@/api/chats-messages/request.interfaces";
|
|
|
|
|
import { useSelector } from "react-redux"; |
|
|
|
|
import { getProfile } from "@/store/account"; |
|
|
|
|
import { appEvents } from "@/shared/events"; |
|
|
|
|
import { ChatMessageActionEnum } from "../enums"; |
|
|
|
|
import { ChatMessageActionEnum, ChatViewModeEnum } from "../enums"; |
|
|
|
|
import { getChatMessageMenuOptions } from "../configs"; |
|
|
|
|
import { chatMessagesApi } from "@/api"; |
|
|
|
|
import { useChatViewModeState } from "./use-chat-view-mode-state.hook"; |
|
|
|
|
|
|
|
|
|
export const useChatMessages = ( |
|
|
|
|
chatId: number, |
|
|
|
@ -32,6 +33,8 @@ export const useChatMessages = (
@@ -32,6 +33,8 @@ export const useChatMessages = (
|
|
|
|
|
const [replyTo, setReplyTo] = useState<IChatMessage>(null); |
|
|
|
|
const [scrollToId, setScrollToId] = useState(null); |
|
|
|
|
|
|
|
|
|
const { setMode: setChatViewMode } = useChatViewModeState(); |
|
|
|
|
|
|
|
|
|
const { |
|
|
|
|
items: messages, |
|
|
|
|
loadNew, |
|
|
|
@ -44,14 +47,14 @@ export const useChatMessages = (
@@ -44,14 +47,14 @@ export const useChatMessages = (
|
|
|
|
|
resetList, |
|
|
|
|
_setItems, |
|
|
|
|
loadParams, |
|
|
|
|
setLoadParams |
|
|
|
|
setLoadParams, |
|
|
|
|
} = useIdsList<IChatMessage, IFetchChatMessages>({ |
|
|
|
|
limit: 20, |
|
|
|
|
req: async params => await chatMessagesService.fetchMessages(params), |
|
|
|
|
req: async (params) => await chatMessagesService.fetchMessages(params), |
|
|
|
|
needInit: false, |
|
|
|
|
clearWhenReload: false, |
|
|
|
|
lastMessageId, |
|
|
|
|
firstMessageId |
|
|
|
|
firstMessageId, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// const afterAction = () => {
|
|
|
|
@ -76,7 +79,7 @@ export const useChatMessages = (
@@ -76,7 +79,7 @@ export const useChatMessages = (
|
|
|
|
|
appEvents.emit("openConfirmDeleteMessageModal", { |
|
|
|
|
message, |
|
|
|
|
deleteForAll, |
|
|
|
|
isShow: true |
|
|
|
|
isShow: true, |
|
|
|
|
}), |
|
|
|
|
300 |
|
|
|
|
); |
|
|
|
@ -93,11 +96,11 @@ export const useChatMessages = (
@@ -93,11 +96,11 @@ export const useChatMessages = (
|
|
|
|
|
const isAuthor = message.userId === account.id; |
|
|
|
|
if (_.isEmpty(message.events) && isAuthor) return true; |
|
|
|
|
|
|
|
|
|
const viewed = _.some(message.events, event => { |
|
|
|
|
const viewed = _.some(message.events, (event) => { |
|
|
|
|
const keys = Object.keys(event); |
|
|
|
|
return _.some( |
|
|
|
|
keys, |
|
|
|
|
key => Number(key) !== account.id && event[key] === "view" |
|
|
|
|
(key) => Number(key) !== account.id && event[key] === "view" |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
@ -126,6 +129,8 @@ export const useChatMessages = (
@@ -126,6 +129,8 @@ export const useChatMessages = (
|
|
|
|
|
appEvents.emit("onPressEditmessage", { message }); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const onSelect = () => setChatViewMode(ChatViewModeEnum.SELECT); |
|
|
|
|
|
|
|
|
|
const actions = { |
|
|
|
|
[ChatMessageActionEnum.FORWARD]: onForward, |
|
|
|
|
[ChatMessageActionEnum.DELETE]: onDelete, |
|
|
|
@ -134,7 +139,8 @@ export const useChatMessages = (
@@ -134,7 +139,8 @@ export const useChatMessages = (
|
|
|
|
|
[ChatMessageActionEnum.COPY]: onCopy, |
|
|
|
|
[ChatMessageActionEnum.PIN]: onPin, |
|
|
|
|
[ChatMessageActionEnum.UNPIN]: onUnpin, |
|
|
|
|
[ChatMessageActionEnum.EDIT]: onEdit |
|
|
|
|
[ChatMessageActionEnum.EDIT]: onEdit, |
|
|
|
|
[ChatMessageActionEnum.SELECT]: onSelect, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const onMessageActions = (message: IChatMessage, role: ChatMemberRole) => { |
|
|
|
@ -153,7 +159,7 @@ export const useChatMessages = (
@@ -153,7 +159,7 @@ export const useChatMessages = (
|
|
|
|
|
canCopy, |
|
|
|
|
onClick: (actionType: ChatMessageActionEnum) => |
|
|
|
|
actions[actionType](message), |
|
|
|
|
canEdit |
|
|
|
|
canEdit, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
appEvents.emit("openMessageMenuOptions", { items: options }); |
|
|
|
@ -179,24 +185,24 @@ export const useChatMessages = (
@@ -179,24 +185,24 @@ export const useChatMessages = (
|
|
|
|
|
}, [chatId]); |
|
|
|
|
|
|
|
|
|
const messageContainsItem = (itemId: number) => |
|
|
|
|
_.find(messages, message => message.id === itemId); |
|
|
|
|
_.find(messages, (message) => message.id === itemId); |
|
|
|
|
|
|
|
|
|
const updateEvents = ( |
|
|
|
|
messages: IChatMessage[], |
|
|
|
|
userId: number, |
|
|
|
|
event: ChatMessageEventType |
|
|
|
|
) => { |
|
|
|
|
const updatedItems = messages.map(item => { |
|
|
|
|
const updatedItems = messages.map((item) => { |
|
|
|
|
const userEvent = _.find( |
|
|
|
|
item.events, |
|
|
|
|
it => |
|
|
|
|
(it) => |
|
|
|
|
_.includes(Object.keys(it), userId.toString()) && it[userId] === event |
|
|
|
|
); |
|
|
|
|
if (userEvent) return item; |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
...item, |
|
|
|
|
events: [...item.events, { userId: event }] |
|
|
|
|
events: [...item.events, { userId: event }], |
|
|
|
|
}; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
@ -213,7 +219,7 @@ export const useChatMessages = (
@@ -213,7 +219,7 @@ export const useChatMessages = (
|
|
|
|
|
socket.emit("chat/read-message", { |
|
|
|
|
userId: account.id, |
|
|
|
|
messagesIds: [message.id], |
|
|
|
|
chatId |
|
|
|
|
chatId, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (message.userId === account.id) setScrollToId(message.id); |
|
|
|
@ -226,18 +232,18 @@ export const useChatMessages = (
@@ -226,18 +232,18 @@ export const useChatMessages = (
|
|
|
|
|
{ |
|
|
|
|
const messagesToAdd = _.filter( |
|
|
|
|
message, |
|
|
|
|
it => !messageContainsItem(it.id) |
|
|
|
|
(it) => !messageContainsItem(it.id) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(messagesToAdd)) { |
|
|
|
|
_setItems([...messagesToAdd, ...messages]); |
|
|
|
|
|
|
|
|
|
const messagesIds = messagesToAdd.map(it => it.id); |
|
|
|
|
const messagesIds = messagesToAdd.map((it) => it.id); |
|
|
|
|
|
|
|
|
|
socket.emit("chat/read-message", { |
|
|
|
|
userId: account.id, |
|
|
|
|
messagesIds, |
|
|
|
|
chatId |
|
|
|
|
chatId, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -263,15 +269,15 @@ export const useChatMessages = (
@@ -263,15 +269,15 @@ export const useChatMessages = (
|
|
|
|
|
const onPinedMessage = (data: { chatId: number; messageId: number }) => { |
|
|
|
|
if ( |
|
|
|
|
data?.chatId !== chatId || |
|
|
|
|
!_.find(messages, message => message.id === data.messageId) |
|
|
|
|
!_.find(messages, (message) => message.id === data.messageId) |
|
|
|
|
) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
const newItems = messages.map(item => { |
|
|
|
|
const newItems = messages.map((item) => { |
|
|
|
|
if (item.id === data.messageId) |
|
|
|
|
return { |
|
|
|
|
...item, |
|
|
|
|
isPined: !item.isPined |
|
|
|
|
isPined: !item.isPined, |
|
|
|
|
}; |
|
|
|
|
return item; |
|
|
|
|
}); |
|
|
|
@ -282,7 +288,7 @@ export const useChatMessages = (
@@ -282,7 +288,7 @@ export const useChatMessages = (
|
|
|
|
|
const onMessageDeleted = (data: { messageId: number; chatId: number }) => { |
|
|
|
|
if (data?.chatId !== chatId) return; |
|
|
|
|
|
|
|
|
|
const updatedMessages = messages.map(item => { |
|
|
|
|
const updatedMessages = messages.map((item) => { |
|
|
|
|
if ( |
|
|
|
|
item.content?.replyToMessage && |
|
|
|
|
item.content?.replyToMessage?.id === data.messageId |
|
|
|
@ -293,16 +299,16 @@ export const useChatMessages = (
@@ -293,16 +299,16 @@ export const useChatMessages = (
|
|
|
|
|
...item.content, |
|
|
|
|
replyToMessage: { |
|
|
|
|
...item.content.replyToMessage, |
|
|
|
|
isDeleted: true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
isDeleted: true, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
return item; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const filteredItems = _.filter( |
|
|
|
|
updatedMessages, |
|
|
|
|
item => item.id !== data?.messageId |
|
|
|
|
(item) => item.id !== data?.messageId |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
_setItems(filteredItems); |
|
|
|
@ -315,7 +321,7 @@ export const useChatMessages = (
@@ -315,7 +321,7 @@ export const useChatMessages = (
|
|
|
|
|
|
|
|
|
|
const onUpdateMessage = ({ message }) => { |
|
|
|
|
console.log("on update message"); |
|
|
|
|
const newItems = messages.map(it => { |
|
|
|
|
const newItems = messages.map((it) => { |
|
|
|
|
if (it.id === message.id) { |
|
|
|
|
return message; |
|
|
|
|
} |
|
|
|
@ -357,7 +363,7 @@ export const useChatMessages = (
@@ -357,7 +363,7 @@ export const useChatMessages = (
|
|
|
|
|
|
|
|
|
|
useSocketListener( |
|
|
|
|
"chat/new-message", |
|
|
|
|
data => { |
|
|
|
|
(data) => { |
|
|
|
|
onNewMessage(data); |
|
|
|
|
}, |
|
|
|
|
[chatId, messages] |
|
|
|
@ -365,7 +371,7 @@ export const useChatMessages = (
@@ -365,7 +371,7 @@ export const useChatMessages = (
|
|
|
|
|
|
|
|
|
|
useSocketListener("chat/delete-message", onMessageDeleted, [ |
|
|
|
|
chatId, |
|
|
|
|
messages |
|
|
|
|
messages, |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
useSocketListener("chat/pined-message", onPinedMessage, [chatId, messages]); |
|
|
|
@ -384,7 +390,7 @@ export const useChatMessages = (
@@ -384,7 +390,7 @@ export const useChatMessages = (
|
|
|
|
|
isLoadingNew, |
|
|
|
|
isLoadingOld, |
|
|
|
|
lastMessageLoaded, |
|
|
|
|
firstMessageLoaded |
|
|
|
|
firstMessageLoaded, |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
onMessageActions, |
|
|
|
@ -394,6 +400,6 @@ export const useChatMessages = (
@@ -394,6 +400,6 @@ export const useChatMessages = (
|
|
|
|
|
onForward, |
|
|
|
|
onPressMessagePreview, |
|
|
|
|
replyTo, |
|
|
|
|
setReplyToMessage: setReplyTo |
|
|
|
|
setReplyToMessage: setReplyTo, |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|