refactor(tui): centralize active location
This commit is contained in:
parent
f4f761246a
commit
a32c480d45
9 changed files with 218 additions and 249 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue