mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-25 07:16:51 +00:00
fix: 修复搜索无法取消/后台持续占用问题
- 全局搜索和会话内搜索均加 generation 计数,新搜索触发时丢弃旧结果 - 防抖从 400ms 统一改为 500ms - 关闭搜索时立即取消 pending 的 timer 和 generation
This commit is contained in:
@@ -2620,60 +2620,88 @@ function ChatPage(props: ChatPageProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 会话内搜索
|
// 会话内搜索
|
||||||
|
const inSessionSearchTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||||
|
const inSessionSearchGenRef = useRef(0)
|
||||||
const handleInSessionSearch = useCallback(async (keyword: string) => {
|
const handleInSessionSearch = useCallback(async (keyword: string) => {
|
||||||
setInSessionQuery(keyword)
|
setInSessionQuery(keyword)
|
||||||
|
if (inSessionSearchTimerRef.current) clearTimeout(inSessionSearchTimerRef.current)
|
||||||
|
inSessionSearchGenRef.current += 1
|
||||||
if (!keyword.trim() || !currentSessionId) {
|
if (!keyword.trim() || !currentSessionId) {
|
||||||
setInSessionResults([])
|
setInSessionResults([])
|
||||||
|
setInSessionSearching(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const gen = inSessionSearchGenRef.current
|
||||||
|
const sid = currentSessionId
|
||||||
|
inSessionSearchTimerRef.current = setTimeout(async () => {
|
||||||
|
if (gen !== inSessionSearchGenRef.current) return
|
||||||
setInSessionSearching(true)
|
setInSessionSearching(true)
|
||||||
try {
|
try {
|
||||||
const res = await window.electronAPI.chat.searchMessages(keyword.trim(), currentSessionId, 50, 0)
|
const res = await window.electronAPI.chat.searchMessages(keyword.trim(), sid, 50, 0)
|
||||||
|
if (gen !== inSessionSearchGenRef.current) return
|
||||||
setInSessionResults(res?.messages || [])
|
setInSessionResults(res?.messages || [])
|
||||||
} catch {
|
} catch {
|
||||||
|
if (gen !== inSessionSearchGenRef.current) return
|
||||||
setInSessionResults([])
|
setInSessionResults([])
|
||||||
} finally {
|
} finally {
|
||||||
setInSessionSearching(false)
|
if (gen === inSessionSearchGenRef.current) setInSessionSearching(false)
|
||||||
}
|
}
|
||||||
|
}, 500)
|
||||||
}, [currentSessionId])
|
}, [currentSessionId])
|
||||||
|
|
||||||
const handleToggleInSessionSearch = useCallback(() => {
|
const handleToggleInSessionSearch = useCallback(() => {
|
||||||
setShowInSessionSearch(v => {
|
setShowInSessionSearch(v => {
|
||||||
if (v) { setInSessionQuery(''); setInSessionResults([]) }
|
if (v) {
|
||||||
else setTimeout(() => inSessionSearchRef.current?.focus(), 50)
|
inSessionSearchGenRef.current += 1
|
||||||
|
if (inSessionSearchTimerRef.current) clearTimeout(inSessionSearchTimerRef.current)
|
||||||
|
setInSessionQuery('')
|
||||||
|
setInSessionResults([])
|
||||||
|
setInSessionSearching(false)
|
||||||
|
} else {
|
||||||
|
setTimeout(() => inSessionSearchRef.current?.focus(), 50)
|
||||||
|
}
|
||||||
return !v
|
return !v
|
||||||
})
|
})
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
// 全局消息搜索
|
// 全局消息搜索
|
||||||
const globalMsgSearchTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
const globalMsgSearchTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||||
|
const globalMsgSearchGenRef = useRef(0)
|
||||||
const handleGlobalMsgSearch = useCallback(async (keyword: string) => {
|
const handleGlobalMsgSearch = useCallback(async (keyword: string) => {
|
||||||
setGlobalMsgQuery(keyword)
|
setGlobalMsgQuery(keyword)
|
||||||
if (globalMsgSearchTimerRef.current) clearTimeout(globalMsgSearchTimerRef.current)
|
if (globalMsgSearchTimerRef.current) clearTimeout(globalMsgSearchTimerRef.current)
|
||||||
|
globalMsgSearchGenRef.current += 1
|
||||||
if (!keyword.trim()) {
|
if (!keyword.trim()) {
|
||||||
setGlobalMsgResults([])
|
setGlobalMsgResults([])
|
||||||
setShowGlobalMsgSearch(false)
|
setShowGlobalMsgSearch(false)
|
||||||
|
setGlobalMsgSearching(false)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setShowGlobalMsgSearch(true)
|
setShowGlobalMsgSearch(true)
|
||||||
|
const gen = globalMsgSearchGenRef.current
|
||||||
globalMsgSearchTimerRef.current = setTimeout(async () => {
|
globalMsgSearchTimerRef.current = setTimeout(async () => {
|
||||||
|
if (gen !== globalMsgSearchGenRef.current) return
|
||||||
setGlobalMsgSearching(true)
|
setGlobalMsgSearching(true)
|
||||||
try {
|
try {
|
||||||
const res = await window.electronAPI.chat.searchMessages(keyword.trim(), undefined, 50, 0)
|
const res = await window.electronAPI.chat.searchMessages(keyword.trim(), undefined, 50, 0)
|
||||||
|
if (gen !== globalMsgSearchGenRef.current) return
|
||||||
setGlobalMsgResults(res?.messages || [])
|
setGlobalMsgResults(res?.messages || [])
|
||||||
} catch {
|
} catch {
|
||||||
|
if (gen !== globalMsgSearchGenRef.current) return
|
||||||
setGlobalMsgResults([])
|
setGlobalMsgResults([])
|
||||||
} finally {
|
} finally {
|
||||||
setGlobalMsgSearching(false)
|
if (gen === globalMsgSearchGenRef.current) setGlobalMsgSearching(false)
|
||||||
}
|
}
|
||||||
}, 400)
|
}, 500)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const handleCloseGlobalMsgSearch = useCallback(() => {
|
const handleCloseGlobalMsgSearch = useCallback(() => {
|
||||||
|
globalMsgSearchGenRef.current += 1
|
||||||
|
if (globalMsgSearchTimerRef.current) clearTimeout(globalMsgSearchTimerRef.current)
|
||||||
setShowGlobalMsgSearch(false)
|
setShowGlobalMsgSearch(false)
|
||||||
setGlobalMsgQuery('')
|
setGlobalMsgQuery('')
|
||||||
setGlobalMsgResults([])
|
setGlobalMsgResults([])
|
||||||
if (globalMsgSearchTimerRef.current) clearTimeout(globalMsgSearchTimerRef.current)
|
setGlobalMsgSearching(false)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
// 滚动加载更多 + 显示/隐藏回到底部按钮(优化:节流,避免频繁执行)
|
// 滚动加载更多 + 显示/隐藏回到底部按钮(优化:节流,避免频繁执行)
|
||||||
|
|||||||
Reference in New Issue
Block a user