refactor(tui): centralize active location · argszero/opencode@a32c480 · GitHub
Skip to content

Commit a32c480

Browse files
committed
refactor(tui): centralize active location
1 parent f4f7612 commit a32c480

9 files changed

Lines changed: 218 additions & 249 deletions

File tree

packages/plugin/src/v2/tui/context.ts

Lines changed: 1 addition & 0 deletions

packages/tui/src/app.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,15 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) {
333333
<PermissionProvider>
334334
<ProjectProvider>
335335
<DataProvider>
336-
<ThemeProvider mode={mode}>
337-
<LocalProvider>
338-
<PromptStashProvider>
339-
<DialogProvider>
340-
<FrecencyProvider>
341-
<PromptHistoryProvider>
342-
<PromptRefProvider>
343-
<EditorContextProvider>
344-
<LocationProvider>
336+
<LocationProvider>
337+
<ThemeProvider mode={mode}>
338+
<LocalProvider>
339+
<PromptStashProvider>
340+
<DialogProvider>
341+
<FrecencyProvider>
342+
<PromptHistoryProvider>
343+
<PromptRefProvider>
344+
<EditorContextProvider>
345345
<PluginProvider packages={input.packages}>
346346
<App
347347
pair={
@@ -354,15 +354,15 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) {
354354
}
355355
/>
356356
</PluginProvider>
357-
</LocationProvider>
358-
</EditorContextProvider>
359-
</PromptRefProvider>
360-
</PromptHistoryProvider>
361-
</FrecencyProvider>
362-
</DialogProvider>
363-
</PromptStashProvider>
364-
</LocalProvider>
365-
</ThemeProvider>
357+
</EditorContextProvider>
358+
</PromptRefProvider>
359+
</PromptHistoryProvider>
360+
</FrecencyProvider>
361+
</DialogProvider>
362+
</PromptStashProvider>
363+
</LocalProvider>
364+
</ThemeProvider>
365+
</LocationProvider>
366366
</DataProvider>
367367
</ProjectProvider>
368368
</PermissionProvider>

packages/tui/src/component/prompt/move.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { useClient } from "../../context/client"
77
import { useToast } from "../../ui/toast"
88
import { DialogMoveSession, type MoveSessionSelection } from "../dialog-move-session"
99
import { DialogWorkspaceFileChanges } from "../dialog-workspace-file-changes"
10-
import { useHomeSessionDestination } from "../../routes/home/session-destination"
1110
import { useProject } from "../../context/project"
1211
import { useData } from "../../context/data"
1312

@@ -19,13 +18,13 @@ export function usePromptMove(input: { projectID: () => string | undefined; sess
1918
const dialog = useDialog()
2019
const client = useClient()
2120
const toast = useToast()
22-
const homeDestination = useHomeSessionDestination()
2321
const project = useProject()
2422
const data = useData()
2523
const paths = useTuiPaths()
2624
const [creating, setCreating] = createSignal(false)
2725
const [creatingDots, setCreatingDots] = createSignal(3)
2826
const [progress, setProgress] = createSignal<string>()
27+
const [destination, setDestination] = createSignal<MoveSessionSelection>()
2928

3029
async function create(name: string) {
3130
const projectID = await resolveProjectID()
@@ -49,7 +48,7 @@ export function usePromptMove(input: { projectID: () => string | undefined; sess
4948
setProgress("Creating session")
5049
return directory
5150
} catch (err) {
52-
homeDestination?.clear()
51+
setDestination(undefined)
5352
setProgress(undefined)
5453
setCreating(false)
5554
toast.show({ title: "Creating workspace failed", message: errorMessage(err), variant: "error" })
@@ -69,7 +68,7 @@ export function usePromptMove(input: { projectID: () => string | undefined; sess
6968
<DialogMoveSession
7069
projectID={projectID}
7170
current={
72-
homeDestination?.destination() ??
71+
destination() ??
7372
(session
7473
? {
7574
type: "directory",
@@ -82,11 +81,11 @@ export function usePromptMove(input: { projectID: () => string | undefined; sess
8281
subdirectory: project.instance.directory() !== project.instance.path().worktree,
8382
})
8483
}
85-
onCurrentChange={(selection) => homeDestination?.setDestination(selection)}
84+
onCurrentChange={setDestination}
8685
onSelect={(selection) => {
8786
const sessionID = input.sessionID()
8887
if (!sessionID) {
89-
homeDestination?.setDestination(selection)
88+
setDestination(selection)
9089
dialog.clear()
9190
return
9291
}
@@ -144,11 +143,11 @@ export function usePromptMove(input: { projectID: () => string | undefined; sess
144143
return data.session.get(sessionID)
145144
}
146145

147-
const pending = createMemo(() => Boolean(homeDestination?.destination()))
148-
const pendingNew = createMemo(() => homeDestination?.destination()?.type === "new")
146+
const pending = createMemo(() => Boolean(destination()))
147+
const pendingNew = createMemo(() => destination()?.type === "new")
149148

150149
async function getDirectory() {
151-
const value = homeDestination?.destination()
150+
const value = destination()
152151
if (!value) return
153152
if (value.type === "directory") {
154153
return value.directory
@@ -161,7 +160,7 @@ export function usePromptMove(input: { projectID: () => string | undefined; sess
161160
}
162161

163162
function finishSubmit() {
164-
homeDestination?.clear()
163+
setDestination(undefined)
165164
setProgress(undefined)
166165
setCreating(false)
167166
}
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import type { LocationRef } from "@opencode-ai/client"
2-
import { createContext, useContext, type Accessor, type ParentProps } from "solid-js"
2+
import { createContext, createSignal, useContext, type Accessor, type ParentProps, type Setter } from "solid-js"
33

4-
const context = createContext<Accessor<LocationRef | undefined>>()
4+
const context = createContext<{
5+
current: Accessor<LocationRef | undefined>
6+
set: Setter<LocationRef | undefined>
7+
}>()
58

6-
export function LocationProvider(props: ParentProps<{ location?: LocationRef }>) {
7-
return <context.Provider value={() => props.location}>{props.children}</context.Provider>
9+
export function LocationProvider(props: ParentProps) {
10+
const [current, set] = createSignal<LocationRef>()
11+
return <context.Provider value={{ current, set }}>{props.children}</context.Provider>
812
}
913

1014
export function useLocation() {
1115
const value = useContext(context)
1216
if (!value) throw new Error("Location context must be used within a LocationProvider")
13-
return value
17+
return value.current
18+
}
19+
20+
export function useSetLocation() {
21+
const value = useContext(context)
22+
if (!value) throw new Error("Location context must be used within a LocationProvider")
23+
return value.set
1424
}

packages/tui/src/feature-plugins/home/footer.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ import { createMemo, Match, Show, Switch } from "solid-js"
44
import { useTerminalDimensions } from "@opentui/solid"
55
import { useTuiPaths } from "../../context/runtime"
66
import { useTheme } from "../../context/theme"
7-
import { useHomeSessionDestination } from "../../routes/home/session-destination"
87
import { abbreviateHome } from "../../runtime"
98
import { FilePath } from "../../ui/file-path"
109

1110
function Directory(props: { context: Plugin.Context; maxWidth: number }) {
1211
const { theme } = useTheme()
13-
const destination = useHomeSessionDestination()
1412
const paths = useTuiPaths()
15-
const directory = createMemo(() => {
16-
const selected = destination?.destination()
17-
if (!selected || selected.type === "new") return
18-
return abbreviateHome(selected.directory || props.context.data.location.default().directory, paths.home)
19-
})
13+
const directory = createMemo(() =>
14+
props.context.location ? abbreviateHome(props.context.location.directory, paths.home) : undefined,
15+
)
2016

2117
return (
2218
<Show when={directory()}>
@@ -27,7 +23,7 @@ function Directory(props: { context: Plugin.Context; maxWidth: number }) {
2723

2824
function Mcp(props: { context: Plugin.Context }) {
2925
const { theme } = useTheme()
30-
const list = createMemo(() => props.context.data.location.mcp.server.list() ?? [])
26+
const list = createMemo(() => props.context.data.location.mcp.server.list(props.context.location) ?? [])
3127
const failed = createMemo(() => list().some((item) => item.status.status === "failed"))
3228
const count = createMemo(() => list().filter((item) => item.status.status === "connected").length)
3329

@@ -55,7 +51,7 @@ function View(props: { context: Plugin.Context }) {
5551
const { theme } = useTheme()
5652
const dimensions = useTerminalDimensions()
5753
const mcpWidth = createMemo(() => {
58-
const list = props.context.data.location.mcp.server.list() ?? []
54+
const list = props.context.data.location.mcp.server.list(props.context.location) ?? []
5955
if (list.length === 0) return 0
6056
const count = list.filter((item) => item.status.status === "connected").length
6157
return Bun.stringWidth(`⊙ ${count} MCP /status`) + 2

packages/tui/src/plugin/context.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useData } from "../context/data"
2121
import { Keymap } from "../context/keymap"
2222
import { useRoute } from "../context/route"
2323
import { useTuiLifecycle } from "../context/runtime"
24+
import { useLocation } from "../context/location"
2425
import { builtins } from "./builtins"
2526

2627
export interface PackageResolver {
@@ -63,6 +64,7 @@ export function PluginProvider(props: ParentProps<{ packages: PackageResolver }>
6364
const keymap = Keymap.use()
6465
const shortcuts = Keymap.useShortcuts()
6566
const lifecycle = useTuiLifecycle()
67+
const location = useLocation()
6668
const directory = config.path ? path.dirname(config.path) : process.cwd()
6769
const [store, setStore] = createStore({
6870
ready: false,
@@ -82,6 +84,9 @@ export function PluginProvider(props: ParentProps<{ packages: PackageResolver }>
8284
const owned: Dispose[] = []
8385
const context: Context = {
8486
options: item.options ?? {},
87+
get location() {
88+
return location()
89+
},
8590
client: client.api,
8691
data,
8792
keymap: {

packages/tui/src/routes/home.tsx

Lines changed: 41 additions & 41 deletions

packages/tui/src/routes/home/session-destination.tsx

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)