diff --git a/src/pages/ChatPage.tsx b/src/pages/ChatPage.tsx index 0b31d39..0e32b64 100644 --- a/src/pages/ChatPage.tsx +++ b/src/pages/ChatPage.tsx @@ -2646,27 +2646,34 @@ function ChatPage(props: ChatPageProps) { }, []) // 全局消息搜索 + const globalMsgSearchTimerRef = useRef | null>(null) const handleGlobalMsgSearch = useCallback(async (keyword: string) => { setGlobalMsgQuery(keyword) + if (globalMsgSearchTimerRef.current) clearTimeout(globalMsgSearchTimerRef.current) if (!keyword.trim()) { setGlobalMsgResults([]) + setShowGlobalMsgSearch(false) return } - setGlobalMsgSearching(true) - try { - const res = await window.electronAPI.chat.searchMessages(keyword.trim(), undefined, 50, 0) - setGlobalMsgResults(res?.messages || []) - } catch { - setGlobalMsgResults([]) - } finally { - setGlobalMsgSearching(false) - } + setShowGlobalMsgSearch(true) + globalMsgSearchTimerRef.current = setTimeout(async () => { + setGlobalMsgSearching(true) + try { + const res = await window.electronAPI.chat.searchMessages(keyword.trim(), undefined, 50, 0) + setGlobalMsgResults(res?.messages || []) + } catch { + setGlobalMsgResults([]) + } finally { + setGlobalMsgSearching(false) + } + }, 400) }, []) const handleCloseGlobalMsgSearch = useCallback(() => { setShowGlobalMsgSearch(false) setGlobalMsgQuery('') setGlobalMsgResults([]) + if (globalMsgSearchTimerRef.current) clearTimeout(globalMsgSearchTimerRef.current) }, []) // 滚动加载更多 + 显示/隐藏回到底部按钮(优化:节流,避免频繁执行) @@ -3956,26 +3963,20 @@ function ChatPage(props: ChatPageProps) { showGlobalMsgSearch ? handleGlobalMsgSearch(e.target.value) : handleSearch(e.target.value)} + placeholder="搜索" + value={searchKeyword} + onChange={(e) => { + handleSearch(e.target.value) + handleGlobalMsgSearch(e.target.value) + }} /> - {(showGlobalMsgSearch ? globalMsgQuery : searchKeyword) && ( - )} + {globalMsgSearching && } - @@ -3989,22 +3990,27 @@ function ChatPage(props: ChatPageProps) { {!globalMsgSearching && globalMsgQuery && globalMsgResults.length === 0 && (
没有找到相关消息
)} - {globalMsgResults.map((msg, i) => ( -
{ - const sid = msg._session_id || msg.username - if (sid) { - const target = sessions.find(s => s.username === sid) - if (target) { - handleSelectSession(target) + {globalMsgResults.map((msg, i) => { + const sid = msg._session_id || msg.username || '' + const sessionObj = sessions.find(s => s.username === sid) + const sessionName = sessionObj?.displayName || sid || '未知会话' + const content = (msg.content || msg.strContent || msg.message_content || '').slice(0, 60) + const ts = msg.createTime || msg.create_time + const timeStr = ts ? new Date(ts * 1000).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }) : '' + return ( +
{ + if (sessionObj) { + handleSelectSession(sessionObj) handleCloseGlobalMsgSearch() + setSearchKeyword('') } - } - }}> -
{msg._session_id || msg.username || '未知会话'}
-
{(msg.content || msg.strContent || msg.message_content || '').slice(0, 60)}
-
{(msg.createTime || msg.create_time) ? new Date((msg.createTime || msg.create_time) * 1000).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }) : ''}
-
- ))} + }}> +
{sessionName}
+
{content}
+
{timeStr}
+
+ ) + })} )}