Skip to content

Commit 94f6ceb

Browse files
authored
fix(ui): keep branch and editor tooltips beside their buttons (#133556)
* fix(ui): anchor branch and editor tooltips beside their buttons * test(ui): wait for app mount before checking route readiness
1 parent 6af76da commit 94f6ceb

5 files changed

Lines changed: 255 additions & 112 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import type WaTooltip from "@awesome.me/webawesome/dist/components/tooltip/tooltip.js";
2+
import { render } from "lit";
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4+
import { installTitleTooltips } from "../../../components/tooltip-title.ts";
5+
import { i18n } from "../../../i18n/index.ts";
6+
import "../../../styles.css";
7+
import "../../../styles/chat/split-view.css";
8+
import { mountChatPaneHeader } from "./chat-pane-header.test-support.ts";
9+
import { renderChatSidebarEditorMenu } from "./chat-sidebar-editor-menu.ts";
10+
11+
describe.skipIf(typeof HTMLElement.prototype.checkVisibility !== "function")(
12+
"chat pane branch tooltip positioning",
13+
() => {
14+
const containers: HTMLElement[] = [];
15+
let dispose: () => void;
16+
let page: (typeof import("vitest/browser"))["page"];
17+
18+
beforeEach(async () => {
19+
({ page } = await import("vitest/browser"));
20+
await page.viewport(1200, 800);
21+
await i18n.setLocale("en");
22+
dispose = installTitleTooltips(document);
23+
});
24+
25+
afterEach(() => {
26+
dispose();
27+
containers.splice(0).forEach((container) => container.remove());
28+
});
29+
30+
it.each(["idle", "busy", "editor"] as const)(
31+
"anchors the %s hint to its menu button",
32+
async (state) => {
33+
const busy = state === "busy";
34+
const editor = state === "editor";
35+
const onOpenEditor = vi.fn();
36+
const reason = "Branch switch is unavailable while the agent is working.";
37+
const { container, props } = mountChatPaneHeader(containers, {
38+
branchSwitchDisabledReason: busy ? reason : null,
39+
branches: [
40+
{ leafEntryId: "active", headline: "Current work", messageCount: 4, active: true },
41+
{ leafEntryId: "other", headline: "Earlier idea", messageCount: 2, active: false },
42+
],
43+
});
44+
if (editor) {
45+
render(
46+
renderChatSidebarEditorMenu({
47+
absolutePath: "/repo/example.ts",
48+
open: false,
49+
onOpenChange: () => undefined,
50+
onOpenEditor,
51+
}),
52+
container,
53+
);
54+
}
55+
container.style.cssText = "position: fixed; top: 80px; left: 500px; width: 650px";
56+
const trigger = container.querySelector<HTMLButtonElement>(
57+
editor ? ".sidebar-file-view__action" : ".chat-pane__branches-trigger",
58+
)!;
59+
await page.elementLocator(trigger).hover();
60+
const tooltip = () =>
61+
[...document.querySelectorAll("openclaw-tooltip")]
62+
.map((element) => element.shadowRoot?.querySelector<WaTooltip>("wa-tooltip"))
63+
.find((element) => element?.open);
64+
await expect
65+
.poll(() => tooltip()?.textContent)
66+
.toContain(editor ? "Open in editor" : busy ? reason : "Session branches");
67+
await expect
68+
.poll(() => {
69+
const body = tooltip()?.shadowRoot?.querySelector<HTMLElement>('[part="body"]');
70+
if (!body) {
71+
return false;
72+
}
73+
const hint = body.getBoundingClientRect();
74+
const button = trigger.getBoundingClientRect();
75+
return (
76+
hint.width > 0 &&
77+
hint.left < button.right &&
78+
hint.right > button.left &&
79+
Math.min(Math.abs(hint.bottom - button.top), Math.abs(hint.top - button.bottom)) < 24
80+
);
81+
})
82+
.toBe(true);
83+
84+
if (!busy) {
85+
await page.elementLocator(trigger).click();
86+
await expect.poll(() => tooltip()).toBeUndefined();
87+
await page.getByRole("menuitem", { name: editor ? "VS Code" : /Earlier idea/ }).click();
88+
expect(editor ? onOpenEditor : props.onBranchSelect).toHaveBeenCalledWith(
89+
editor ? "vscode" : "other",
90+
);
91+
}
92+
},
93+
);
94+
},
95+
);

‎ui/src/pages/chat/components/chat-pane-header.ts‎

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -488,70 +488,62 @@ export function renderChatPaneHeader(props: ChatPaneHeaderProps) {
488488
${props.sharingControl ?? nothing}
489489
${!props.catalog && props.branches.length > 1
490490
? html`
491-
<openclaw-tooltip
492-
.content=${props.branchSwitchDisabledReason ?? t("chat.sessionHeader.branches")}
491+
<wa-dropdown
492+
class="chat-pane__branches-menu"
493+
placement="bottom-end"
494+
@wa-select=${(event: CustomEvent<{ item: { value?: string } }>) => {
495+
const leafEntryId = event.detail.item.value;
496+
const branch = props.branches.find(
497+
(candidate) => candidate.leafEntryId === leafEntryId,
498+
);
499+
if (leafEntryId && branch && !branch.active && !props.branchSwitchDisabledReason) {
500+
props.onBranchSelect(leafEntryId);
501+
}
502+
}}
493503
>
494-
<wa-dropdown
495-
class="chat-pane__branches-menu"
496-
placement="bottom-end"
497-
@wa-select=${(event: CustomEvent<{ item: { value?: string } }>) => {
498-
const leafEntryId = event.detail.item.value;
499-
const branch = props.branches.find(
500-
(candidate) => candidate.leafEntryId === leafEntryId,
501-
);
502-
if (
503-
leafEntryId &&
504-
branch &&
505-
!branch.active &&
506-
!props.branchSwitchDisabledReason
507-
) {
508-
props.onBranchSelect(leafEntryId);
509-
}
510-
}}
504+
<button
505+
slot="trigger"
506+
class="btn btn--ghost btn--icon chat-icon-btn chat-pane__branches-trigger"
507+
type="button"
508+
?disabled=${Boolean(props.branchSwitchDisabledReason)}
509+
title=${props.branchSwitchDisabledReason ?? t("chat.sessionHeader.branches")}
510+
aria-label=${t("chat.sessionHeader.branches")}
511511
>
512-
<button
513-
slot="trigger"
514-
class="btn btn--ghost btn--icon chat-icon-btn chat-pane__branches-trigger"
515-
type="button"
516-
?disabled=${Boolean(props.branchSwitchDisabledReason)}
517-
aria-label=${t("chat.sessionHeader.branches")}
518-
>
519-
${icons.gitBranch}
520-
</button>
521-
${props.branches.map((branch) => {
522-
const relativeTime = branchRelativeTime(branch.updatedAt);
523-
return html`
524-
<wa-dropdown-item
525-
class="chat-pane__branch-item"
526-
value=${branch.leafEntryId}
527-
?disabled=${branch.active || Boolean(props.branchSwitchDisabledReason)}
528-
data-active=${branch.active ? "true" : "false"}
529-
>
530-
<span class="chat-pane__branch-copy">
531-
<span class="chat-pane__branch-headline"
532-
>${branch.headline || t("chat.sessionHeader.untitledBranch")}</span
533-
>
534-
<span class="chat-pane__branch-meta"
535-
>${t(
536-
branch.messageCount === 1
537-
? "chat.sessionHeader.oneMessage"
538-
: "chat.sessionHeader.messages",
539-
{ count: String(branch.messageCount) },
540-
)}${relativeTime ? ` · ${relativeTime}` : ""}</span
541-
>
542-
</span>
543-
${branch.active
544-
? html`<span
545-
class="chat-pane__branch-active"
546-
aria-label=${t("chat.sessionHeader.activeBranch")}
547-
>${icons.check}</span
548-
>`
549-
: nothing}
550-
</wa-dropdown-item>
551-
`;
552-
})}
553-
</wa-dropdown>
554-
</openclaw-tooltip>
512+
${icons.gitBranch}
513+
</button>
514+
${props.branches.map((branch) => {
515+
const relativeTime = branchRelativeTime(branch.updatedAt);
516+
return html`
517+
<wa-dropdown-item
518+
class="chat-pane__branch-item"
519+
value=${branch.leafEntryId}
520+
?disabled=${branch.active || Boolean(props.branchSwitchDisabledReason)}
521+
data-active=${branch.active ? "true" : "false"}
522+
>
523+
<span class="chat-pane__branch-copy">
524+
<span class="chat-pane__branch-headline"
525+
>${branch.headline || t("chat.sessionHeader.untitledBranch")}</span
526+
>
527+
<span class="chat-pane__branch-meta"
528+
>${t(
529+
branch.messageCount === 1
530+
? "chat.sessionHeader.oneMessage"
531+
: "chat.sessionHeader.messages",
532+
{ count: String(branch.messageCount) },
533+
)}${relativeTime ? ` · ${relativeTime}` : ""}</span
534+
>
535+
</span>
536+
${branch.active
537+
? html`<span
538+
class="chat-pane__branch-active"
539+
aria-label=${t("chat.sessionHeader.activeBranch")}
540+
>${icons.check}</span
541+
>`
542+
: nothing}
543+
</wa-dropdown-item>
544+
`;
545+
})}
546+
</wa-dropdown>
555547
`
556548
: nothing}
557549
${renderGatewayPicker(props)}
Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { html, nothing } from "lit";
22
import { icons } from "../../../components/icons.ts";
3-
import "../../../components/tooltip.ts";
43
import "../../../components/web-awesome.ts";
54
import { EDITOR_IDS, EDITOR_LABELS, type EditorId } from "../../../lib/editor-links.ts";
65

@@ -16,37 +15,36 @@ export function renderChatSidebarEditorMenu(params: {
1615
const label = "Open in editor";
1716
return html`
1817
<div class="sidebar-file-view__editor">
19-
<openclaw-tooltip .content=${label}>
20-
<wa-dropdown
21-
class="sidebar-file-view__editor-menu"
22-
placement="bottom-end"
23-
.open=${params.open}
24-
@wa-select=${(event: CustomEvent<{ item: { value?: string } }>) => {
25-
const editor = event.detail.item.value;
26-
if (editor && EDITOR_IDS.includes(editor as EditorId)) {
27-
params.onOpenEditor(editor as EditorId);
28-
}
29-
}}
30-
@wa-show=${() => params.onOpenChange(true)}
31-
@wa-hide=${() => params.onOpenChange(false)}
18+
<wa-dropdown
19+
class="sidebar-file-view__editor-menu"
20+
placement="bottom-end"
21+
.open=${params.open}
22+
@wa-select=${(event: CustomEvent<{ item: { value?: string } }>) => {
23+
const editor = event.detail.item.value;
24+
if (editor && EDITOR_IDS.includes(editor as EditorId)) {
25+
params.onOpenEditor(editor as EditorId);
26+
}
27+
}}
28+
@wa-show=${() => params.onOpenChange(true)}
29+
@wa-hide=${() => params.onOpenChange(false)}
30+
>
31+
<button
32+
slot="trigger"
33+
class="btn btn--sm sidebar-file-view__action"
34+
type="button"
35+
title=${label}
36+
aria-label=${label}
3237
>
33-
<button
34-
slot="trigger"
35-
class="btn btn--sm sidebar-file-view__action"
36-
type="button"
37-
aria-label=${label}
38-
>
39-
${icons.externalLink}
40-
</button>
41-
${EDITOR_IDS.map(
42-
(editor) => html`
43-
<wa-dropdown-item class="sidebar-file-view__editor-item" value=${editor}>
44-
${EDITOR_LABELS[editor]}
45-
</wa-dropdown-item>
46-
`,
47-
)}
48-
</wa-dropdown>
49-
</openclaw-tooltip>
38+
${icons.externalLink}
39+
</button>
40+
${EDITOR_IDS.map(
41+
(editor) => html`
42+
<wa-dropdown-item class="sidebar-file-view__editor-item" value=${editor}>
43+
${EDITOR_LABELS[editor]}
44+
</wa-dropdown-item>
45+
`,
46+
)}
47+
</wa-dropdown>
5048
</div>
5149
`;
5250
}

‎ui/src/test-helpers/control-ui-e2e.test.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Control UI tests cover control ui e2e behavior.
2-
import { describe, expect, it } from "vitest";
2+
import type { Page } from "playwright";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
34
import {
45
resolvePlaywrightChromiumExecutablePath,
56
systemChromiumExecutableCandidates,
7+
waitForControlUiRoute,
68
} from "./control-ui-e2e.ts";
79

810
describe("resolvePlaywrightChromiumExecutablePath", () => {
@@ -28,3 +30,54 @@ describe("resolvePlaywrightChromiumExecutablePath", () => {
2830
).toBe("/custom/chromium");
2931
});
3032
});
33+
34+
describe("waitForControlUiRoute", () => {
35+
afterEach(() => {
36+
document.body.replaceChildren();
37+
});
38+
39+
it("keeps polling while a new tab has no app element", async () => {
40+
// SAFETY: this fixture implements the Page methods used by the route helper.
41+
const page = {
42+
async waitForFunction(
43+
predicate: (target: { routeId: string }) => boolean,
44+
target: { routeId: string },
45+
) {
46+
expect(predicate(target)).toBe(false);
47+
const app = document.createElement("openclaw-app");
48+
Object.assign(app, {
49+
runtime: {
50+
router: {
51+
getState: () => ({
52+
status: "success",
53+
resolvedLocation: { pathname: window.location.pathname },
54+
matches: [{ routeId: "chat" }],
55+
pendingMatches: [],
56+
}),
57+
},
58+
},
59+
});
60+
document.body.append(app);
61+
expect(predicate(target)).toBe(true);
62+
return { dispose: vi.fn() };
63+
},
64+
evaluate: (read: () => unknown) => read(),
65+
} as unknown as Page;
66+
67+
await waitForControlUiRoute(page, { routeId: "chat" });
68+
});
69+
70+
it("preserves readiness failures when the app is still absent", async () => {
71+
const cause = new Error("Route readiness failed");
72+
// SAFETY: this fixture implements the Page methods used by the route helper.
73+
const page = {
74+
waitForFunction: vi.fn().mockRejectedValue(cause),
75+
evaluate: (read: () => unknown) => read(),
76+
} as unknown as Page;
77+
78+
await expect(waitForControlUiRoute(page, { routeId: "chat" })).rejects.toMatchObject({
79+
cause,
80+
message: expect.stringContaining('"router":null'),
81+
});
82+
});
83+
});

0 commit comments

Comments
 (0)