summaryrefslogtreecommitdiffhomepage
path: root/packages/ui/src/context/helper.tsx
diff options
context:
space:
mode:
authorAdam <[email protected]>2025-10-30 13:49:29 -0500
committerAdam <[email protected]>2025-10-30 13:49:29 -0500
commitdc6e54503cb400ea2533740c9a92d09c8a50d077 (patch)
tree7abad7c0275fe646395a2f4f44d90e5f4a48dbe0 /packages/ui/src/context/helper.tsx
parent2a0b67d84f048207d20d952cafa10c430451dc70 (diff)
downloadopencode-dc6e54503cb400ea2533740c9a92d09c8a50d077.tar.gz
opencode-dc6e54503cb400ea2533740c9a92d09c8a50d077.zip
wip: desktop work
Diffstat (limited to 'packages/ui/src/context/helper.tsx')
-rw-r--r--packages/ui/src/context/helper.tsx25
1 files changed, 25 insertions, 0 deletions
diff --git a/packages/ui/src/context/helper.tsx b/packages/ui/src/context/helper.tsx
new file mode 100644
index 000000000..6be88e775
--- /dev/null
+++ b/packages/ui/src/context/helper.tsx
@@ -0,0 +1,25 @@
+import { createContext, Show, useContext, type ParentProps } from "solid-js"
+
+export function createSimpleContext<T, Props extends Record<string, any>>(input: {
+ name: string
+ init: ((input: Props) => T) | (() => T)
+}) {
+ const ctx = createContext<T>()
+
+ return {
+ provider: (props: ParentProps<Props>) => {
+ const init = input.init(props)
+ return (
+ // @ts-expect-error
+ <Show when={init.ready === undefined || init.ready === true}>
+ <ctx.Provider value={init}>{props.children}</ctx.Provider>
+ </Show>
+ )
+ },
+ use() {
+ const value = useContext(ctx)
+ if (!value) throw new Error(`${input.name} context must be used within a context provider`)
+ return value
+ },
+ }
+}