summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/terminal.tsx
diff options
context:
space:
mode:
authorAdam <[email protected]>2026-02-12 09:49:14 -0600
committerGitHub <[email protected]>2026-02-12 09:49:14 -0600
commitff4414bb152acfddb5c0eb073c38bedc1df4ae14 (patch)
tree78381c67d21ef6f089647f6b19e7aa2976840dbc /packages/app/src/context/terminal.tsx
parent56ad2db02055955f926fda0e4a89055b22ead6f9 (diff)
downloadopencode-ff4414bb152acfddb5c0eb073c38bedc1df4ae14.tar.gz
opencode-ff4414bb152acfddb5c0eb073c38bedc1df4ae14.zip
chore: refactor packages/app files (#13236)
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Frank <[email protected]>
Diffstat (limited to 'packages/app/src/context/terminal.tsx')
-rw-r--r--packages/app/src/context/terminal.tsx67
1 files changed, 37 insertions, 30 deletions
diff --git a/packages/app/src/context/terminal.tsx b/packages/app/src/context/terminal.tsx
index c7816158c..0e6aa08cb 100644
--- a/packages/app/src/context/terminal.tsx
+++ b/packages/app/src/context/terminal.tsx
@@ -79,19 +79,38 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
}),
)
- const unsub = sdk.event.on("pty.exited", (event: { properties: { id: string } }) => {
- const id = event.properties.id
- if (!store.all.some((x) => x.id === id)) return
+ const pickNextTerminalNumber = () => {
+ const existingTitleNumbers = new Set(
+ store.all.flatMap((pty) => {
+ const direct = Number.isFinite(pty.titleNumber) && pty.titleNumber > 0 ? pty.titleNumber : undefined
+ if (direct !== undefined) return [direct]
+ const parsed = numberFromTitle(pty.title)
+ if (parsed === undefined) return []
+ return [parsed]
+ }),
+ )
+
+ return (
+ Array.from({ length: existingTitleNumbers.size + 1 }, (_, index) => index + 1).find(
+ (number) => !existingTitleNumbers.has(number),
+ ) ?? 1
+ )
+ }
+
+ const removeExited = (id: string) => {
+ const all = store.all
+ const index = all.findIndex((x) => x.id === id)
+ if (index === -1) return
+ const filtered = all.filter((x) => x.id !== id)
+ const active = store.active === id ? filtered[0]?.id : store.active
batch(() => {
- setStore(
- "all",
- store.all.filter((x) => x.id !== id),
- )
- if (store.active === id) {
- const remaining = store.all.filter((x) => x.id !== id)
- setStore("active", remaining[0]?.id)
- }
+ setStore("all", filtered)
+ setStore("active", active)
})
+ }
+
+ const unsub = sdk.event.on("pty.exited", (event: { properties: { id: string } }) => {
+ removeExited(event.properties.id)
})
onCleanup(unsub)
@@ -117,7 +136,7 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
return {
ready,
- all: createMemo(() => Object.values(store.all)),
+ all: createMemo(() => store.all),
active: createMemo(() => store.active),
clear() {
batch(() => {
@@ -126,20 +145,7 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
})
},
new() {
- const existingTitleNumbers = new Set(
- store.all.flatMap((pty) => {
- const direct = Number.isFinite(pty.titleNumber) && pty.titleNumber > 0 ? pty.titleNumber : undefined
- if (direct !== undefined) return [direct]
- const parsed = numberFromTitle(pty.title)
- if (parsed === undefined) return []
- return [parsed]
- }),
- )
-
- const nextNumber =
- Array.from({ length: existingTitleNumbers.size + 1 }, (_, index) => index + 1).find(
- (number) => !existingTitleNumbers.has(number),
- ) ?? 1
+ const nextNumber = pickNextTerminalNumber()
sdk.client.pty
.create({ title: `Terminal ${nextNumber}` })
@@ -162,10 +168,8 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
})
},
update(pty: Partial<LocalPTY> & { id: string }) {
- const index = store.all.findIndex((x) => x.id === pty.id)
- if (index !== -1) {
- setStore("all", index, (existing) => ({ ...existing, ...pty }))
- }
+ const previous = store.all.find((x) => x.id === pty.id)
+ if (previous) setStore("all", (all) => all.map((item) => (item.id === pty.id ? { ...item, ...pty } : item)))
sdk.client.pty
.update({
ptyID: pty.id,
@@ -173,6 +177,9 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
size: pty.cols && pty.rows ? { rows: pty.rows, cols: pty.cols } : undefined,
})
.catch((error: unknown) => {
+ if (previous) {
+ setStore("all", (all) => all.map((item) => (item.id === pty.id ? previous : item)))
+ }
console.error("Failed to update terminal", error)
})
},