Skip to content

Commit ced4d2d

Browse files
committed
fix: avoid redundant thread activity writes
1 parent 8ab6bcc commit ced4d2d

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/features/threads/hooks/useThreads.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,13 +886,34 @@ export function useThreads({
886886
});
887887
const uniqueThreads = Array.from(uniqueById.values());
888888
const activityByThread = threadActivityRef.current[workspace.id] ?? {};
889+
const nextActivityByThread = { ...activityByThread };
890+
let didChangeActivity = false;
891+
uniqueThreads.forEach((thread) => {
892+
const threadId = String(thread?.id ?? "");
893+
if (!threadId) {
894+
return;
895+
}
896+
const timestamp = getThreadTimestamp(thread);
897+
if (timestamp > (nextActivityByThread[threadId] ?? 0)) {
898+
nextActivityByThread[threadId] = timestamp;
899+
didChangeActivity = true;
900+
}
901+
});
902+
if (didChangeActivity) {
903+
const next = {
904+
...threadActivityRef.current,
905+
[workspace.id]: nextActivityByThread,
906+
};
907+
threadActivityRef.current = next;
908+
saveThreadActivity(next);
909+
}
889910
uniqueThreads.sort((a, b) => {
890911
const aId = String(a?.id ?? "");
891912
const bId = String(b?.id ?? "");
892913
const aCreated = getThreadTimestamp(a);
893914
const bCreated = getThreadTimestamp(b);
894-
const aActivity = Math.max(activityByThread[aId] ?? 0, aCreated);
895-
const bActivity = Math.max(activityByThread[bId] ?? 0, bCreated);
915+
const aActivity = Math.max(nextActivityByThread[aId] ?? 0, aCreated);
916+
const bActivity = Math.max(nextActivityByThread[bId] ?? 0, bCreated);
896917
return bActivity - aActivity;
897918
});
898919
const summaries = uniqueThreads

0 commit comments

Comments
 (0)