fix(thread): count user id tooltips from thread data

Replace the DOM-based user ID tooltip count with a deduped per-thread author map in `getThreadPostCountsByAuthor()`. This keeps mounted preview copies from doubling ID totals on desktop and mobile thread views.
This commit is contained in:
Tommaso Casaburi
2026-03-16 15:13:04 +08:00
parent 47e8c5a37b
commit 6ecf3c40d3
4 changed files with 87 additions and 12 deletions
@@ -0,0 +1,39 @@
import { describe, expect, it } from 'vitest';
import { getThreadPostCountsByAuthor } from '../author-post-counts';
describe('getThreadPostCountsByAuthor', () => {
it('counts the OP and replies per author short address', () => {
const post = { cid: 'post-1', author: { shortAddress: 'author-a' } } as any;
const replies = [
{ cid: 'reply-1', author: { shortAddress: 'author-b' } },
{ cid: 'reply-2', author: { shortAddress: 'author-a' } },
{ cid: 'reply-3', author: { shortAddress: 'author-a' } },
] as any[];
const counts = getThreadPostCountsByAuthor(post, replies);
expect(counts.get('author-a')).toBe(3);
expect(counts.get('author-b')).toBe(1);
});
it('deduplicates repeated CIDs so preview copies do not inflate the count', () => {
const post = { cid: 'post-1', author: { shortAddress: 'author-a' } } as any;
const duplicateReply = { cid: 'reply-1', author: { shortAddress: 'author-b' } } as any;
const replies = [duplicateReply, duplicateReply, { cid: 'reply-2', author: { shortAddress: 'author-b' } }] as any[];
const counts = getThreadPostCountsByAuthor(post, replies);
expect(counts.get('author-a')).toBe(1);
expect(counts.get('author-b')).toBe(2);
});
it('skips comments missing a cid or short address', () => {
const counts = getThreadPostCountsByAuthor(
{ cid: 'post-1', author: { shortAddress: 'author-a' } } as any,
[{ cid: 'reply-1' }, { author: { shortAddress: 'author-b' } }] as any[],
);
expect(counts.get('author-a')).toBe(1);
expect(counts.has('author-b')).toBe(false);
});
});
+17
View File
@@ -0,0 +1,17 @@
import { Comment } from '@bitsocialnet/bitsocial-react-hooks';
export function getThreadPostCountsByAuthor(post: Comment | undefined, replies: Comment[] = []): Map<string, number> {
const counts = new Map<string, number>();
const seenCids = new Set<string>();
for (const comment of [post, ...replies]) {
const cid = comment?.cid;
const shortAddress = comment?.author?.shortAddress;
if (!cid || !shortAddress || seenCids.has(cid)) continue;
seenCids.add(cid);
counts.set(shortAddress, (counts.get(shortAddress) ?? 0) + 1);
}
return counts;
}