refactor(tui): extract standalone package (#31193)
This commit is contained in:
parent
7a2c49e762
commit
106f8e94d6
84 changed files with 1147 additions and 1918 deletions
62
packages/tui/src/context/runtime.tsx
Normal file
62
packages/tui/src/context/runtime.tsx
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import { createComponent, createContext, type JSX, useContext } from "solid-js"
|
||||
|
||||
export type TuiPaths = Readonly<{
|
||||
cwd: string
|
||||
home: string
|
||||
state: string
|
||||
worktree: string
|
||||
}>
|
||||
|
||||
export type TuiTerminalEnvironment = Readonly<{
|
||||
platform: string
|
||||
multiplexer?: "tmux" | "screen"
|
||||
displayServer?: "wayland" | "x11"
|
||||
}>
|
||||
|
||||
export type TuiStartup = Readonly<{
|
||||
initialRoute?: unknown
|
||||
skipInitialLoading: boolean
|
||||
}>
|
||||
|
||||
const PathsContext = createContext<TuiPaths>()
|
||||
const TerminalEnvironmentContext = createContext<TuiTerminalEnvironment>()
|
||||
const StartupContext = createContext<TuiStartup>()
|
||||
|
||||
function provider<T>(context: ReturnType<typeof createContext<T>>, value: T, children: () => JSX.Element) {
|
||||
return createComponent(context.Provider, {
|
||||
value: Object.freeze({ ...value }),
|
||||
get children() {
|
||||
return children()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function TuiPathsProvider(props: { value: TuiPaths; children: JSX.Element }) {
|
||||
return provider(PathsContext, props.value, () => props.children)
|
||||
}
|
||||
|
||||
export function TuiTerminalEnvironmentProvider(props: { value: TuiTerminalEnvironment; children: JSX.Element }) {
|
||||
return provider(TerminalEnvironmentContext, props.value, () => props.children)
|
||||
}
|
||||
|
||||
export function TuiStartupProvider(props: { value: TuiStartup; children: JSX.Element }) {
|
||||
return provider(StartupContext, props.value, () => props.children)
|
||||
}
|
||||
|
||||
function required<T>(context: ReturnType<typeof createContext<T>>, name: string) {
|
||||
const value = useContext(context)
|
||||
if (!value) throw new Error(`${name} is missing`)
|
||||
return value
|
||||
}
|
||||
|
||||
export function useTuiPaths() {
|
||||
return required(PathsContext, "TuiPathsProvider")
|
||||
}
|
||||
|
||||
export function useTuiTerminalEnvironment() {
|
||||
return required(TerminalEnvironmentContext, "TuiTerminalEnvironmentProvider")
|
||||
}
|
||||
|
||||
export function useTuiStartup() {
|
||||
return required(StartupContext, "TuiStartupProvider")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue