mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(tui): close a tab removed by another TUI to its neighbour (#64261)
This commit is contained in:
committed by
GitHub
Unverified
parent
07965844a5
commit
887a78e280
@@ -43,11 +43,13 @@ export function openSessionTab(tabs: SessionTab[], tab: SessionTab): SessionTab[
|
||||
return tabs.map((item, position) => (position === index ? { ...item, title: tab.title } : item))
|
||||
}
|
||||
|
||||
export function closeSessionTab(tabs: SessionTab[], sessionID: string) {
|
||||
export function closeSessionTab(tabs: SessionTab[], sessionID: string, lastIndex?: number) {
|
||||
const index = tabs.findIndex((tab) => tab.sessionID === sessionID)
|
||||
// Like openSessionTab and moveSessionTab, a no-op returns the same reference so callers can
|
||||
// detect it by identity.
|
||||
if (index === -1) return { tabs, next: undefined }
|
||||
// detect it by identity. Another TUI sharing the tab list may already have closed the tab, so
|
||||
// its last known index still picks the neighbour that took its place.
|
||||
if (index === -1)
|
||||
return { tabs, next: lastIndex === undefined ? undefined : tabs[Math.min(lastIndex, tabs.length - 1)]?.sessionID }
|
||||
return {
|
||||
tabs: tabs.filter((tab) => tab.sessionID !== sessionID),
|
||||
next: tabs[index + 1]?.sessionID ?? tabs[index - 1]?.sessionID,
|
||||
|
||||
@@ -150,6 +150,15 @@ export const { use: useSessionTabs, provider: SessionTabsProvider } = createSimp
|
||||
const sessionID = current()
|
||||
return sessionID !== undefined && !state().tabs.some((tab) => tab.sessionID === sessionID)
|
||||
}, false)
|
||||
// Where the viewed session's tab last sat. Another TUI sharing the tab list can close it while
|
||||
// this one keeps showing the session; closing it here then still moves to a neighbour.
|
||||
const currentTab = createMemo<{ sessionID: string; index: number } | undefined>((previous) => {
|
||||
const sessionID = current()
|
||||
if (!sessionID) return
|
||||
const index = state().tabs.findIndex((tab) => tab.sessionID === sessionID)
|
||||
if (index !== -1) return { sessionID, index }
|
||||
if (previous?.sessionID === sessionID) return previous
|
||||
})
|
||||
const status = (sessionID: string) => {
|
||||
const session = root(sessionID)
|
||||
const members = family(session)
|
||||
@@ -353,7 +362,8 @@ export const { use: useSessionTabs, provider: SessionTabsProvider } = createSimp
|
||||
scrollAnchors.delete(id)
|
||||
setExpandedGroups(id, undefined)
|
||||
})
|
||||
const closed = closeSessionTab(state().tabs, target)
|
||||
const known = currentTab()
|
||||
const closed = closeSessionTab(state().tabs, target, known?.sessionID === target ? known.index : undefined)
|
||||
const selected = navigate && current() === target
|
||||
if (closed.tabs === state().tabs && !selected) return
|
||||
const previous = selected
|
||||
|
||||
@@ -112,6 +112,14 @@ describe("session tabs", () => {
|
||||
expect(closeSessionTab(tabs, "missing").tabs).toBe(tabs)
|
||||
})
|
||||
|
||||
test("closing an already removed tab picks the neighbour at its last index", () => {
|
||||
const tabs = [{ sessionID: "a" }, { sessionID: "c" }]
|
||||
expect(closeSessionTab(tabs, "b", 1)).toEqual({ tabs, next: "c" })
|
||||
expect(closeSessionTab(tabs, "d", 2).next).toBe("c")
|
||||
expect(closeSessionTab(tabs, "b").next).toBeUndefined()
|
||||
expect(closeSessionTab([], "b", 0).next).toBeUndefined()
|
||||
})
|
||||
|
||||
test("cycles through a filtered tab set in either direction", () => {
|
||||
const tabs = ["a", "c", "e"].map((sessionID) => ({ sessionID }))
|
||||
expect(cycleSessionTab(tabs, "c", 1)?.sessionID).toBe("e")
|
||||
|
||||
@@ -728,6 +728,39 @@ test("closing a tab is not undone by another TUI viewing the same session", asyn
|
||||
}
|
||||
})
|
||||
|
||||
test("closing a session whose tab another TUI closed selects its former neighbour", async () => {
|
||||
await using temporary = await tmpdir()
|
||||
const clients: Awaited<ReturnType<typeof renderSessionTabs>>[] = []
|
||||
|
||||
try {
|
||||
const first = await renderSessionTabs("one", {
|
||||
state: temporary.path,
|
||||
persisted: ["one", "two", "three", "four", "five"],
|
||||
})
|
||||
clients.push(first)
|
||||
const second = await renderSessionTabs("three", { state: temporary.path })
|
||||
clients.push(second)
|
||||
await wait(() => second.tabs.tabs().length === 5, 2_000, "second client to load tabs")
|
||||
|
||||
first.tabs.close("three")
|
||||
await first.flush()
|
||||
await wait(
|
||||
() => !second.tabs.tabs().some((tab) => tab.sessionID === "three"),
|
||||
2_000,
|
||||
"second client to observe close",
|
||||
)
|
||||
// The other client's close leaves this view in place rather than switching sessions under the user.
|
||||
expect(second.route.data).toEqual({ type: "session", sessionID: "three" })
|
||||
|
||||
second.tabs.close()
|
||||
await wait(() => second.route.data.type !== "session" || second.route.data.sessionID !== "three")
|
||||
expect(second.route.data).toEqual({ type: "session", sessionID: "four" })
|
||||
expect(second.tabs.tabs().map((tab) => tab.sessionID)).toEqual(["one", "two", "four", "five"])
|
||||
} finally {
|
||||
await Promise.allSettled(clients.map((client) => client.destroy()))
|
||||
}
|
||||
})
|
||||
|
||||
test("user prompt admissions pulse an already-busy background tab", async () => {
|
||||
const setup = await renderSessionTabs("background", { persisted: ["background"] })
|
||||
|
||||
|
||||
Reference in New Issue
Block a user