|
|
|
@ -3,7 +3,7 @@ import {
@@ -3,7 +3,7 @@ import {
|
|
|
|
|
ChatType, |
|
|
|
|
IChatDetails, |
|
|
|
|
useEventsListener, |
|
|
|
|
useSocketListener |
|
|
|
|
useSocketListener, |
|
|
|
|
} from "@/shared"; |
|
|
|
|
import { getProfile } from "@/store/account"; |
|
|
|
|
import { UnselectChat } from "@/store/chats"; |
|
|
|
@ -15,12 +15,13 @@ import { useHistory } from "react-router-dom";
@@ -15,12 +15,13 @@ import { useHistory } from "react-router-dom";
|
|
|
|
|
import { getHeaderGroupChatInfo, getHeaderPersonalChatInfo } from "../helpers"; |
|
|
|
|
import { IHeaderChatInfo } from "../interfaces"; |
|
|
|
|
import { transformChatDetailToHeaderChatInfo } from "../transforms"; |
|
|
|
|
import { message } from "antd"; |
|
|
|
|
|
|
|
|
|
const headerChatInfoInitialState: IHeaderChatInfo = { |
|
|
|
|
name: " ", |
|
|
|
|
previewUrl: null, |
|
|
|
|
type: ChatType.Group, |
|
|
|
|
label: " " |
|
|
|
|
label: " ", |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const useChatDetails = (chatId: number) => { |
|
|
|
@ -38,12 +39,18 @@ export const useChatDetails = (chatId: number) => {
@@ -38,12 +39,18 @@ export const useChatDetails = (chatId: number) => {
|
|
|
|
|
const fetchDetails = async () => { |
|
|
|
|
setLoading(true); |
|
|
|
|
|
|
|
|
|
const chatDetails = await chatsService.fetchChatDetails({ id: chatId }); |
|
|
|
|
|
|
|
|
|
setHeaderInfo(chatDetails); |
|
|
|
|
setChatDetails(chatDetails); |
|
|
|
|
|
|
|
|
|
setLoading(false); |
|
|
|
|
try { |
|
|
|
|
const chatDetails = await chatsService.fetchChatDetails({ id: chatId }); |
|
|
|
|
|
|
|
|
|
setHeaderInfo(chatDetails); |
|
|
|
|
setChatDetails(chatDetails); |
|
|
|
|
} catch (e) { |
|
|
|
|
message.error("Сталась помилка, такого чату не існує"); |
|
|
|
|
console.log("e", e); |
|
|
|
|
setChatDetails(null); |
|
|
|
|
} finally { |
|
|
|
|
setLoading(false); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const setHeaderInfo = (chatDetails: IChatDetails) => { |
|
|
|
@ -57,18 +64,18 @@ export const useChatDetails = (chatId: number) => {
@@ -57,18 +64,18 @@ export const useChatDetails = (chatId: number) => {
|
|
|
|
|
); |
|
|
|
|
const headerLabel = { |
|
|
|
|
[ChatType.Group]: getHeaderGroupChatInfo( |
|
|
|
|
_.filter(chatDetails.chatMembers, member => !member.isDeleted).length, |
|
|
|
|
_.filter(chatDetails.chatMembers, (member) => !member.isDeleted).length, |
|
|
|
|
onlineUsersCount |
|
|
|
|
), |
|
|
|
|
[ChatType.Personal]: getHeaderPersonalChatInfo( |
|
|
|
|
chatDetails.chatMembers, |
|
|
|
|
accountId.id |
|
|
|
|
) |
|
|
|
|
), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
setHeaderChatInfo({ |
|
|
|
|
...headerChatInfo, |
|
|
|
|
label: headerLabel[chatDetails.type] |
|
|
|
|
label: headerLabel[chatDetails.type], |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -134,7 +141,6 @@ export const useChatDetails = (chatId: number) => {
@@ -134,7 +141,6 @@ export const useChatDetails = (chatId: number) => {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const onChangeChatRole = (data: { chatId: number }) => { |
|
|
|
|
console.log("changed role"); |
|
|
|
|
updateChatData(data?.chatId); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -142,11 +148,11 @@ export const useChatDetails = (chatId: number) => {
@@ -142,11 +148,11 @@ export const useChatDetails = (chatId: number) => {
|
|
|
|
|
if (!chatDetails || _.isEmpty(chatDetails.pinedMessages)) return; |
|
|
|
|
const pinnedUpdated = _.find( |
|
|
|
|
chatDetails.pinedMessages, |
|
|
|
|
it => it.id === message.id |
|
|
|
|
(it) => it.id === message.id |
|
|
|
|
); |
|
|
|
|
if (!pinnedUpdated) return; |
|
|
|
|
|
|
|
|
|
const newPinned = chatDetails.pinedMessages.map(it => { |
|
|
|
|
const newPinned = chatDetails.pinedMessages.map((it) => { |
|
|
|
|
if (it.id === message.id) { |
|
|
|
|
return message; |
|
|
|
|
} |
|
|
|
@ -155,12 +161,12 @@ export const useChatDetails = (chatId: number) => {
@@ -155,12 +161,12 @@ export const useChatDetails = (chatId: number) => {
|
|
|
|
|
|
|
|
|
|
setChatDetails({ |
|
|
|
|
...chatDetails, |
|
|
|
|
pinedMessages: newPinned |
|
|
|
|
pinedMessages: newPinned, |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
useSocketListener("chat/update-message", onUpdateMessage, [ |
|
|
|
|
chatDetails?.pinedMessages |
|
|
|
|
chatDetails?.pinedMessages, |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
// APP EVENTS LISTENERS //
|
|
|
|
@ -181,6 +187,6 @@ export const useChatDetails = (chatId: number) => {
@@ -181,6 +187,6 @@ export const useChatDetails = (chatId: number) => {
|
|
|
|
|
return { |
|
|
|
|
chatDetails, |
|
|
|
|
headerChatInfo, |
|
|
|
|
isLoading |
|
|
|
|
isLoading, |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|