Skip to content

Commit 83c56e0

Browse files
committed
refactor(ui): avoid redundant presence work in avatar lists
1 parent b7ebff6 commit 83c56e0

6 files changed

Lines changed: 49 additions & 71 deletions

‎ui/src/components/app-sidebar-session-row-render.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ export function renderRecentSession(params: {
229229
leadingOwner,
230230
ownerAttribution,
231231
ownerViewing,
232-
session.participants,
233-
session.participantCount,
234232
channelAvatarAuth,
235233
);
236234
const trailingDescription = session.isChild

‎ui/src/components/session-leading-indicator.ts‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { html, nothing, type TemplateResult } from "lit";
2-
import type {
3-
SessionParticipant,
4-
SessionParticipantIdentity,
5-
} from "../../../packages/gateway-protocol/src/schema/session-participant.js";
2+
import type { SessionParticipantIdentity } from "../../../packages/gateway-protocol/src/schema/session-participant.js";
63
import { t } from "../i18n/index.ts";
74
import type { SidebarRecentSession } from "./app-sidebar-session-types.ts";
85
import {
@@ -53,15 +50,14 @@ export function renderSessionLeadingState(
5350
ownerActor: SessionCreatedActor | null | undefined,
5451
attribution: "created" | "owned" | "archived",
5552
ownerViewing?: boolean,
56-
participants?: readonly SessionParticipant[],
57-
participantCount?: number,
5853
avatarAuth?: SessionAvatarAuth,
5954
): {
6055
running: boolean;
6156
leadingIndicator: TemplateResult | typeof nothing;
6257
trailingIndicator: TemplateResult | typeof nothing;
6358
renderedIdentities?: readonly SessionParticipantIdentity[];
6459
} {
60+
const { participants, participantCount } = session;
6561
const running = sessionHasRunningWork(session);
6662
const trailingIndicator = session.isChild ? nothing : renderSessionState(session, false);
6763
// Transient attention always outranks the persistent decorative icon.
@@ -132,17 +128,16 @@ export function renderSessionLeadingState(
132128
trailingIndicator,
133129
};
134130
}
135-
const ownerChip =
136-
!session.isChild && ownerActor?.id?.trim()
137-
? renderSessionOwnerChip(
138-
ownerActor,
139-
"row",
140-
attribution,
141-
ownerViewing,
142-
participants,
143-
participantCount,
144-
)
145-
: undefined;
131+
const ownerChip = ownerActor?.id?.trim()
132+
? renderSessionOwnerChip(
133+
ownerActor,
134+
"row",
135+
attribution,
136+
ownerViewing,
137+
participants,
138+
participantCount,
139+
)
140+
: undefined;
146141
if (session.channelAvatarUrl) {
147142
ensureChannelAvatarElement();
148143
return {

‎ui/src/components/session-owner-chip.test.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,15 @@
33
import { afterEach, expect, it, vi } from "vitest";
44
import type { SessionParticipant } from "../../../packages/gateway-protocol/src/schema/session-participant.js";
55
import { setAvatarGatewayOrigin } from "../lib/identity-avatar-context.ts";
6-
import { listAssignableSessionOwners, type SessionCreatedActor } from "./session-owner-chip.ts";
7-
8-
type OwnerChipElement = HTMLElement & {
9-
owner: SessionCreatedActor | null;
10-
participants: readonly SessionParticipant[];
11-
participantCount: number;
12-
attribution: "created" | "owned" | "archived";
13-
size: "row" | "header";
14-
updateComplete: Promise<boolean>;
15-
};
6+
import { listAssignableSessionOwners } from "./session-owner-chip.ts";
167

178
afterEach(() => {
189
document.body.replaceChildren();
1910
setAvatarGatewayOrigin(null);
2011
vi.restoreAllMocks();
2112
});
2213

23-
async function waitForChipUpdate(chip: OwnerChipElement) {
14+
async function waitForChipUpdate(chip: HTMLElementTagNameMap["openclaw-session-owner-chip"]) {
2415
await chip.updateComplete;
2516
// The parent's update does not include the nested avatar's render.
2617
await Promise.all(
@@ -29,8 +20,7 @@ async function waitForChipUpdate(chip: OwnerChipElement) {
2920
}
3021

3122
async function mount(params: { participants?: SessionParticipant[]; participantCount?: number }) {
32-
// SAFETY: the imported module registers this custom element with these reactive properties.
33-
const chip = document.createElement("openclaw-session-owner-chip") as OwnerChipElement;
23+
const chip = document.createElement("openclaw-session-owner-chip");
3424
chip.owner = { type: "human", id: "profile-ada", label: "Ada" };
3525
chip.attribution = "owned";
3626
chip.size = "row";

‎ui/src/components/session-owner-chip.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,7 @@ class SessionOwnerChip extends OpenClawLightDomElement {
175175
return html`<span class="session-owner-stack" role="group" aria-label=${combinedLabel}>
176176
<span class="session-owner-stack__back" aria-hidden="true">
177177
${this.participantCount === 1 && participant
178-
? html`<openclaw-viewer-avatar
179-
.user=${{
180-
id: participant.identity.id,
181-
name: participant.label,
182-
avatarUrl: participant.avatarUrl,
183-
watchedSessions: [],
184-
}}
185-
.markAsViewer=${false}
186-
.identity=${participant.identity}
187-
variant="session"
188-
></openclaw-viewer-avatar>`
178+
? renderSessionOwnerAvatar({ ...participant, id: participant.identity.id })
189179
: html`<span class="session-owner-stack__overflow">+${this.participantCount}</span>`}
190180
</span>
191181
${chip}

‎ui/src/components/viewer-facepile.test.ts‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@ import {
88
hasMultiplePresenceIdentities,
99
hasSessionPresenceViewers,
1010
projectOnlinePresenceViewers,
11+
projectPresencePayload,
1112
type PresenceViewer,
1213
} from "../lib/presence-users.ts";
1314
import { renderChatAuthorAvatar } from "../pages/chat/components/chat-author-avatar.ts";
1415
import "./viewer-facepile.ts";
1516

16-
type ViewerAvatarElement = HTMLElement & {
17-
user: PresenceViewer | null;
18-
updateComplete: Promise<boolean>;
19-
};
20-
2117
afterEach(() => {
2218
document.body.replaceChildren();
2319
setAvatarGatewayOrigin(null);
@@ -31,7 +27,7 @@ it("uses the same user initials and identity hue in the roster and attributed ch
3127
3228
watchedSessions: [],
3329
};
34-
const viewerAvatar = document.createElement("openclaw-viewer-avatar") as ViewerAvatarElement;
30+
const viewerAvatar = document.createElement("openclaw-viewer-avatar");
3531
viewerAvatar.user = user;
3632
document.body.append(viewerAvatar);
3733

@@ -60,7 +56,7 @@ it("uses the same user initials and identity hue in the roster and attributed ch
6056
});
6157

6258
it("uses the shared resolver and rejects cross-origin presence avatar metadata", async () => {
63-
const avatar = document.createElement("openclaw-viewer-avatar") as ViewerAvatarElement;
59+
const avatar = document.createElement("openclaw-viewer-avatar");
6460
avatar.user = {
6561
id: "profile-mallory",
6662
name: "Mallory",
@@ -77,7 +73,7 @@ it("uses the shared resolver and rejects cross-origin presence avatar metadata",
7773
});
7874

7975
it("renders trusted presence avatar routes directly", async () => {
80-
const avatar = document.createElement("openclaw-viewer-avatar") as ViewerAvatarElement;
76+
const avatar = document.createElement("openclaw-viewer-avatar");
8177
avatar.user = {
8278
id: "profile-ada",
8379
name: "Ada Lovelace",
@@ -185,7 +181,7 @@ it("shares an authenticated avatar blob between the same user in the roster and
185181
watchedSessions: [],
186182
};
187183
const avatars = Array.from({ length: 2 }, () => {
188-
const avatar = document.createElement("openclaw-viewer-avatar") as ViewerAvatarElement;
184+
const avatar = document.createElement("openclaw-viewer-avatar");
189185
avatar.user = user;
190186
document.body.append(avatar);
191187
return avatar;
@@ -210,12 +206,25 @@ it("shares an authenticated avatar blob between the same user in the roster and
210206
}
211207
});
212208

213-
type ViewerFacepileElement = HTMLElementTagNameMap["openclaw-viewer-facepile"];
209+
it.each(["staticParticipants", "staticUsers"] as const)(
210+
"renders an empty %s list without invalidating cached live presence",
211+
async (source) => {
212+
const payload = { presence: [{ user: { id: "live-viewer" } }] };
213+
const projection = projectPresencePayload(payload);
214+
const facepile = document.createElement("openclaw-viewer-facepile");
215+
facepile[source] = [];
216+
document.body.append(facepile);
217+
await facepile.updateComplete;
218+
219+
expect(facepile.querySelector(".viewer-facepile")).toBeNull();
220+
expect(projectPresencePayload(payload)).toBe(projection);
221+
},
222+
);
214223

215224
it.each(["first", "second"])(
216225
"merges device presence into one non-interactive face watching %s",
217226
async (session) => {
218-
const facepile = document.createElement("openclaw-viewer-facepile") as ViewerFacepileElement;
227+
const facepile = document.createElement("openclaw-viewer-facepile");
219228
facepile.sessionKey = `agent:main:${session}`;
220229
facepile.presencePayload = {
221230
presence: [
@@ -243,8 +252,7 @@ it.each(["first", "second"])(
243252
);
244253

245254
it("renders ordered static participant actors without presence filtering", async () => {
246-
// SAFETY: the registered custom element exposes the tested reactive properties.
247-
const facepile = document.createElement("openclaw-viewer-facepile") as ViewerFacepileElement;
255+
const facepile = document.createElement("openclaw-viewer-facepile");
248256
facepile.maxVisible = 2;
249257
facepile.staticParticipants = [
250258
{ identity: { type: "profile", id: "profile-ada" }, label: "Ada" },
@@ -265,7 +273,7 @@ it("renders ordered static participant actors without presence filtering", async
265273
});
266274

267275
it("excludes displayed owners and participants before choosing visible avatars and overflow", async () => {
268-
const facepile = document.createElement("openclaw-viewer-facepile") as ViewerFacepileElement;
276+
const facepile = document.createElement("openclaw-viewer-facepile");
269277
facepile.sessionKey = "agent:main:active";
270278
facepile.excludeIdentities = [
271279
{ type: "profile", id: "owner" },
@@ -352,7 +360,7 @@ it.each([
352360
],
353361
},
354362
])("excludes authenticated self from session facepiles when $name", async (fixture) => {
355-
const facepile = document.createElement("openclaw-viewer-facepile") as ViewerFacepileElement;
363+
const facepile = document.createElement("openclaw-viewer-facepile");
356364
facepile.selfUser = { id: "self" };
357365
facepile.selfInstanceId = fixture.selfInstanceId;
358366
facepile.sessionKey = "agent:main:active";
@@ -395,11 +403,7 @@ it("links faces only when the host opts in, so nested facepiles stay plain", asy
395403
{ identity: { type: "profile", id: "profile-mira" }, label: "Mira" },
396404
];
397405
const mount = async (personActivity?: { basePath: string; navigate: (id: string) => void }) => {
398-
const facepile = document.createElement("openclaw-viewer-facepile") as HTMLElement & {
399-
staticParticipants: readonly SessionParticipant[];
400-
personActivity?: { basePath: string; navigate: (id: string) => void };
401-
updateComplete: Promise<boolean>;
402-
};
406+
const facepile = document.createElement("openclaw-viewer-facepile");
403407
facepile.staticParticipants = users;
404408
if (personActivity) {
405409
facepile.personActivity = personActivity;

‎ui/src/components/viewer-facepile.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@ class ViewerFacepile extends OpenClawLightDomContentsElement {
8888
@property({ attribute: false }) personActivity?: PersonActivityRouting;
8989

9090
override render() {
91-
const viewers = projectPresenceViewers(
92-
this.presencePayload,
93-
this.selfUser,
94-
this.selfInstanceId,
95-
this.sessionKey,
96-
this.excludeIdentities,
97-
);
91+
// Prepared faces must not evict the cached live projection used by sibling rows.
9892
const users = this.staticParticipants
9993
? this.staticParticipants.map(({ identity, label, avatarUrl }) => ({
10094
identity,
@@ -103,7 +97,14 @@ class ViewerFacepile extends OpenClawLightDomContentsElement {
10397
avatarUrl,
10498
watchedSessions: [],
10599
}))
106-
: (this.staticUsers ?? viewers);
100+
: (this.staticUsers ??
101+
projectPresenceViewers(
102+
this.presencePayload,
103+
this.selfUser,
104+
this.selfInstanceId,
105+
this.sessionKey,
106+
this.excludeIdentities,
107+
));
107108
if (users.length === 0) {
108109
return nothing;
109110
}

0 commit comments

Comments
 (0)