32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { createContext, createMemo, createSignal, useContext, type Accessor, type ParentProps, type Setter } from "solid-js"
|
|
import { useSync } from "../../context/sync"
|
|
|
|
export type HomeSessionDestination = { type: "directory"; directory: string } | { type: "new" }
|
|
|
|
type Context = {
|
|
destination: Accessor<HomeSessionDestination | undefined>
|
|
setDestination: Setter<HomeSessionDestination | undefined>
|
|
clear: () => void
|
|
}
|
|
|
|
const HomeSessionDestinationContext = createContext<Context>()
|
|
|
|
export function HomeSessionDestinationProvider(props: ParentProps) {
|
|
const sync = useSync()
|
|
const [selected, setDestination] = createSignal<HomeSessionDestination>()
|
|
const destination = createMemo<HomeSessionDestination>(() =>
|
|
selected() ?? { type: "directory", directory: sync.path.directory || process.cwd() },
|
|
)
|
|
return (
|
|
<HomeSessionDestinationContext.Provider
|
|
value={{ destination, setDestination, clear: () => setDestination(undefined) }}
|
|
>
|
|
{props.children}
|
|
</HomeSessionDestinationContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useHomeSessionDestination() {
|
|
return useContext(HomeSessionDestinationContext)
|
|
}
|