Skip to content

Commit 9f69463

Browse files
fix(app): backport session rename and tab menu fixes to v1 (anomalyco#46116)
Co-authored-by: Brendonovich <[email protected]>
1 parent 10765ff commit 9f69463

4 files changed

Lines changed: 196 additions & 22 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { expect, test } from "@playwright/test"
2+
import { fixture, pageMessages } from "../smoke/session-timeline.fixture"
3+
import { mockOpenCodeServer } from "../utils/mock-server"
4+
5+
test.beforeEach(async ({ page }) => {
6+
const sessions = fixture.sessions.map((session) => ({ ...session }))
7+
await mockOpenCodeServer(page, {
8+
protocol: "v1",
9+
sessions,
10+
provider: fixture.provider,
11+
directory: fixture.directory,
12+
project: fixture.project,
13+
pageMessages,
14+
})
15+
await page.route(/\/session\/[^/]+(?:\?.*)?$/, async (route) => {
16+
if (route.request().method() !== "PATCH") return route.fallback()
17+
const id = new URL(route.request().url()).pathname.split("/").at(-1)
18+
const session = sessions.find((item) => item.id === id)
19+
const payload: unknown = route.request().postDataJSON()
20+
if (
21+
!session ||
22+
!payload ||
23+
typeof payload !== "object" ||
24+
!("title" in payload) ||
25+
typeof payload.title !== "string"
26+
)
27+
throw new Error("Invalid rename request")
28+
session.title = payload.title
29+
await route.fulfill({ json: session, headers: { "access-control-allow-origin": "*" } })
30+
})
31+
await page.addInitScript((directory) => {
32+
localStorage.setItem(
33+
"opencode.global.dat:server",
34+
JSON.stringify({
35+
projects: { local: [{ worktree: directory, expanded: true }] },
36+
lastProject: { local: directory },
37+
}),
38+
)
39+
}, fixture.directory)
40+
await page.goto("/")
41+
await page.locator('[data-component="home-session-row"]').filter({ hasText: fixture.expected.targetTitle }).click()
42+
await expect(page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true })).toBeVisible()
43+
})
44+
45+
for (const commit of ["Enter", "blur", "click outside"]) {
46+
test(`saves the session heading on ${commit}`, async ({ page }) => {
47+
await page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true }).click()
48+
const input = page.locator('input[data-slot="session-title-child"]')
49+
await expect(input).toBeFocused()
50+
await input.fill("Renamed session")
51+
if (commit === "Enter") await input.press("Enter")
52+
if (commit === "blur") await input.press("Tab")
53+
if (commit === "click outside") await page.getByRole("textbox", { name: "Prompt", exact: true }).click()
54+
await expect(page.getByRole("heading", { name: "Renamed session", exact: true })).toBeVisible()
55+
await expect(page.locator('[data-slot="titlebar-tabs"] a').filter({ hasText: "Renamed session" })).toBeVisible()
56+
await page.reload()
57+
await expect(page.getByRole("heading", { name: "Renamed session", exact: true })).toBeVisible()
58+
})
59+
}
60+
61+
test("cancels the session heading with Escape", async ({ page }) => {
62+
await page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true }).click()
63+
const input = page.locator('input[data-slot="session-title-child"]')
64+
await input.fill("Discard this title")
65+
await input.press("Escape")
66+
await expect(page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true })).toBeVisible()
67+
await page.reload()
68+
await expect(page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true })).toBeVisible()
69+
})
70+
71+
test("keeps the draft when saving the session heading fails", async ({ page }) => {
72+
await page.route(/\/session\/[^/]+(?:\?.*)?$/, (route) => {
73+
if (route.request().method() !== "PATCH") return route.fallback()
74+
return route.fulfill({ status: 500, headers: { "access-control-allow-origin": "*" } })
75+
})
76+
await page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true }).click()
77+
const input = page.locator('input[data-slot="session-title-child"]')
78+
await input.fill("Retry this title")
79+
await input.press("Tab")
80+
await expect(page.getByText("Request failed", { exact: true })).toBeVisible()
81+
await expect(input).toBeEnabled()
82+
await expect(input).toHaveValue("Retry this title")
83+
await expect(
84+
page.locator('[data-slot="titlebar-tabs"] a').filter({ hasText: fixture.expected.targetTitle }),
85+
).toBeVisible()
86+
})
87+
88+
test("does not save an empty session heading", async ({ page }) => {
89+
await page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true }).click()
90+
const input = page.locator('input[data-slot="session-title-child"]')
91+
await input.fill(" ")
92+
await input.press("Tab")
93+
await expect(page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true })).toBeVisible()
94+
await page.reload()
95+
await expect(page.getByRole("heading", { name: fixture.expected.targetTitle, exact: true })).toBeVisible()
96+
})
97+
98+
test("renames and closes the session tab from its context menu", async ({ page }) => {
99+
const tab = page.locator('[data-slot="titlebar-tabs"] a').filter({ hasText: fixture.expected.targetTitle })
100+
await tab.click({ button: "right" })
101+
await expect(page.getByRole("menuitem", { name: "Rename", exact: true })).toBeVisible()
102+
await page.keyboard.press("Escape")
103+
await expect(page.getByRole("menuitem", { name: "Rename", exact: true })).toBeHidden()
104+
await expect(tab).toBeFocused()
105+
await tab.press("Shift+F10")
106+
await page.getByRole("menuitem", { name: "Rename", exact: true }).click()
107+
const input = page.locator('[data-slot="tab-title"][contenteditable="true"]')
108+
await expect(input).toBeFocused()
109+
await input.fill("Renamed from tab")
110+
await input.press("Enter")
111+
await expect(page.getByRole("heading", { name: "Renamed from tab", exact: true })).toBeVisible()
112+
await page.reload()
113+
await expect(page.getByRole("heading", { name: "Renamed from tab", exact: true })).toBeVisible()
114+
const renamed = page.locator('[data-slot="titlebar-tabs"] a').filter({ hasText: "Renamed from tab" })
115+
await renamed.click({ button: "right" })
116+
await page.getByRole("menuitem", { name: "Close tab", exact: true }).click()
117+
await expect(renamed).toBeHidden()
118+
await page.getByRole("button", { name: "Home", exact: true }).click()
119+
await expect(
120+
page.locator('[data-component="home-session-row"]').filter({ hasText: "Renamed from tab" }),
121+
).toBeVisible()
122+
})
123+
124+
test("renames an inactive tab without switching sessions", async ({ page }) => {
125+
await page.getByRole("button", { name: "Home", exact: true }).click()
126+
await page.locator('[data-component="home-session-row"]').filter({ hasText: fixture.expected.sourceTitle }).click()
127+
await expect(page.getByRole("heading", { name: fixture.expected.sourceTitle, exact: true })).toBeVisible()
128+
const tab = page.locator('[data-slot="titlebar-tabs"] a').filter({ hasText: fixture.expected.targetTitle })
129+
await tab.click({ button: "right" })
130+
await page.getByRole("menuitem", { name: "Rename", exact: true }).click()
131+
const input = page.locator('[data-slot="tab-title"][contenteditable="true"]')
132+
await expect(input).toBeFocused()
133+
await input.fill("Inactive tab renamed")
134+
await input.press("Tab")
135+
await expect(page.getByRole("heading", { name: fixture.expected.sourceTitle, exact: true })).toBeVisible()
136+
await expect(page).toHaveURL(new RegExp(`/session/${fixture.sourceID}$`))
137+
await page.locator('[data-slot="titlebar-tabs"] a').filter({ hasText: "Inactive tab renamed" }).click()
138+
await expect(page.getByRole("heading", { name: "Inactive tab renamed", exact: true })).toBeVisible()
139+
await page.reload()
140+
await expect(page.getByRole("heading", { name: "Inactive tab renamed", exact: true })).toBeVisible()
141+
})

‎packages/app/e2e/regression/subagent-child-navigation.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test("shows the not found fallback when the viewed session is deleted", async ({
3939
})
4040

4141
await expect(page.getByText("This session cannot be found")).toBeVisible()
42-
await expect(page.getByRole("button", { name: "Close Tab" })).toBeVisible()
42+
await expect(page.getByRole("button", { name: "Close Tab", exact: true })).toBeVisible()
4343
await expect(page.getByRole("heading", { name: taskDescription })).toHaveCount(0)
4444
})
4545

‎packages/app/src/components/titlebar-tab-nav.tsx‎

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { createEffect, createMemo, createSignal, onCleanup, Show, type Ref } from "solid-js"
2+
import { createStore } from "solid-js/store"
23
import { makeEventListener } from "@solid-primitives/event-listener"
34
import { createResizeObserver } from "@solid-primitives/resize-observer"
45
import { createMutation } from "@tanstack/solid-query"
56
import { IconButtonV2 } from "@opencode-ai/ui/v2/icon-button-v2"
67
import { Icon as IconV2 } from "@opencode-ai/ui/v2/icon"
8+
import { MenuV2 } from "@opencode-ai/ui/v2/menu-v2"
79
import { useGlobal } from "@/context/global"
810
import { useLanguage } from "@/context/language"
911
import { ServerConnection, serverName } from "@/context/server"
@@ -33,6 +35,8 @@ export function TabNavItem(props: {
3335
pressed?: boolean
3436
hidden?: boolean
3537
}) {
38+
const language = useLanguage()
39+
const [menu, setMenu] = createStore({ open: false, rename: false })
3640
const [editing, setEditing] = createSignal(false)
3741
const [titleOverflowing, setTitleOverflowing] = createSignal(false)
3842
let tabRoot!: HTMLDivElement
@@ -76,7 +80,7 @@ export function TabNavItem(props: {
7680
})
7781

7882
const [popoverOpen, setPopoverOpen] = createSignal(false)
79-
const previewBlocked = () => !!props.dragging || editing() || !!props.pressed || !props.session()
83+
const previewBlocked = () => !!props.dragging || editing() || menu.open || !!props.pressed || !props.session()
8084

8185
const measureTitleOverflow = () => {
8286
if (!titleEl || editing()) {
@@ -138,9 +142,9 @@ export function TabNavItem(props: {
138142
titleEl.textContent = value
139143
})
140144

141-
const openRename = (event: MouseEvent) => {
142-
event.preventDefault()
143-
event.stopPropagation()
145+
const openRename = (event?: MouseEvent) => {
146+
event?.preventDefault()
147+
event?.stopPropagation()
144148
if (!canOpenTabRename(props.dragging, editing(), rename.isPending)) return
145149
const session = props.session()
146150
if (!session) return
@@ -171,7 +175,7 @@ export function TabNavItem(props: {
171175
onCleanup(cleanup)
172176
})
173177

174-
const tab = (
178+
const tab = () => (
175179
<div
176180
ref={(el) => {
177181
tabRoot = el
@@ -196,7 +200,11 @@ export function TabNavItem(props: {
196200
closeTab(event)
197201
}}
198202
>
199-
<a
203+
<MenuV2.Context.Trigger
204+
as="a"
205+
disabled={editing() || props.dragging}
206+
aria-haspopup="menu"
207+
aria-expanded={menu.open}
200208
data-slot="tab-link"
201209
data-titlebar-tab-link
202210
href={props.href}
@@ -276,7 +284,7 @@ export function TabNavItem(props: {
276284
event.preventDefault()
277285
}}
278286
/>
279-
</a>
287+
</MenuV2.Context.Trigger>
280288

281289
<div data-slot="tab-close">
282290
<IconButtonV2
@@ -289,26 +297,49 @@ export function TabNavItem(props: {
289297
}}
290298
onClick={closeTab}
291299
icon={<IconV2 name="xmark-small" />}
300+
aria-label={language.t("common.closeTab")}
292301
/>
293302
</div>
294303
</div>
295304
)
296305

297306
return (
298-
<TabPreviewPopover
299-
trigger={tab}
300-
open={popoverOpen() && !previewBlocked()}
301-
onOpenChange={(value) => {
302-
if (value && previewBlocked()) return
303-
setPopoverOpen(value)
304-
}}
305-
data={{
306-
projectName: projectName(),
307-
title: props.session()?.title,
308-
path: previewPath(),
309-
serverName: serverLabel(),
307+
<MenuV2.Context
308+
onOpenChange={(open) => {
309+
setMenu("open", open)
310+
if (open) setPopoverOpen(false)
310311
}}
311-
/>
312+
>
313+
<TabPreviewPopover
314+
trigger={tab()}
315+
open={popoverOpen() && !previewBlocked()}
316+
onOpenChange={(value) => {
317+
if (value && previewBlocked()) return
318+
setPopoverOpen(value)
319+
}}
320+
data={{
321+
projectName: projectName(),
322+
title: props.session()?.title,
323+
path: previewPath(),
324+
serverName: serverLabel(),
325+
}}
326+
/>
327+
<MenuV2.Context.Portal>
328+
<MenuV2.Context.Content
329+
onCloseAutoFocus={(event) => {
330+
if (!menu.rename) return
331+
event.preventDefault()
332+
setMenu("rename", false)
333+
openRename()
334+
}}
335+
>
336+
<MenuV2.Item disabled={!props.session() || rename.isPending} onSelect={() => setMenu("rename", true)}>
337+
{language.t("common.rename")}
338+
</MenuV2.Item>
339+
<MenuV2.Item onSelect={props.onClose}>{language.t("common.closeTab")}</MenuV2.Item>
340+
</MenuV2.Context.Content>
341+
</MenuV2.Context.Portal>
342+
</MenuV2.Context>
312343
)
313344
}
314345

‎packages/app/src/pages/session/timeline/message-timeline.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ export function MessageTimeline(props: {
777777
}
778778

779779
const saveTitleEditor = () => {
780+
if (!title.editing) return
780781
const id = sessionID()
781782
if (!id) return
782783
if (titleMutation.isPending) return
@@ -1447,6 +1448,7 @@ export function MessageTimeline(props: {
14471448
onInput={(event) => setTitle("draft", event.currentTarget.value)}
14481449
onKeyDown={(event) => {
14491450
event.stopPropagation()
1451+
if (event.isComposing || event.keyCode === 229) return
14501452
if (event.key === "Enter") {
14511453
event.preventDefault()
14521454
void saveTitleEditor()
@@ -1457,7 +1459,7 @@ export function MessageTimeline(props: {
14571459
closeTitleEditor()
14581460
}
14591461
}}
1460-
onBlur={closeTitleEditor}
1462+
onBlur={saveTitleEditor}
14611463
/>
14621464
</Show>
14631465
</Show>

0 commit comments

Comments
 (0)