From 84fdba892a3378fc986fa7b72d21ff603c02b583 Mon Sep 17 00:00:00 2001 From: Lei OT Date: Mon, 8 Apr 2024 11:11:46 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=9C=A8=E7=BA=BF=E7=AA=97=E5=8F=A3:?= =?UTF-8?q?=20=E4=BC=9A=E8=AF=9D=E5=88=97=E8=A1=A8=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/ConversationStore.js | 11 ++++- .../Online/ConversationsList.jsx | 40 +++++++++++++++---- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/stores/ConversationStore.js b/src/stores/ConversationStore.js index 1591b7e..1b8c83b 100644 --- a/src/stores/ConversationStore.js +++ b/src/stores/ConversationStore.js @@ -148,9 +148,12 @@ const complexMsgSlice = (set) => ({ }); const conversationSlice = (set, get) => ({ + conversationsListLoading: false, conversationsList: [], currentConversation: {}, + setConversationsListLoading: (conversationsListLoading) => set({ conversationsListLoading }), + /** * @deprecated */ @@ -167,10 +170,14 @@ const conversationSlice = (set, get) => ({ const newListIds = newList.map((chatItem) => `${chatItem.sn}`); const withoutNew = conversationsList.filter((item) => !newListIds.includes(`${item.sn}`)); + const mergedList = [...newList, ...withoutNew]; + const refreshTotalNotify = mergedList.reduce((r, c) => r+c.unread_msg_count, 0); + return set((state) => ({ - conversationsList: [...newList, ...withoutNew], + conversationsList: mergedList, activeConversations: { ...activeConversations, ...newConversationsMapped }, - totalNotify: state.totalNotify + newConversations.map((ele) => ele.unread_msg_count).reduce((acc, cur) => acc + (cur || 0), 0), + totalNotify: refreshTotalNotify, + // totalNotify: state.totalNotify + newConversations.map((ele) => ele.unread_msg_count).reduce((acc, cur) => acc + (cur || 0), 0), })); }, delConversationitem: (conversation) => { diff --git a/src/views/Conversations/Online/ConversationsList.jsx b/src/views/Conversations/Online/ConversationsList.jsx index d2ed052..8f3eb2f 100644 --- a/src/views/Conversations/Online/ConversationsList.jsx +++ b/src/views/Conversations/Online/ConversationsList.jsx @@ -1,12 +1,13 @@ import { useEffect, useState, useRef } from 'react'; import { useParams, useNavigate, useLocation } from 'react-router-dom'; import { Dropdown, Input, Button, } from 'antd'; -import { PlusOutlined, WhatsAppOutlined, } from '@ant-design/icons'; -import { fetchOrderConversationsList, fetchConversationItemClose, postNewConversationItem } from '@/actions/ConversationActions'; +import { PlusOutlined, WhatsAppOutlined, LoadingOutlined } from '@ant-design/icons'; +import { fetchConversationsList, fetchOrderConversationsList, fetchConversationItemClose, postNewConversationItem } from '@/actions/ConversationActions'; import { ChatItem } from 'react-chat-elements'; -import { isEmpty } from '@/utils/commons'; +import { isEmpty, olog } from '@/utils/commons'; import useConversationStore from '@/stores/ConversationStore'; import useAuthStore from '@/stores/AuthStore'; +import { useVisibilityState } from '@/hooks/useVisibilityState'; /** * [] @@ -22,19 +23,36 @@ const Conversations = ({ mobile }) => { const initialState = useConversationStore((state) => state.initialState); const [currentConversation, setCurrentConversation] = useConversationStore((state) => [state.currentConversation, state.setCurrentConversation]); const conversationsList = useConversationStore((state) => state.conversationsList); + const [conversationsListLoading, setConversationsListLoading] = useConversationStore((state) => [state.conversationsListLoading, state.setConversationsListLoading]); const addToConversationList = useConversationStore((state) => state.addToConversationList); const delConversationitem = useConversationStore((state) => state.delConversationitem); + const isVisible = useVisibilityState(); + const [tabSelectedConversation, setTabSelectedConversation] = useState({}); const [tabCnt, setTabCnt] = useState(-1); + async function refreshConversationList() { + setConversationsListLoading(mobile !== undefined ? true : false); + const _list = await fetchConversationsList({opisn:userId}); + addToConversationList(_list); + setConversationsListLoading(false); + } + useEffect(() => { if (mobile !== undefined) { setCurrentConversation({}); } + return () => {}; + }, []); + + useEffect(() => { + if (isVisible) { + refreshConversationList(); + } return () => {}; - }, []) + }, [isVisible]); const [dataSource, setDataSource] = useState(conversationsList); @@ -143,10 +161,10 @@ const Conversations = ({ mobile }) => { setTabCnt(-1); setTabSelectedConversation({}); }} - onKeyDown={e => { + onKeyDown={(e) => { if (e.key === 'Tab') { e.preventDefault(); - const _this = tabCnt >= dataSource.length-1 ? 0 : tabCnt + 1 + const _this = tabCnt >= dataSource.length - 1 ? 0 : tabCnt + 1; setTabCnt(_this); setTabSelectedConversation(dataSource[_this]); } @@ -154,15 +172,21 @@ const Conversations = ({ mobile }) => { onPressEnter={(e) => { handleSearchConversations(e.target.value); searchInputRef.current.blur(); - onSwitchConversation(dataSource[(tabCnt < 0 ? 0 : tabCnt)]); + onSwitchConversation(dataSource[tabCnt < 0 ? 0 : tabCnt]); setTabCnt(-1); setTabSelectedConversation({}); return false; }} - placeholder='搜索名称' + placeholder={`搜索名称${conversationsListLoading ? '...' : ''}`} />
+ {conversationsListLoading && dataSource.length === 0 ? ( +
+ +
+ ) : null} + {dataSource.map((item) => (