diff options
Diffstat (limited to 'cloud/web/src/util')
| -rw-r--r-- | cloud/web/src/util/context.tsx | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/cloud/web/src/util/context.tsx b/cloud/web/src/util/context.tsx new file mode 100644 index 000000000..d1c6f4e7f --- /dev/null +++ b/cloud/web/src/util/context.tsx @@ -0,0 +1,26 @@ +import { ParentProps, Show, createContext, useContext } from "solid-js" + +export function createInitializedContext< + Name extends string, + T extends { ready: boolean }, +>(name: Name, cb: () => T) { + const ctx = createContext<T>() + + return { + use: () => { + const context = useContext(ctx) + if (!context) throw new Error(`No ${name} context`) + return context + }, + provider: (props: ParentProps) => { + const value = cb() + return ( + <Show when={value.ready}> + <ctx.Provider value={value} {...props}> + {props.children} + </ctx.Provider> + </Show> + ) + }, + } +} |
