|
|
|
@ -6,7 +6,7 @@ import {
@@ -6,7 +6,7 @@ import {
|
|
|
|
|
useEventsListener, |
|
|
|
|
usePaginationList, |
|
|
|
|
useSocket, |
|
|
|
|
useSocketListener, |
|
|
|
|
useSocketListener |
|
|
|
|
} from "@/shared"; |
|
|
|
|
import { appEvents, SocketEvents } from "@/shared/events"; |
|
|
|
|
import { getProfile } from "@/store/account"; |
|
|
|
@ -29,9 +29,9 @@ export const useChatList = () => {
@@ -29,9 +29,9 @@ export const useChatList = () => {
|
|
|
|
|
isLoading, |
|
|
|
|
loadPage, |
|
|
|
|
_setItems, |
|
|
|
|
setOrderBy, |
|
|
|
|
setOrderBy |
|
|
|
|
} = usePaginationList<IChat>({ |
|
|
|
|
fetchItems: (params) => chatsService.fetchChats(params), |
|
|
|
|
fetchItems: params => chatsService.fetchChats(params) |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
@ -40,7 +40,7 @@ export const useChatList = () => {
@@ -40,7 +40,7 @@ export const useChatList = () => {
|
|
|
|
|
}, [searchString]); |
|
|
|
|
|
|
|
|
|
const presentInList = (chatId: number) => { |
|
|
|
|
const chatIds = chats.map((it) => it.id); |
|
|
|
|
const chatIds = chats.map(it => it.id); |
|
|
|
|
return _.includes(chatIds, chatId); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -49,7 +49,15 @@ export const useChatList = () => {
@@ -49,7 +49,15 @@ export const useChatList = () => {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const onDeleteChat = (data: { chatId: number }) => { |
|
|
|
|
reloadIfIncludes(data?.chatId); |
|
|
|
|
if (presentInList(Number(data?.chatId))) { |
|
|
|
|
// *** при видаленні чату для себе або для всіх потрібно відразу забрати його зі списку
|
|
|
|
|
// бо перезавантаження чатів з серверу дає затримку і в цей час користувач може встигнути
|
|
|
|
|
// натиснути на цей видалений чат і відкрити його деталку *** //
|
|
|
|
|
_setItems(chats.filter(it => it.id !== data.chatId)); |
|
|
|
|
// *** перезавантаження списку з сервера все одно робимо, щоб отримати актуальний список
|
|
|
|
|
// чатів та правильний порядок закріплених чатів
|
|
|
|
|
resetFlatList(); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const onEditChat = (data: { chatId: number }) => { |
|
|
|
@ -57,21 +65,21 @@ export const useChatList = () => {
@@ -57,21 +65,21 @@ export const useChatList = () => {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const onSetUnread = async (chatId: number, data?: Partial<IChat>) => { |
|
|
|
|
const chat = _.find(chats, (chat) => chat.id === chatId); |
|
|
|
|
const chat = _.find(chats, chat => chat.id === chatId); |
|
|
|
|
|
|
|
|
|
if (!chat.isChatUnread) return setUnread(chatId); |
|
|
|
|
|
|
|
|
|
if (data?.unreadMessagesCount > 0) |
|
|
|
|
socket.emit("chat/read-chat", { |
|
|
|
|
userId: accountId.id, |
|
|
|
|
chatsIds: [chatId], |
|
|
|
|
chatsIds: [chatId] |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (chat.isChatUnread) await chatsApi.setChatReadReq(chatId); |
|
|
|
|
|
|
|
|
|
appEvents.emit("onReadChat", { |
|
|
|
|
chatId, |
|
|
|
|
unreadCount: data.unreadMessagesCount, |
|
|
|
|
unreadCount: data.unreadMessagesCount |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -80,7 +88,7 @@ export const useChatList = () => {
@@ -80,7 +88,7 @@ export const useChatList = () => {
|
|
|
|
|
await chatsApi.setChatUnreadReq(chatId); |
|
|
|
|
|
|
|
|
|
_setItems( |
|
|
|
|
_.clone(chats).map((it) => { |
|
|
|
|
_.clone(chats).map(it => { |
|
|
|
|
if (it.id === chatId) it.isChatUnread = true; |
|
|
|
|
return it; |
|
|
|
|
}) |
|
|
|
@ -92,10 +100,10 @@ export const useChatList = () => {
@@ -92,10 +100,10 @@ export const useChatList = () => {
|
|
|
|
|
|
|
|
|
|
const onPin = async (chatId: number) => { |
|
|
|
|
try { |
|
|
|
|
const chatIndex = chats.findIndex((item) => item.id === chatId); |
|
|
|
|
const chatIndex = chats.findIndex(item => item.id === chatId); |
|
|
|
|
if (chats[chatIndex].isChatFixed) return unPin(chatId); |
|
|
|
|
|
|
|
|
|
const pinned = chats.filter((item) => item.isChatFixed); |
|
|
|
|
const pinned = chats.filter(item => item.isChatFixed); |
|
|
|
|
|
|
|
|
|
if (pinned.length > 4) { |
|
|
|
|
// alert("max pinned chats 5!");
|
|
|
|
@ -104,7 +112,7 @@ export const useChatList = () => {
@@ -104,7 +112,7 @@ export const useChatList = () => {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const lastPinnedIndex = chats.findIndex( |
|
|
|
|
(item) => item.id === pinned[pinned.length - 1]?.id |
|
|
|
|
item => item.id === pinned[pinned.length - 1]?.id |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
await chatsApi.pinChatReq({ chatId, order: lastPinnedIndex + 2 }); |
|
|
|
@ -124,14 +132,13 @@ export const useChatList = () => {
@@ -124,14 +132,13 @@ export const useChatList = () => {
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const isPinned = (chatId: number): boolean => { |
|
|
|
|
const pinned = |
|
|
|
|
chats.find((item) => item.id === chatId)?.isChatFixed || false; |
|
|
|
|
const pinned = chats.find(item => item.id === chatId)?.isChatFixed || false; |
|
|
|
|
return pinned; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const isUnread = (chatId: number): boolean => { |
|
|
|
|
const readed = |
|
|
|
|
chats.find((item) => item.id === chatId)?.isChatUnread || false; |
|
|
|
|
chats.find(item => item.id === chatId)?.isChatUnread || false; |
|
|
|
|
return readed; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -139,18 +146,18 @@ export const useChatList = () => {
@@ -139,18 +146,18 @@ export const useChatList = () => {
|
|
|
|
|
(data: { chatId: number }) => { |
|
|
|
|
const chat = _.find( |
|
|
|
|
chats, |
|
|
|
|
(chat) => Number(chat.id) === Number(data.chatId) |
|
|
|
|
chat => Number(chat.id) === Number(data.chatId) |
|
|
|
|
); |
|
|
|
|
if (!chat || (chat?.unreadMessagesCount <= 0 && !chat?.isChatUnread)) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
_setItems( |
|
|
|
|
chats.map((it) => { |
|
|
|
|
chats.map(it => { |
|
|
|
|
if (Number(it.id) !== Number(data.chatId)) return it; |
|
|
|
|
return { |
|
|
|
|
...it, |
|
|
|
|
unreadMessagesCount: 0, |
|
|
|
|
isChatUnread: false, |
|
|
|
|
isChatUnread: false |
|
|
|
|
}; |
|
|
|
|
}) |
|
|
|
|
); |
|
|
|
@ -179,7 +186,7 @@ export const useChatList = () => {
@@ -179,7 +186,7 @@ export const useChatList = () => {
|
|
|
|
|
const onChatPinned = (data: SocketEvents["chat/pin"]) => { |
|
|
|
|
const chat = _.find( |
|
|
|
|
chats, |
|
|
|
|
(chat) => chat.id.toString() === data.chatId.toString() |
|
|
|
|
chat => chat.id.toString() === data.chatId.toString() |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if (!chat || chat.isChatFixed) return; |
|
|
|
@ -189,7 +196,7 @@ export const useChatList = () => {
@@ -189,7 +196,7 @@ export const useChatList = () => {
|
|
|
|
|
const onChatUnpinned = (data: SocketEvents["chat/unpin"]) => { |
|
|
|
|
const chat = _.find( |
|
|
|
|
chats, |
|
|
|
|
(chat) => chat.id.toString() === data.chatId.toString() |
|
|
|
|
chat => chat.id.toString() === data.chatId.toString() |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if (!chat || !chat.isChatFixed) return; |
|
|
|
@ -200,13 +207,13 @@ export const useChatList = () => {
@@ -200,13 +207,13 @@ export const useChatList = () => {
|
|
|
|
|
const onSetChatUnread = (data: SocketEvents["chat/unread"]) => { |
|
|
|
|
const chat = _.find( |
|
|
|
|
chats, |
|
|
|
|
(chat) => chat.id.toString() === data.chatId.toString() |
|
|
|
|
chat => chat.id.toString() === data.chatId.toString() |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if (!chat || chat.isChatUnread) return; |
|
|
|
|
|
|
|
|
|
_setItems( |
|
|
|
|
_.clone(chats).map((it) => { |
|
|
|
|
_.clone(chats).map(it => { |
|
|
|
|
if (it.id.toString() === data.chatId.toString()) it.isChatUnread = true; |
|
|
|
|
return it; |
|
|
|
|
}) |
|
|
|
@ -242,12 +249,12 @@ export const useChatList = () => {
@@ -242,12 +249,12 @@ export const useChatList = () => {
|
|
|
|
|
"onClearAllChats", |
|
|
|
|
() => { |
|
|
|
|
_setItems( |
|
|
|
|
chats.map((it) => ({ |
|
|
|
|
chats.map(it => ({ |
|
|
|
|
...it, |
|
|
|
|
lastMessage: null, |
|
|
|
|
lastMessageDate: it.createdAt, |
|
|
|
|
unreadMessagesCount: 0, |
|
|
|
|
isChatUnread: false, |
|
|
|
|
isChatUnread: false |
|
|
|
|
})) |
|
|
|
|
); |
|
|
|
|
}, |
|
|
|
@ -303,6 +310,6 @@ export const useChatList = () => {
@@ -303,6 +310,6 @@ export const useChatList = () => {
|
|
|
|
|
isPinned, |
|
|
|
|
isUnread, |
|
|
|
|
onPin, |
|
|
|
|
onSetUnread, |
|
|
|
|
onSetUnread |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|