refactor(tui): centralize active location

This commit is contained in:
Dax Raad 2026-07-14 16:17:53 -04:00
commit a32c480d45
9 changed files with 218 additions and 249 deletions

View file

@ -1,14 +1,24 @@
import type { LocationRef } from "@opencode-ai/client"
import { createContext, useContext, type Accessor, type ParentProps } from "solid-js"
import { createContext, createSignal, useContext, type Accessor, type ParentProps, type Setter } from "solid-js"
const context = createContext<Accessor<LocationRef | undefined>>()
const context = createContext<{
current: Accessor<LocationRef | undefined>
set: Setter<LocationRef | undefined>
}>()
export function LocationProvider(props: ParentProps<{ location?: LocationRef }>) {
return <context.Provider value={() => props.location}>{props.children}</context.Provider>
export function LocationProvider(props: ParentProps) {
const [current, set] = createSignal<LocationRef>()
return <context.Provider value={{ current, set }}>{props.children}</context.Provider>
}
export function useLocation() {
const value = useContext(context)
if (!value) throw new Error("Location context must be used within a LocationProvider")
return value
return value.current
}
export function useSetLocation() {
const value = useContext(context)
if (!value) throw new Error("Location context must be used within a LocationProvider")
return value.set
}