Files
template-app/modules/branding/ui/transport.tsx
T

29 lines
1.2 KiB
TypeScript

"use client"
// How this module's interactive components reach the API: through a transport the app provides
// (apps/web/src/ui/transport.tsx). A module may not import the app, and a server component cannot
// hand a function to a client one, so the seam is a context -- the same one every module with a
// client component declares for itself, until `@conusai-kit/ui/data` is the one shared facade.
import { createContext, type ReactElement, type ReactNode, useContext } from "react"
export type Transport = (procedure: string, input: unknown) => Promise<unknown>
export type DescribeError = (error: unknown) => string
interface Provided {
readonly call: Transport
readonly describe: DescribeError
}
const TransportContext = createContext<Provided | undefined>(undefined)
export function TransportProvider({ call, describe, children }: Provided & { children: ReactNode }): ReactElement {
return <TransportContext.Provider value={{ call, describe }}>{children}</TransportContext.Provider>
}
export function useTransport(): Provided {
const provided = useContext(TransportContext)
if (!provided) throw new Error("branding: wrap the page in a TransportProvider (apps/web/src/ui/transport.tsx)")
return provided
}