mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-24 23:06:51 +00:00
修复朋友圈联系人重复加载的问题
This commit is contained in:
@@ -176,6 +176,8 @@ export default function SnsPage() {
|
|||||||
const selectedContactUsernamesRef = useRef<string[]>(selectedContactUsernames)
|
const selectedContactUsernamesRef = useRef<string[]>(selectedContactUsernames)
|
||||||
const cacheScopeKeyRef = useRef('')
|
const cacheScopeKeyRef = useRef('')
|
||||||
const snsUserPostCountsCacheScopeKeyRef = useRef('')
|
const snsUserPostCountsCacheScopeKeyRef = useRef('')
|
||||||
|
const activeContactsLoadTaskIdRef = useRef<string | null>(null)
|
||||||
|
const activeContactsCountTaskIdRef = useRef<string | null>(null)
|
||||||
const scrollAdjustmentRef = useRef<{ scrollHeight: number; scrollTop: number } | null>(null)
|
const scrollAdjustmentRef = useRef<{ scrollHeight: number; scrollTop: number } | null>(null)
|
||||||
const pendingResetFeedRef = useRef(false)
|
const pendingResetFeedRef = useRef(false)
|
||||||
const contactsLoadTokenRef = useRef(0)
|
const contactsLoadTokenRef = useRef(0)
|
||||||
@@ -750,6 +752,12 @@ export default function SnsPage() {
|
|||||||
window.clearTimeout(contactsCountBatchTimerRef.current)
|
window.clearTimeout(contactsCountBatchTimerRef.current)
|
||||||
contactsCountBatchTimerRef.current = null
|
contactsCountBatchTimerRef.current = null
|
||||||
}
|
}
|
||||||
|
if (activeContactsCountTaskIdRef.current) {
|
||||||
|
finishBackgroundTask(activeContactsCountTaskIdRef.current, 'canceled', {
|
||||||
|
detail: '已停止后续联系人朋友圈条数补算'
|
||||||
|
})
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
if (resetProgress) {
|
if (resetProgress) {
|
||||||
setContactsCountProgress({
|
setContactsCountProgress({
|
||||||
resolved: 0,
|
resolved: 0,
|
||||||
@@ -814,31 +822,56 @@ export default function SnsPage() {
|
|||||||
cancelable: true
|
cancelable: true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
activeContactsCountTaskIdRef.current = taskId
|
||||||
let normalizedCounts: Record<string, number> = {}
|
let normalizedCounts: Record<string, number> = {}
|
||||||
try {
|
try {
|
||||||
const result = await window.electronAPI.sns.getUserPostCounts()
|
const result = await window.electronAPI.sns.getUserPostCounts()
|
||||||
if (isBackgroundTaskCancelRequested(taskId)) {
|
if (isBackgroundTaskCancelRequested(taskId)) {
|
||||||
|
if (activeContactsCountTaskIdRef.current === taskId) {
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
finishBackgroundTask(taskId, 'canceled', {
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
detail: '已停止后续加载,当前计数查询结束后不再继续分批写入'
|
detail: '已停止后续加载,当前计数查询结束后不再继续分批写入'
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (runToken !== contactsCountHydrationTokenRef.current) return
|
if (runToken !== contactsCountHydrationTokenRef.current) {
|
||||||
|
if (activeContactsCountTaskIdRef.current === taskId) {
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
|
detail: '页面状态已刷新,本次联系人朋友圈条数补算已过期'
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
if (result.success && result.counts) {
|
if (result.success && result.counts) {
|
||||||
normalizedCounts = Object.fromEntries(
|
normalizedCounts = pendingTargets.reduce<Record<string, number>>((acc, username) => {
|
||||||
Object.entries(result.counts).map(([username, value]) => [username, normalizePostCount(value)])
|
acc[username] = normalizePostCount(result.counts?.[username])
|
||||||
)
|
return acc
|
||||||
|
}, {})
|
||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const scopeKey = await ensureSnsUserPostCountsCacheScopeKey()
|
const scopeKey = await ensureSnsUserPostCountsCacheScopeKey()
|
||||||
await configService.setExportSnsUserPostCountsCache(scopeKey, normalizedCounts)
|
const currentCache = await configService.getExportSnsUserPostCountsCache(scopeKey)
|
||||||
|
await configService.setExportSnsUserPostCountsCache(scopeKey, {
|
||||||
|
...(currentCache?.counts || {}),
|
||||||
|
...normalizedCounts
|
||||||
|
})
|
||||||
} catch (cacheError) {
|
} catch (cacheError) {
|
||||||
console.error('Failed to persist SNS user post counts cache:', cacheError)
|
console.error('Failed to persist SNS user post counts cache:', cacheError)
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
} else {
|
||||||
|
normalizedCounts = pendingTargets.reduce<Record<string, number>>((acc, username) => {
|
||||||
|
acc[username] = 0
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load contact post counts:', error)
|
console.error('Failed to load contact post counts:', error)
|
||||||
|
if (activeContactsCountTaskIdRef.current === taskId) {
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
finishBackgroundTask(taskId, 'failed', {
|
finishBackgroundTask(taskId, 'failed', {
|
||||||
detail: String(error)
|
detail: String(error)
|
||||||
})
|
})
|
||||||
@@ -848,8 +881,19 @@ export default function SnsPage() {
|
|||||||
let resolved = preResolved
|
let resolved = preResolved
|
||||||
let cursor = 0
|
let cursor = 0
|
||||||
const applyBatch = () => {
|
const applyBatch = () => {
|
||||||
if (runToken !== contactsCountHydrationTokenRef.current) return
|
if (runToken !== contactsCountHydrationTokenRef.current) {
|
||||||
|
if (activeContactsCountTaskIdRef.current === taskId) {
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
|
detail: '页面状态已刷新,本次联系人朋友圈条数补算已过期'
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
if (isBackgroundTaskCancelRequested(taskId)) {
|
if (isBackgroundTaskCancelRequested(taskId)) {
|
||||||
|
if (activeContactsCountTaskIdRef.current === taskId) {
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
finishBackgroundTask(taskId, 'canceled', {
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
detail: `已停止后续加载,已完成 ${resolved}/${totalTargets}`
|
detail: `已停止后续加载,已完成 ${resolved}/${totalTargets}`
|
||||||
})
|
})
|
||||||
@@ -870,6 +914,9 @@ export default function SnsPage() {
|
|||||||
running: false
|
running: false
|
||||||
})
|
})
|
||||||
contactsCountBatchTimerRef.current = null
|
contactsCountBatchTimerRef.current = null
|
||||||
|
if (activeContactsCountTaskIdRef.current === taskId) {
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
finishBackgroundTask(taskId, 'completed', {
|
finishBackgroundTask(taskId, 'completed', {
|
||||||
detail: '联系人朋友圈条数补算完成',
|
detail: '联系人朋友圈条数补算完成',
|
||||||
progressText: `${totalTargets}/${totalTargets}`
|
progressText: `${totalTargets}/${totalTargets}`
|
||||||
@@ -910,6 +957,18 @@ export default function SnsPage() {
|
|||||||
contactsCountBatchTimerRef.current = window.setTimeout(applyBatch, CONTACT_COUNT_SORT_DEBOUNCE_MS)
|
contactsCountBatchTimerRef.current = window.setTimeout(applyBatch, CONTACT_COUNT_SORT_DEBOUNCE_MS)
|
||||||
} else {
|
} else {
|
||||||
contactsCountBatchTimerRef.current = null
|
contactsCountBatchTimerRef.current = null
|
||||||
|
setContactsCountProgress({
|
||||||
|
resolved: totalTargets,
|
||||||
|
total: totalTargets,
|
||||||
|
running: false
|
||||||
|
})
|
||||||
|
if (activeContactsCountTaskIdRef.current === taskId) {
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
|
finishBackgroundTask(taskId, 'completed', {
|
||||||
|
detail: '鑱旂郴浜烘湅鍙嬪湀鏉℃暟琛ョ畻瀹屾垚',
|
||||||
|
progressText: `${totalTargets}/${totalTargets}`
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -918,6 +977,12 @@ export default function SnsPage() {
|
|||||||
|
|
||||||
// Load Contacts(先按最近会话显示联系人,再异步统计朋友圈条数并增量排序)
|
// Load Contacts(先按最近会话显示联系人,再异步统计朋友圈条数并增量排序)
|
||||||
const loadContacts = useCallback(async () => {
|
const loadContacts = useCallback(async () => {
|
||||||
|
if (activeContactsLoadTaskIdRef.current) {
|
||||||
|
finishBackgroundTask(activeContactsLoadTaskIdRef.current, 'canceled', {
|
||||||
|
detail: '新一轮联系人列表加载已开始,旧任务已取消'
|
||||||
|
})
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
const requestToken = ++contactsLoadTokenRef.current
|
const requestToken = ++contactsLoadTokenRef.current
|
||||||
const taskId = registerBackgroundTask({
|
const taskId = registerBackgroundTask({
|
||||||
sourcePage: 'sns',
|
sourcePage: 'sns',
|
||||||
@@ -926,6 +991,7 @@ export default function SnsPage() {
|
|||||||
progressText: '初始化',
|
progressText: '初始化',
|
||||||
cancelable: true
|
cancelable: true
|
||||||
})
|
})
|
||||||
|
activeContactsLoadTaskIdRef.current = taskId
|
||||||
stopContactsCountHydration(true)
|
stopContactsCountHydration(true)
|
||||||
setContactsLoading(true)
|
setContactsLoading(true)
|
||||||
try {
|
try {
|
||||||
@@ -955,7 +1021,15 @@ export default function SnsPage() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (requestToken !== contactsLoadTokenRef.current) return
|
if (requestToken !== contactsLoadTokenRef.current) {
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
|
detail: '页面状态已刷新,本次联系人列表加载已过期'
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
if (cachedContacts.length > 0) {
|
if (cachedContacts.length > 0) {
|
||||||
const cachedContactsSorted = sortContactsForRanking(cachedContacts)
|
const cachedContactsSorted = sortContactsForRanking(cachedContacts)
|
||||||
setContacts(cachedContactsSorted)
|
setContacts(cachedContactsSorted)
|
||||||
@@ -977,6 +1051,9 @@ export default function SnsPage() {
|
|||||||
window.electronAPI.chat.getSessions()
|
window.electronAPI.chat.getSessions()
|
||||||
])
|
])
|
||||||
if (isBackgroundTaskCancelRequested(taskId)) {
|
if (isBackgroundTaskCancelRequested(taskId)) {
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
finishBackgroundTask(taskId, 'canceled', {
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
detail: '已停止后续加载,当前联系人查询结束后未继续补齐'
|
detail: '已停止后续加载,当前联系人查询结束后未继续补齐'
|
||||||
})
|
})
|
||||||
@@ -1021,7 +1098,15 @@ export default function SnsPage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let contactsList = sortContactsForRanking(Array.from(contactMap.values()))
|
let contactsList = sortContactsForRanking(Array.from(contactMap.values()))
|
||||||
if (requestToken !== contactsLoadTokenRef.current) return
|
if (requestToken !== contactsLoadTokenRef.current) {
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
|
detail: '页面状态已刷新,本次联系人列表加载已过期'
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
setContacts(contactsList)
|
setContacts(contactsList)
|
||||||
const readyUsernames = new Set(
|
const readyUsernames = new Set(
|
||||||
contactsList
|
contactsList
|
||||||
@@ -1043,6 +1128,9 @@ export default function SnsPage() {
|
|||||||
})
|
})
|
||||||
const enriched = await window.electronAPI.chat.enrichSessionsContactInfo(allUsernames)
|
const enriched = await window.electronAPI.chat.enrichSessionsContactInfo(allUsernames)
|
||||||
if (isBackgroundTaskCancelRequested(taskId)) {
|
if (isBackgroundTaskCancelRequested(taskId)) {
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
finishBackgroundTask(taskId, 'canceled', {
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
detail: '已停止后续加载,联系人补齐未继续写入'
|
detail: '已停止后续加载,联系人补齐未继续写入'
|
||||||
})
|
})
|
||||||
@@ -1058,7 +1146,15 @@ export default function SnsPage() {
|
|||||||
avatarUrl: extra.avatarUrl || contact.avatarUrl
|
avatarUrl: extra.avatarUrl || contact.avatarUrl
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if (requestToken !== contactsLoadTokenRef.current) return
|
if (requestToken !== contactsLoadTokenRef.current) {
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
|
detail: '页面状态已刷新,本次联系人列表加载已过期'
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
setContacts((prev) => {
|
setContacts((prev) => {
|
||||||
const prevMap = new Map(prev.map((contact) => [contact.username, contact]))
|
const prevMap = new Map(prev.map((contact) => [contact.username, contact]))
|
||||||
const merged = contactsList.map((contact) => {
|
const merged = contactsList.map((contact) => {
|
||||||
@@ -1074,18 +1170,35 @@ export default function SnsPage() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
finishBackgroundTask(taskId, 'completed', {
|
finishBackgroundTask(taskId, 'completed', {
|
||||||
detail: `朋友圈联系人列表加载完成,共 ${contactsList.length} 人`,
|
detail: `朋友圈联系人列表加载完成,共 ${contactsList.length} 人`,
|
||||||
progressText: `${contactsList.length} 人`
|
progressText: `${contactsList.length} 人`
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (requestToken !== contactsLoadTokenRef.current) return
|
if (requestToken !== contactsLoadTokenRef.current) {
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
|
finishBackgroundTask(taskId, 'canceled', {
|
||||||
|
detail: '页面状态已刷新,本次联系人列表加载已过期'
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
console.error('Failed to load contacts:', error)
|
console.error('Failed to load contacts:', error)
|
||||||
stopContactsCountHydration(true)
|
stopContactsCountHydration(true)
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
finishBackgroundTask(taskId, 'failed', {
|
finishBackgroundTask(taskId, 'failed', {
|
||||||
detail: String(error)
|
detail: String(error)
|
||||||
})
|
})
|
||||||
} finally {
|
} finally {
|
||||||
|
if (activeContactsLoadTaskIdRef.current === taskId && requestToken !== contactsLoadTokenRef.current) {
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
if (requestToken === contactsLoadTokenRef.current) {
|
if (requestToken === contactsLoadTokenRef.current) {
|
||||||
setContactsLoading(false)
|
setContactsLoading(false)
|
||||||
}
|
}
|
||||||
@@ -1185,6 +1298,18 @@ export default function SnsPage() {
|
|||||||
window.clearTimeout(contactsCountBatchTimerRef.current)
|
window.clearTimeout(contactsCountBatchTimerRef.current)
|
||||||
contactsCountBatchTimerRef.current = null
|
contactsCountBatchTimerRef.current = null
|
||||||
}
|
}
|
||||||
|
if (activeContactsCountTaskIdRef.current) {
|
||||||
|
finishBackgroundTask(activeContactsCountTaskIdRef.current, 'canceled', {
|
||||||
|
detail: '已离开朋友圈页,联系人朋友圈条数补算已取消'
|
||||||
|
})
|
||||||
|
activeContactsCountTaskIdRef.current = null
|
||||||
|
}
|
||||||
|
if (activeContactsLoadTaskIdRef.current) {
|
||||||
|
finishBackgroundTask(activeContactsLoadTaskIdRef.current, 'canceled', {
|
||||||
|
detail: '已离开朋友圈页,联系人列表加载已取消'
|
||||||
|
})
|
||||||
|
activeContactsLoadTaskIdRef.current = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user