Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(chat): read reaction avatars only for badges near the viewport

Dash32b821ea570d
diff --git a/apps/desktop/src/renderer/features/bots/ChatReactionButton.tsx b/apps/desktop/src/renderer/features/bots/ChatReactionButton.tsx
index cc88b331b8..a7007ef617 100644
--- a/apps/desktop/src/renderer/features/bots/ChatReactionButton.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatReactionButton.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useState } from 'react';
+import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import type { TFunction } from 'i18next';
import { Button } from '@/components/ui/button';
@@ -43,9 +43,20 @@ export function ChatReactionButton({ groupId, messageId, reaction, members, disa
// The authoritative summary changes on every add/remove, local or remote.
const summary = `${reaction.count}:${reaction.me}`;
const wantsAvatars = !!groupId && reaction.count > 0 && reaction.count <= REACTION_AVATAR_LIMIT;
- // Read whenever the summary changes; a superseded or cross-account read never lands.
+ // Only badges near the viewport read, so opening a long history does not fire one read per badge.
+ const badgeRef = useRef<HTMLButtonElement>(null);
+ const [nearViewport, setNearViewport] = useState(() => typeof IntersectionObserver === 'undefined');
useEffect(() => {
- if (!wantsAvatars || !groupId) return;
+ const el = badgeRef.current;
+ if (!wantsAvatars || !el || typeof IntersectionObserver === 'undefined') return;
+ const observer = new IntersectionObserver(entries => setNearViewport(entries.some(e => e.isIntersecting)), { rootMargin: '200px 0px' });
+ observer.observe(el);
+ return () => observer.disconnect();
+ }, [wantsAvatars]);
+ const loadedSummary = loaded?.summary;
+ // Read when a visible badge's summary changes; a superseded or cross-account read never lands.
+ useEffect(() => {
+ if (!wantsAvatars || !groupId || !nearViewport || loadedSummary === summary) return;
let superseded = false;
const owner = getDataOwnerGeneration();
const settle = (reactors: ChatReactor[] | null) => {
@@ -54,7 +65,7 @@ export function ChatReactionButton({ groupId, messageId, reaction, members, disa
window.electronAPI.maker.chatServer.reactors({ groupId, messageId, emoji: reaction.emoji })
.then(result => settle(result.ok ? result.reactors : null), () => settle(null));
return () => { superseded = true; };
- }, [wantsAvatars, groupId, messageId, reaction.emoji, summary]);
+ }, [wantsAvatars, groupId, nearViewport, loadedSummary, messageId, reaction.emoji, summary]);
// Reactors read for an older summary are never shown for the current one. A failed read, or a list
// that disagrees with the count (a change still in flight), keeps the honest count instead.
const current = loaded?.summary === summary ? loaded : null;
@@ -65,7 +76,7 @@ export function ChatReactionButton({ groupId, messageId, reaction, members, disa
? t(key('reactionWho'), { emoji: reaction.emoji, count: reaction.count, names: reactorNames(reactors, t, i18n.language) })
: countLabel;
return (
- <Button variant={reaction.me ? 'primary' : 'secondary'} size="sm" compact
+ <Button ref={badgeRef} variant={reaction.me ? 'primary' : 'secondary'} size="sm" compact
disabled={disabled} aria-pressed={reaction.me} aria-label={label}
className={showAvatars ? 'pr-1' : undefined} onClick={onToggle}>
<span>{reaction.emoji}</span>
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/ChatReactionButton.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/ChatReactionButton.test.tsx
index bf074853db..6fc5d04d76 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/ChatReactionButton.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/ChatReactionButton.test.tsx
@@ -72,6 +72,25 @@ describe('reaction avatars', () => {
expect(mocks.react).not.toHaveBeenCalled();
});
+ it('reads only once a badge is near the viewport, and not again for an unchanged reaction', async () => {
+ const observers: Array<(entries: Array<{ isIntersecting: boolean }>) => void> = [];
+ vi.stubGlobal('IntersectionObserver', class {
+ constructor(callback: (entries: Array<{ isIntersecting: boolean }>) => void) { observers.push(callback); }
+ observe() {} disconnect() {}
+ });
+ mocks.reactors.mockResolvedValue({ ok: true, reactors: [reactor('z', 'Zoe')] });
+ render(actions([{ emoji: '👍', count: 1, me: false }]));
+ // Off-screen history badges keep their slots without reading.
+ expect(avatars()?.children).toHaveLength(1);
+ expect(mocks.reactors).not.toHaveBeenCalled();
+ act(() => observers.forEach(notify => notify([{ isIntersecting: true }])));
+ await waitFor(() => expect(badge().getAttribute('aria-label')).toBe('👍 · 1: Zoe'));
+ // Scrolling away and back does not re-read the same reaction state.
+ act(() => observers.forEach(notify => notify([{ isIntersecting: false }])));
+ act(() => observers.forEach(notify => notify([{ isIntersecting: true }])));
+ expect(mocks.reactors).toHaveBeenCalledTimes(1);
+ });
+
it('keeps the count above the avatar limit and reads nothing', () => {
render(actions([{ emoji: '👍', count: 4, me: false }]));
expect(badge().textContent).toBe('👍4');
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
index 2c46ff1dde..448cb5f694 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
@@ -153,7 +153,8 @@ describe('chat interaction controls', () => {
expect(reaction.getAttribute('aria-pressed')).toBe('true');
fireEvent.click(reaction);
await waitFor(() => expect(screen.getByRole('button', { name: k('reactionCount') }).getAttribute('aria-pressed')).toBe('false'));
- expect(screen.getByRole('button', { name: k('reactionCount') }).textContent).toContain('1');
+ // One reactor: the badge settles on its count once the reactor read disagrees (mock returns none).
+ await waitFor(() => expect(screen.getByRole('button', { name: k('reactionCount') }).textContent).toContain('1'));
expect(group.messages[0].reactions?.[0].me).toBe(true);
});
diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.md
index 50516533b3..876f428f0d 100644
--- a/docs/product-rules/bot-group-chat.md
+++ b/docs/product-rules/bot-group-chat.md
@@ -360,8 +360,9 @@ Chris 已明确扩展到多人各带伙伴,并选择「主人按群预先授
Desktop 主群与 Thread 的表情回应徽标参考 Telegram:某个表情的回应者不超过 3 人时,徽标直接显示他们的头像
(替代数字,不需要悬停);超过 3 人时显示数字。头像按 Actor ID 从群成员投影取,同名伙伴各用自己的头像;
-已离群成员或没有成员记录的回应者只显示名字首字母或「?」,不借用别人的头像、不编造名字。徽标的读屏名称
-列出全部回应者(自己为「你」,离群成员标注不在群中,无记录的只计数)。名单按 Actor ID 去重,由主进程读取
+已离群成员或没有成员记录的回应者只显示名字首字母或「?」,不借用别人的头像、不编造名字。显示头像时,徽标的读屏
+名称列出这几位回应者(自己为「你」,离群成员标注不在群中,无记录的只计数);超过 3 人时与视觉一致只读出人数,
+不额外读取名单。只有接近可视区域的徽标才读取回应者,打开长历史不会逐个徽标发请求。名单按 Actor ID 去重,由主进程读取
Chat Server 既有的回应者接口直到最后一页(离群成员保留回应,总数可超过群人数上限)并按群成员投影命名;
读取只读,不触发伙伴资料同步等任何写入。加载期间按人数预留头像位,不撑动消息;读取失败或名单与人数暂不一致时
如实显示数字。回应数或自己的状态变化(本地或远端)时重新读取。点击仍是添加/取消回应。回归见 `ChatReactionButton.test.tsx`